IMD SolutionComputing LibraryShop main store

Troubleshooting

Why AFElementSearch throws error BC30182 type expected

Edge computing and IIoT Published 6 min read

Short answer

BC30182 Type expected is a Visual Basic compiler and debugger diagnostic, not a message from AFElementSearch or the AF Server. Keep the Category-only query unescaped, materialize FindObjects with ToList into a List(Of AFElement), and read any genuine search failure as an exception at that materialization line.

On this page
  1. What the BC30182 message actually reports
  2. Why the category query string is not the failure
  3. Where the deferred IEnumerable produces the symptom
  4. Working case: the Edict:ARSrxr_PhaseTimes category search as a list
  5. Scoping the run under a parent element
  6. The decision for each case
  7. Questions asked

An AFElementSearch call that shows BC30182 Type expected is not failing on its query string. BC30182 is a Visual Basic diagnostic, raised where the compiler or the debugger expression evaluator needs a type name; the AF SDK never emits it. The Category-only query, colon included, is valid and needs no escaping. The correction that changes behavior is to materialize FindObjects() once with ToList() into a List(Of AFElement): the search then executes at one known line, the count becomes a property read, and any genuine search fault appears there as a real exception with a call stack. Keep the explicit As AFElementSearch declaration, keep the query as PI System Explorer built it, and judge the search by what that materialization line does.

What the BC30182 message actually reports #

Microsoft documents BC30182 as the Visual Basic diagnostic for text found where a type is required, and its documented corrections are to confirm the type is defined and spelled correctly. Neither correction touches a query string, so no edit to the filter can cause the message or remove it. The AF SDK is a .NET library: it throws exceptions and returns objects, and it has no Visual Basic diagnostic codes of its own.

In the working case the text appeared around the search object while the code ran under the debugger, and it survived two changes that had nothing to do with a type: switching the query, and replacing the explicit declaration with type inference. Both facts point to the same conclusion, so treat the message as one of three distinct states:

  • A project compile error: the build fails and the compiler names the offending declaration. Fix that before running anything.
  • A debugger evaluation diagnostic: the build is clean and the code runs, and the text appears in an inspector or watch window. Judge the search by runtime behavior, not by that display.
  • An enumeration exception: the search actually executed and failed. Its message and call stack name the real cause.

If the project builds, a BC30182 shown near the search object is toolchain text. The next piece of evidence is the exception, or the absence of one, at the line that enumerates the results.

Why the category query string is not the failure #

The query used in the working case was Category:'Edict:ARSrxr_PhaseTimes', and it is built from a documented pair of filters: Category and CategoryName. CategoryName matches the category by name; Category also accepts the category's identity, including its GUID. The colon inside the quoted name was the obvious parsing suspect; the query that worked retained it.

Guidance reported for AFElementSearch holds that the query string should explicitly name an element template, an element name or a naming pattern. The working case kept a Category-only string and got the expected elements, so treat that guidance as a refinement for deterministic searches, not a parser precondition: begin from the Category-only form, add scope deliberately, and widen the query only when a real AF exception names the query text. AVEVA documents the query-string syntax and the full filter list for AF SDK searches, so test any new filter name against that reference rather than against the debugger.

When the diagnostic target is category identity itself, resolve the object and filter on its GUID:

Dim elemCategory = AFDB.ElementCategories(CategoryName)
Dim QryStr = $"Category:'{elemCategory.ID.ToString("B")}'"

Check the lookup result before reading ID: a failed category lookup points at the database selection or the category name, never at the enumeration.

Where the deferred IEnumerable produces the symptom #

FindObjects() returns IEnumerable(Of AFElement), and the search runs when that sequence is enumerated, not when FindObjects() returns. This is ordinary .NET deferred execution, and it governs every search class on the PI AF SDK: the native path from a C# application to PI.

A deferred sequence has no Count property. In Visual Basic, SearchElmts.Count on that interface resolves to the LINQ Count extension, with the parentheses optional, and every read enumerates the whole search again. The log line that reports the count therefore executes the search at logging time, and the caller's loop executes it once more.

When the debugger is asked to display a deferred sequence, its expression evaluator renders values from the code being debugged, and that evaluation context is where the Type expected text appeared in this case. Nothing about the AF query was evaluated at that moment.

Materializing fixes all three behaviors at once. One statement, ElmtQry.FindObjects().ToList(), moves execution to a single line: failures throw there with a call stack, the count becomes a property read on a populated List(Of AFElement), and no downstream consumer re-runs the search by accident.

Working case: the Edict:ARSrxr_PhaseTimes category search as a list #

Inputs, each as the working case gives it:

  • Category name passed to the function: ARSrxr_PhaseTimes, a non-null String.
  • Full category filter value: Edict:ARSrxr_PhaseTimes, the stored name used verbatim inside single quotes.
  • Optional root path for scoped runs: HSC\%DCC\0160, taken from the parent element with GetPath().
  • Cache timeout: 10 minutes, set with TimeSpan.FromMinutes(10).
  • Return contract: List(Of AFElement).
  • Database: AFDB, a connected AFDatabase object, valid before the call.

Rule in words: build the query from the Category filter and, only when a parent element is supplied, prepend a Root filter with AllDescendants:true. Construct the AFElementSearch against the database, set the cache timeout, then call FindObjects().ToList() in one statement so the search executes exactly once, at that line. Return the list and read its Count property afterwards.

