Home All Groups Group Topic Archive Search About

Working with structures and the New keyword

Author
17 Jan 2006 5:30 PM
Terry Olsen
What is the proper way to initialize a variable as a structure?  It
seems to work for me either way, using the New keyword or not.

Private Structure FLASHWINFO
    Dim cbSize As Int32
    Dim hwnd As Int32
    Dim dwFlags As Int32
    Dim uCount As Int32
    Dim dwTimeout As Int32
End Structure

Dim FI As FLASHWINFO
     --OR--
Dim FI As New FLASHWINFO

Both seem to work fine.

*** Sent via Developersdex http://www.developersdex.com ***

Author
17 Jan 2006 6:12 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Terry Olsen" <tolse***@hotmail.com> schrieb:
> What is the proper way to initialize a variable as a structure?  It
> seems to work for me either way, using the New keyword or not.
>
> Private Structure FLASHWINFO
>    Dim cbSize As Int32
>    Dim hwnd As Int32
>    Dim dwFlags As Int32
>    Dim uCount As Int32
>    Dim dwTimeout As Int32
> End Structure
>
> Dim FI As FLASHWINFO
>     --OR--
> Dim FI As New FLASHWINFO

Both lines are semantically equivalent.  It's up to personal preference to
prefer one of the methods over the other.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
17 Jan 2006 7:38 PM
Chris Dunaway
If the structure actually defines a constructor, do you have to call
it?  e. g. :

Private Structure FLASHWINFO
    Dim cbSize As Int32
    Dim hwnd As Int32
    Dim dwFlags As Int32
    Dim uCount As Int32
    Dim dwTimeout As Int32

    Public Sub New()
        'some code here
    End Sub
End Structure

Does this still work and does it call the constructor?

Dim fwi As FLASHWINFO
Author
17 Jan 2006 7:45 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Chris Dunaway" <dunaw***@gmail.com> schrieb:
> If the structure actually defines a constructor, do you have to call
> it?  e. g. :
>
> Private Structure FLASHWINFO
>    Dim cbSize As Int32
>    Dim hwnd As Int32
>    Dim dwFlags As Int32
>    Dim uCount As Int32
>    Dim dwTimeout As Int32
>
>    Public Sub New()
>        'some code here
>    End Sub
> End Structure
>
> Does this still work and does it call the constructor?
>
> Dim fwi As FLASHWINFO

It's not supported to declare a parameterless non-shared constructor for a
structure type.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
17 Jan 2006 6:29 PM
Patrice
A structure is always allocated so using new or rather a matter of personal
preference (though it would be interesting to see if new erases the previous
members).

--
Patrice

Show quoteHide quote
"Terry Olsen" <tolse***@hotmail.com> a écrit dans le message de
news:uI82Iv4GGHA.1192@TK2MSFTNGP11.phx.gbl...
> What is the proper way to initialize a variable as a structure?  It
> seems to work for me either way, using the New keyword or not.
>
> Private Structure FLASHWINFO
>     Dim cbSize As Int32
>     Dim hwnd As Int32
>     Dim dwFlags As Int32
>     Dim uCount As Int32
>     Dim dwTimeout As Int32
> End Structure
>
> Dim FI As FLASHWINFO
>      --OR--
> Dim FI As New FLASHWINFO
>
> Both seem to work fine.
>
> *** Sent via Developersdex http://www.developersdex.com ***