[RESOLVED] Freaky thing with Point type.
I've just noticed after all my time in VB.Net that the Point structure has a constructor that takes no parameters. The really freaky thing is that such a thing is supposed to be illegal. VB.Net doesn't allow you to define structures with a parameterless constructor. The only way to get a structure to accept a constructor with no parameters is not define one which makes it impossible to define if you have other constructors that take parameters and the Point structure does.
Anyone has any idea how this is possible ?
Re: Freaky thing with Point type.
The Point structure, just like any other structure in .Net, does not have a default parameterless constructor at all. The Point structure has 3 different constructors all of which accepts parameters. If a structure was allowed to have a default parameterless constructor it would imply that it will always be called during initialization but the CLR makes no such promise.
This can look a bit confusing since the following is fully allowed:
Code:
Dim p As Point = New Point()
However no constructor call is actually made when you do the above. The members of the structure are simply set to their default values, 0 for numeric types, Nothing for reference types and so on. This is important since structures are value types and all of its members are initialized to their default value without a constructor call. If a default constructor was allowed it always have to be called during initialization and how effective would then the following code be?
Code:
Dim pArray(1000) As Point
The constructor has to be called 1001 times just to initialize the array.
So the following two lines does the same thing, they do not call any constructor but rather initialize the members with their default value.
Code:
Dim p1 As Point
Dim p2 As Point = New Point()
The following however will call on one of the constructors:
Code:
Dim p As Point = New Point(5, 12)
I must always be able to use a value type even if I haven't called upon any constructor, this code is valid:
Code:
Dim p As Point
p.X = 5
p.Y = 12
Re: Freaky thing with Point type.
The Point structure does not have a parameterless constructor. Check the documentation and you'll see three constructors: one with one Integer parameter, one with two Integer parameters and one with one Size parameter. In code it is legal to do this:but that doesn't actually invoke a constructor that does anything. It's functionally equivalent to this:I'm fairly sure that that first syntax was originally illegal in VB.NET but they may have allowed it because the second syntax can cause warnings if you use the variable before assigning a value to it.
Re: Freaky thing with Point type.
Thank you both. I get it now. I almost never define structures of my own and I've always used the ones in the Framework with their "parametered" constructors. Never really stopped to notice that oddity. Thx;)
Re: [RESOLVED] Freaky thing with Point type.
Just to make this complete; this is also legal code:
Code:
Dim i As New Integer
'or
Dim i As Integer = New Integer
Both does the exact same thing as this:Everybody know that an Integer is, just like a structure, a value type in .Net. In fact the Integer is a structure (or rather the framework System.Int32 is a structure while Integer is a VB keyword for that structure).
BTW, did you know that this is also valid in VB:
Code:
Dim i As Integer = Nothing
While the following is not valid in C#:Do you also know why it's valid in VB but not in C#? (The question is rhetorical.)
Re: [RESOLVED] Freaky thing with Point type.
Quote:
Originally Posted by
Joacim Andersson
BTW, did you know that this is also valid in VB:
Code:
Dim i As Integer = Nothing
While the following is
not valid in C#:
Do you also know why it's valid in VB but not in C#? (The question is rhetorical.)
Now I feel real dumb :( That fact that the question is rhetorical implies that I should know the answer but I must confess I really don't. I knew you could do it in VB. I experimented with that before but I had no clue C# prohibited it. Imma throw a fit if the answer is really something I should have known :mad: lol
Re: [RESOLVED] Freaky thing with Point type.
It's a value type; you can't assign null to value types in C# ;)
I believe that's the correct answer.
Re: [RESOLVED] Freaky thing with Point type.
Yes I know its a value type but the question is why does VB allow it and C# doesn't. Nothing evaluates to Object and even value types inherit from Object. I'm thinking this might have something to do with the difference. null in C# is probably a different animal internally from Nothing in VB.
Re: [RESOLVED] Freaky thing with Point type.
Null implies the absence of a value I believe.
EDIT: Besides, if you want to assign a default value to a value type (or any time, for that matter), C# has default(T), not sure if VB has an equivalent.
Re: [RESOLVED] Freaky thing with Point type.
This is from the MSDN documentation for the Nothing keyword:
Quote:
For non-nullable value types, Nothing in Visual Basic differs from null in C#. In Visual Basic, if you set a variable of a non-nullable value type to Nothing, the variable is set to the default value for its declared type. In C#, if you assign a variable of a non-nullable value type to null, a compile-time error occurs.
Basically, 'null' in C# specifically means a null reference while Nothing in VB means the default value for the type, which is a null reference for reference types and something else for value types.
Quote:
Originally Posted by formlesstree4
Besides, if you want to assign a default value to a value type (or any time, for that matter), C# has default(T), not sure if VB has an equivalent.
default(T) was added to C# when generics were added because C# had no existing way to create a default value for an arbitrary type. VB already did, i.e. Nothing, so there was no need to add anything new.
Re: [RESOLVED] Freaky thing with Point type.
Points go to jmcilhinney for the correct answer, and also for explaining it the same way I would have done. :)
We are often told that Nothing in VB is the same as null in C#. This however isn't technically correct unless you talk about reference types in which case it happens to be correct. Nothing is not the same as null. Nothing really means "give me my default value" which happens to be null for reference types.
Re: [RESOLVED] Freaky thing with Point type.
Quote:
Originally Posted by
Joacim Andersson
Points go to jmcilhinney for the correct answer, and also for explaining it the same way I would have done. :)
We always agree on everything. ;)