Visual Basic 6.0到VB.NET声明

时间:2021-06-28 16:33:27

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

我如何在VB.NET中声明“as any”,或者什么是等效的?

5 个解决方案

#1


3  

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

VB.NET不支持任何关键字,VB.NET是强类型语言,但是你可以(使用.NET 3.5)在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 :-)

Dim fred =“Hello World”将隐式输入fred作为字符串变量。如果你想简单地保存一个你不知道设计时类型的值,那么你可以简单地将你的变量声明为对象(所有对象的母亲)注意,这通常是代码审阅者的红旗,所以请确保你有充分的理由准备好了:-)

#2


3  

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.

因为Any必须引用Windows API声明,因为它不能在变量声明中使用。您可以使用重载:只需为您希望传递的每种不同数据类型重复声明。 VB.NET选择与您在调用中传递的参数匹配的那个。

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

这比VB6中的As Any更好,因为编译器仍然可以进行类型检查。

#3


3  

The closest you can get is:

你最接近的是:

Dim var as Object

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.

它与VB6的Any(在Variant中存储值)不完全相同,但您可以将任何类型的变量存储为Object,尽管是盒装的。

#4


1  

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.

我想你在转换WinAPI声明方面遇到了问题。如果您只是将变量声明为字符串或整数,有时您可以逃脱,因为这是返回的实际值类型。

You can also try marshaling:

你也可以尝试封送:

<MarshalAsAttribute(UnmanagedType.AsAny)> ByRef buff As Object

#5


0  

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

VB.NET不支持“As Any”关键字。您需要明确指定类型。

#1


3  

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

VB.NET不支持任何关键字,VB.NET是强类型语言,但是你可以(使用.NET 3.5)在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 :-)

Dim fred =“Hello World”将隐式输入fred作为字符串变量。如果你想简单地保存一个你不知道设计时类型的值,那么你可以简单地将你的变量声明为对象(所有对象的母亲)注意,这通常是代码审阅者的红旗,所以请确保你有充分的理由准备好了:-)

#2


3  

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.

因为Any必须引用Windows API声明,因为它不能在变量声明中使用。您可以使用重载:只需为您希望传递的每种不同数据类型重复声明。 VB.NET选择与您在调用中传递的参数匹配的那个。

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

这比VB6中的As Any更好,因为编译器仍然可以进行类型检查。

#3


3  

The closest you can get is:

你最接近的是:

Dim var as Object

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.

它与VB6的Any(在Variant中存储值)不完全相同,但您可以将任何类型的变量存储为Object,尽管是盒装的。

#4


1  

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.

我想你在转换WinAPI声明方面遇到了问题。如果您只是将变量声明为字符串或整数,有时您可以逃脱,因为这是返回的实际值类型。

You can also try marshaling:

你也可以尝试封送:

<MarshalAsAttribute(UnmanagedType.AsAny)> ByRef buff As Object

#5


0  

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

VB.NET不支持“As Any”关键字。您需要明确指定类型。