Working with structures and the New keyword

T

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.
 
H

Herfried K. Wagner [MVP]

Terry Olsen said:
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.
 
P

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).
 
C

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
 
H

Herfried K. Wagner [MVP]

Chris Dunaway said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top