Results 1 to 12 of 12

Thread: [RESOLVED] Freaky thing with Point type.

  1. #1

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Resolved [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 ?
    Last edited by Niya; Mar 11th, 2013 at 10:32 PM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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
    Last edited by Joacim Andersson; Mar 11th, 2013 at 10:59 PM.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    Code:
    Dim p As New Point
    but that doesn't actually invoke a constructor that does anything. It's functionally equivalent to this:
    Code:
    Dim p As Point
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    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
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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:
    Code:
    Dim i As Integer
    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#:
    Code:
    int i = null;
    Do you also know why it's valid in VB but not in C#? (The question is rhetorical.)

  6. #6

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: [RESOLVED] Freaky thing with Point type.

    Quote Originally Posted by Joacim Andersson View Post
    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#:
    Code:
    int i = null;
    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 lol
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7

    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.

  8. #8

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    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.
    Last edited by Niya; Apr 4th, 2013 at 08:42 PM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9

    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.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Freaky thing with Point type.

    This is from the MSDN documentation for the Nothing keyword:
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.
    Last edited by Joacim Andersson; Apr 5th, 2013 at 12:59 AM.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Freaky thing with Point type.

    Quote Originally Posted by Joacim Andersson View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width