Default value for structures

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I've got a problem, I have a structure named mStruc but when I create a variable of that type I need to initialize all of its members otherwise I get an exception System.NullReferenceException when I make reference to the variable (that's obvious)

Is there a way to set default values for the members of a structure so that I don't get an error before initializing it? All I could get in the help is that you can't do that directly in the structure but you can use this inside the structure..

Public Structure mStru
Dim mVal as intege
Sub New(ByVal Tval as integer
mVal = Tva
End Su
End Structur

How does it work? I mean, this sub new inside the structure....I tried this but I got the same, I’m a VB6 programmer so I’m very newbie in all about VB.NET, I think I'm missing something...or should I use a class instead??? As you can see, any comment will be very helpful :-

Thank
FO
 
* =?Utf-8?B?Rk9Q?= said:
I've got a problem, I have a structure named mStruc but when I create a variable of that type I need to initialize all of its members otherwise I get an exception System.NullReferenceException when I make reference to the variable (that's obvious).

Is there a way to set default values for the members of a structure so that I don't get an error before initializing it? All I could get in the help is that you can't do that directly in the structure but you can use this inside the structure...

Public Structure mStruc
Dim mVal as integer
Sub New(ByVal Tval as integer)
mVal = Tval
End Sub
End Structure

How does it work? I mean, this sub new inside the structure....I tried
this but I got the same, I’m a VB6 programmer so I’m very newbie in
all about VB.NET, I think I'm missing something...or should I use a
class instead??? As you can see, any comment will be very helpful :-)

\\\
Public Structure MyStruct
Public Sub New(ByVal a As Integer, ByVal b As Integer)
Me.a = a
Me.b = b
End Sub

Dim a As Integer
Dim b As Integer
End Structure
..
..
..
Dum MyData(19) As MyStruct
Dim i As Integer
For i = 0 to 19
MyData(i) = New MyData(1, 2)
Next i
///

The only problem is that you cannot define a parameterless ctor for the
structure, but you can add a dummy parameter or add a method called
'Initialize' that initializes the members. You will have to explicitly
call this method.
 
Back
Top