Public Function FindElementsByCategory(ByVal CategoryName As String,
                                       Optional ByRef objParentElement As AFElement = Nothing
                                       ) As List(Of AFElement)
    Dim RtnName As String = "FindElementsByCategory"
    Dim QryStr As String = String.Format("Category:'{0}'", CategoryName)
    If (Not objParentElement Is Nothing) Then
        QryStr = String.Format("Root:'{0}' AllDescendants:true Category:'{1}'",
                               objParentElement.GetPath(), CategoryName)
    End If
    Dim ElmtQry As AFElementSearch = New AFElementSearch(AFDB, RtnName, QryStr)
    ElmtQry.CacheTimeout = TimeSpan.FromMinutes(10)
    Dim SearchElmts As List(Of AFElement) = ElmtQry.FindObjects().ToList()
    Return SearchElmts
End Function

Result: the Category-only query with the unescaped colon ran and returned the elements carrying that category, and the search executed exactly once, at the ToList line. A search fault would throw there, with a message and call stack that name the real cause; Count afterwards is a property read, so logging the count no longer re-executes anything.

Decision and threshold: if anything reads Count, indexes the results, or touches them a second time, materialize with ToList and return List(Of AFElement). Return the bare IEnumerable only for a caller that streams one pass and accepts the search running inside its own loop.

Scoping the run under a parent element #

The working function adds Root:'{0}' AllDescendants:true ahead of the Category filter only when objParentElement is supplied, and takes the path from objParentElement.GetPath() rather than assembling it by hand. An earlier commented branch of the same code used Parent instead of Root; the construction that worked uses Root, so keep Root for descendants under a selected root.

One check belongs with that scope: AllDescendants expresses traversal beneath the root. If the root itself can carry the category and must appear in the results, test membership with a known element before trusting the total, because a correct count can hide a wrong scope when several branches carry the same category.

The decision for each case #

  • BC30182 Type expected near the search object and the project builds: toolchain text, not an AF failure. Keep the explicit declaration, keep the unescaped query, materialize with ToList, and read what happens at that line.
  • The same text after materialization: capture the exception raised at the ToList line with its call stack. That exception is the real failure; if none is raised, the search is working and the message was display text.
  • Helper must count or hand back a reusable set: return List(Of AFElement); the search runs once and Count is free.
  • Caller streams one pass: return IEnumerable(Of AFElement), accept execution inside the caller's loop, and count with LINQ only when the extra enumeration is affordable.
  • A genuine query fault: an AF exception at the materialization line names it. Then change one filter at a time against AVEVA's documented filter list, keeping the unescaped Category form as the baseline.

Questions asked

I built the query string in PI System Explorer, so why does code reject it?

In this case the string was never rejected: the same Category filter ran and returned elements once the results were materialized. PI System Explorer executes its search when you run it there, while AFElementSearch defers execution until the sequence is enumerated in code. Keep the string, materialize with ToList, and judge the search by the exception raised at that line; a genuine query fault raises a real AF exception there, not a Visual Basic diagnostic code.

Does the colon inside the quoted category name need escaping?

No. The working query kept the full stored name Category:'Edict:ARSrxr_PhaseTimes' unescaped inside its single quotes and ran, so the colon passes through the filter untouched. Only reconsider escaping if a genuine AF parser exception names the query, and change one layer at a time from this unescaped baseline.

When should a search helper return IEnumerable instead of List?

Return the deferred IEnumerable(Of AFElement) only when the caller consumes the results in one streaming pass and you accept that the search executes inside that caller's loop, where failures will then surface. If anything downstream counts, indexes or walks the results twice, return List(Of AFElement); counting a deferred sequence re-enumerates it and can double the work the server does.

What does CacheTimeout control on an AFElementSearch?

CacheTimeout is a property of that search object, not of the database, and the working function sets it to 10 minutes with TimeSpan.FromMinutes(10) after constructing the search. Each new AFElementSearch starts with its own default. Treat the value as tuning for how long that object may reuse its results; it does not change which elements the query matches.

Keep reading

  • Explainer

    PI AF SDK: the native path from a C# application to PI

    No driver joins two OPC UA clients, so KEPServerEX needs no new driver for this. Read PI Data Archive and PI Asset Framework from C# through PI AF SDK, which needs the server-side PSA license. Any new values must flow through a real OPC UA server endpoint that a licensed PI OPC UA interface or connector collects from.

    Edge computing and IIoT

  • Explainer

    Why a PI value timestamp is not a receipt time

    No, an AFDataPipe event carries one timestamp — the value's event time, assigned by whatever wrote it — and the PI Data Archive exposes no per-value server receipt time. A sync over a one-way diode therefore restarts from the earlier of the two readers' last sent timestamps minus an overlap margin, journals every event locally before sending, and reconciles archives on a schedule. Treat 0% data loss as a requirement you validate, not something AFDataPipe guarantees.

    Edge computing and IIoT

  • Explainer

    Why PI SDK needs a real PI Server endpoint for cloud tests

    No. No public PI Server accepts direct PI SDK connections on port 5450, so a cloud address alone never yields a read-only test endpoint. Use the managed VLE included with a paid PI Developers Club membership, deploy a licensed PI System on Azure or AWS, or build new code on AF SDK — and put the read-only boundary in the server-side identity, not the client code.

    Edge computing and IIoT