user11420
user11420

Reputation: 31

Visual Basic 6.0 to VB.NET declaration

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

Upvotes: 3

Views: 8028

Answers (5)

MarkJ
MarkJ

Reputation: 30398

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.

Upvotes: 3

1800 INFORMATION
1800 INFORMATION

Reputation: 135245

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

Upvotes: 0

Grokys
Grokys

Reputation: 16526

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.

Upvotes: 4

Miroslav Zadravec
Miroslav Zadravec

Reputation: 3730

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

Upvotes: 1

Tim Jarvis
Tim Jarvis

Reputation: 18815

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 :-)

Upvotes: 3

Related Questions