Is there any difference between Integer and Int32 in VB.NET?
Asked Answered
N

1

23

In VB.NET, is there any difference between Integer and Int32?

If yes, please explain.

Nobe answered 8/3, 2013 at 6:4 Comment(2)
@MitchWheat...Google gave me confusing answers for this. So, thought of asking over here. I know how to search.Nobe
OK, Int32 and Integer are different. Which would you recommend using in projects where there is no pre-existing standard - and why? Presumably this thought was behind @Vikram's question.Prearrange
R
34

Functionally, there is no difference between the types Integer and System.Int32. In VB.NET Integer is just an alias for the System.Int32 type.

The identifiers Int32 and Integer are not completely equal though. Integer is always an alias for System.Int32 and is understood by the compiler. Int32 though is not special cased in the compiler and goes through normal name resolution like any other type. So it's possible for Int32 to bind to a different type in certain cases. This is very rare though; no one should be defining their own Int32 type.

Here is a concrete repro which demonstrates the difference.

Class Int32

End Class

Module Module1
    Sub Main()
        Dim local1 As Integer = Nothing
        Dim local2 As Int32 = Nothing
        local1 = local2 ' Error!!! 
    End Sub
End Module

In this case local1 and local2 are actually different types, because Int32 binds to the user defined type over System.Int32.

Rowdyish answered 8/3, 2013 at 6:9 Comment(15)
@JaredPar, Thanks for explanation and thanks a lot for not putting some comment like "your google broken?" or something. :)Nobe
@MitchWheat i googled the question and didn't find a suitable explanation. Most links just say "no difference" which is incorrect. There is one answer on the msdn website which had a decent explanation but it was fairly far down in my google resultsRowdyish
There is no difference. Your answer states "Fuctionally there is no difference between the types"Glassware
I agree with JaredPar. That was the same reason I asked the question over here. I always believe that SO is the platform I can trust...of course, one has to face some rude comments! But, I ignore for gaining knowledge.Nobe
@MitchWheat there is a difference in how the compiler interprets the names. This is an often overlooked difference and can bite people (especially in generated code). You see the same effect in C# with int and Int32Rowdyish
When was the last time you saw Int32 not equal to System.Int32?Glassware
@MitchWheat I used to work on the .Net compiler team. I've seen this bug pop up several times (in both C# and VB.Net). As I said, people run into this in generated code or in certain nested type situations. It's not a fictional issueRowdyish
That's cool. Glad I stuck around to see this answer. I'm surprised you can do that without brackets i.e. Class [Int32]. ThanksTunicate
@DanVerdolino that's very much related. Int32 isn't a keyword in Vb.Net although many believe it to be. It's just another name that's interpreted like anything elseRowdyish
@Jared: you might want to have a word with one of your colleagues! : social.msdn.microsoft.com/forums/en-US/vblanguage/thread/…Glassware
@Mitch that's the post I originally saw when I Googled. SO prevails.Tunicate
@MitchWheat yes that answer is incorrect. Those forums are apparently closed and I cannot offer another answer. I will sent Matt an email.Rowdyish
Bit of a contrived example. If people are defining there own Int32 they reap what they sow.Glassware
@MitchWheat it's a contrived example to concretely demonstrate the problem. As I said, the most common way this comes up is in generated code.Rowdyish
So if the question was "Is there a difference between Integer and System.Int32, then the answer would be no. Generated code should specify the namespace to prevent this problem from ever happening.Tami

© 2022 - 2024 — McMap. All rights reserved.