views:

997

answers:

5

How do I declare "as any" in VB.NET, or what is the equivalent?

A: 

VB.Net doesn't support the "As Any" keyword. You'll need to explicitly specify the type.

1800 INFORMATION
+3  A: 

The closest you can get is:

Dim var as Object

It's not exactly the same as VB6's as Any (which stores values in a Variant) but you can store variables of any type as Object, albeit boxed.

Groky
+3  A: 

VB.NET does not support the as any keyword, VB.NET is a strongly typed language, you can however (with .NET 3.5) use implicit typing in VB

Dim fred = "Hello World" will implicitly type fred as a string variable. If you want to simply hold a value that you do not know the type of at design time then you can simply declare your variable as object (the mother of all objects) NOTE, this usually is a red flag for code reviewers, so make sure you have a good reason ready :-)

Tim Jarvis
+1  A: 

I suppose you have problems with converting WinAPI declarations. Sometimes you can get away if you just declare your variable as string or integer because that is the real type of value returned.

You can also try marshaling:

<MarshalAsAttribute(UnmanagedType.AsAny)> ByRef buff As Object
Hugo Riley
+2  A: 

As Any must be referring to Windows API declarations, as it can't be used in variable declarations. You can use overloading: just repeat the declarations for each different data type you wish to pass. VB.NET picks out the one that matches the argument you pass in your call.

This is better than As Any was in VB6 because the compiler can still do type-checking.

MarkJ