使用“Dim”的VB.NET和類型推斷

[英]VB.NET and type inference using “Dim”


I'm coming from a C# background and I really like the type inference that C# 3.0 has. I'm trying to do similar things in VB.NET (some of which appear possible), but in some cases the compiler seems to be not nearly as good at inferring the type.

我來自C#背景,我非常喜歡C#3.0的類型推斷。我試圖在VB.NET中做類似的事情(其中一些似乎是可能的),但在某些情況下,編譯器似乎在推斷類型方面似乎不太好。

For example, I have a method that returns an object of type System.Guid. In C# I'd do this and the variable 'prop' would be of type Guid through inference.

例如,我有一個返回System.Guid類型的對象的方法。在C#中我會這樣做,變量'prop'將通過推理變為Guid類型。

var prop = RegisterProperty<Guid>(...);

However, if I do a similar thing in VB.NET:

但是,如果我在VB.NET中做類似的事情:

Dim prop = RegisterProperty(Of Guid(...)

I get prop as type System.Object. I've played with some of the VB.NET project settings but the only thing it changes is whether I get a warning that the object is of type Object when I use it later as a Guid.

我得到支持類型System.Object。我已經玩了一些VB.NET項目設置,但它唯一改變的是我在以后作為Guid使用它時是否收到對象類型為Object的警告。

Any ideas? I'm thinking the use of generics should allow the compiler to tell beyond a doubt what type prop should be.

有任何想法嗎?我認為使用泛型應該允許編譯器毫無疑問地告訴我應該使用什么類型的prop。


@J Cooper: ok, I did have that setting turned on, but I just re-read the documentation for that compiler option and it reads "Specifies whether to allow local type inference in variable declarations". I believe the reason it's not working for me is that I'm declaring static fields in my class. I guess even though they are initialized when declared, the compiler doesn't support type inference at that point. Bummer.

@J Cooper:好的,我確實打開了那個設置,但我只是重新閱讀了該編譯器選項的文檔,並且它顯示“指定是否允許在變量聲明中進行本地類型推斷”。我相信它不適合我的原因是我在課堂上宣布靜態字段。我猜即使它們在聲明時被初始化,編譯器也不支持那時的類型推斷。游民。

3 个解决方案

#1


1  

You mention below that you're trying to do this in a static variable declaration. That won't work in C#, and will give you Object in VB.NET (for instance and statics).

您在下面提到您正在嘗試在靜態變量聲明中執行此操作。這在C#中不起作用,並且會在VB.NET中為您提供Object(例如靜態)。

#2


2  

I'm assuming by "played with the VB.NET project settings" you mean you already did this: Type Inference in VB.NET

我假設“使用VB.NET項目設置”,你的意思是你已經這樣做了:在VB.NET中輸入類型推斷

If not, may help

如果沒有,可能會有幫助

#3


1  

If VB is infering the type as Object, you need to turn Option Strict On at the top of your class file at least. Once you do that, you should see that class fields do not support type inferencing. The following raises the compiler error "Option Strict On requires all variable declarations to have an As clause".

如果VB將類型推斷為Object,則至少需要在類文件的頂部打開Option Strict On。執行此操作后,您應該看到類字段不支持類型推斷。以下引發了編譯器錯誤“Option Strict On要求所有變量聲明都有一個As子句”。

Public Shared foo = "Test"

In general, you should use Dim inside of methods and Public/Private/Friend on the class/module level fields.

通常,您應該在方法和類/模塊級別字段中使用方法中的Dim和Public / Private / Friend。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2008/12/04/b33ff4402ed9c6d8a7852d5c8df290d0.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com