Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What does a varName inside [] mean????

Status
Not open for further replies.

FederalProgrammer

Programmer
Jul 2, 2003
318
CA
I am maintaining a code that uses, [] quite a lot. Here's how it's used:
Public [DataTable] as DataTable

I think it's got something to do with the assembly file... i'm not sure.... does any body know anything about this??
Cheers!
 
The square brackets allow you to use variable names that would otherwise be reserved words in Visual Basic, or in the framework.

This is often necessary because VB.NET is not case-sensitive. So you can do this:
Code:
   Dim assembly As Assembly
Because the VB parser would automatically make the first "assembly" into "Assembly", which is a type in the System namespace. You'd have to do it like either of these ways:
Code:
  Dim [Assembly] As Assembly
  Dim MyAssembly As Assembly
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
thanx for the tip....
but check out the following declaration:
' load up assembly file
Dim a As [Assembly] = [Assembly].GetExecutingAssembly()

If a is being delared as an assembly, that should probably mean, Assembly is a type... but that's not the case:

Dim a as Assembly
doesn't work...
The error states the keyword is not a type... which is a bit bizzar...

Any ways, let me know if you know why this is...
cheers!
 
Federal,

chiph is correct is his answer. The [] are used to enable the use of a keyword as a variable name.

The correct syntax for what you are trying to do is

Dim a As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()

Either by using the fully qualified name or by adding Imports.

The error you mentioned is because the compiler is looking for a class named [Assembly] like this

Public Class [Assembly]

End Class

Which of course does not exist.

DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb
-----------------------------------
If you can't explain it simply, you don't understand it well enough.
- A. Einstein
 
Hay Doc,
Yes, you are right... but you see, the following code works perfectly:
Dim a As [Assembly] = [Assembly].GetExecutingAssembly()
eventhough there are no local [Assembly] class anywhere!!

what is not working is:
Dim a as Assembly

You see, system complains about "Assembly" being a keyword (I did in fact imported system.reflection)...

This is why the confusion is started in the first place. Any ways, I'd appreciate any further explainations....
cheers!
 
The reason for this is that Assembly is not only a KEYWORD but it is also a CLASS.

The compiler gets confused on what you are trying to do.

Which is why

Dim a As [Assembly] = [Assembly].GetExecutingAssembly()

Works ( It knows what you are trying to do)

And


Dim a as Assembly or Dim a as [Assembly]

does not (here it does not )

Which is why you should use the fully qualified name

Dim a AS System.Reflection.Assembly

If you create a class called textbox in your windows application and tried to instantiate like so

Dim myText as Textbox

You would get an Ambiguous error because it does not know which textbox class you want to use, yours or the one in Windows.System.Forms.

Assembly is more convoluted because it is both a class AND a keyword.



DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb
-----------------------------------
If you can't explain it simply, you don't understand it well enough.
- A. Einstein
 
that makes sense; I am not arguing the correctness of your answer; but, to me, a class defines a datatype. let's say I have the following class:
Public Class Employee
End class

Shouldn't I be able to instantiate from this class by saying:
dim E as Employee

And if Assembly is a class, shouldn't one be able to do the same? If system.reflection.assembly is different from the Assembly class, something's gone wrong in the design process. wouldn't you say?
 
Assembly is not different from System.Reflection.Assembly it is just that it has the distinction of being both a KEYWORD & CLASS



DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb
-----------------------------------
If you can't explain it simply, you don't understand it well enough.
- A. Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top