VBC09335 — Type-declaration character does not match declared data type
Severity: Error · Family: VBC (static semantics / abstract syntax tree) ·
Source metadata: VBCompileErrorInfo
A name is written with a type-declaration character (%, &, ^, !, #, @ or $) that is not the type it was declared as.
The character says the type of the name it is written on: x$ is a String, x% an Integer. A name written with one is the identifier that was declared, and has the type that declaration gave it; if that is not the type the character says, the character is changing the type of a variable, which VBA does not allow.
MS-VBA reports the same error: Type-declaration character does not match declared data type.
This code is raised by SimpleNameExpressionStaticSemantics, at the name and not at the statement it is in.
How it is raised
| Situation | Example | Resolution |
|---|---|---|
| A variable declared with one character, written with another. | Dim x$ … x% = 42 |
Write the name with the character it was declared with, or with none. |
A variable declared As a type, written with the character of another. |
Dim x As String … x% = 42 |
Write it with $, or with none. |
| A name that is declared by being used, written with two characters. | y$ = "a" … y% = 1 |
Use one type for the name. |
| A name declared like a member of the library, written with the library's character. | Dim Left As Long … s = Left$ |
Rename the declaration, or qualify the library's member (VBA.Left$). |
A member of the library with no $ form, written with $. |
n = Len$(s) |
Write it with no character. |
Notes
A name written with no character never raises this: the declaration is the only thing that says its type.
A name written with a character is looked for by its identifier first, through the usual scopes, and the character is judged against what that finds: after Dim Left As Long, Left$ is that variable, and raises this. Only where the identifier finds a member of the library, or nothing, is the name the library's member that has the character in its name (Left$, Environ$), if there is one: Hex$ is then a different member from Hex, with a different type. Where the library has no such member, the identifier's member is judged as any declaration is: Len returns a Variant, so Len$ raises this. A member after a dot is looked for as written: VBA.Left$ is the library's Left$. See RD-VBAL §6.1.2.11 Strings.
Declaring one identifier twice in a procedure, with two characters or none (Dim x$ and Dim x%), is a duplicate declaration (VBC09303), not this; see RD-VBAL §2.3.1.3 Name Resolution.
See also
⏮️ VBC09334 Access not valid for the file mode | ⏭️ VBC09336 Private Enum and user defined types cannot be used in public members
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca