473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Default value for structures

FOP
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

Nov 20 '05 #1
1 12890
* =?Utf-8?B?Rk9Q?= <an*******@discussions.microsoft.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Migrators | last post by:
1)How is the memory structure organised in C. i.e., the way in which the stack and heap memory are used in C. 2) What will be the value of EOF.
29
by: John Wood | last post by:
Even though CSC does its best to detect use of unassigned variables, it often misses it... for example if you just declare a double in a class without assigning a default value, it has a default...
9
by: John | last post by:
If a value type is immutable, I guess it's threadsafe to read it? But not threadsafe to assign a new value to it (can any value type be truely immutable? Isn't assigning a totally new value to it,...
5
by: Hendrik Schober | last post by:
Hi, we just run into the problem, that "default" alignment in the project properies dialog seem to be different. We have a project that's a DLL, which is linked with a couple of LIBs. All are...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
11
by: prefersgolfing | last post by:
I'm trying to find on MSDN, or someplace, that speaks to variables being public or private by default. Anyone know where? Thanks.
1
by: shrini | last post by:
Hi, Im using a map implementation that has structures as both the key and it value. Ive used as below: typedef struct RadioMeasurmentType { enum measurementId; uint32 radioCardId; uint32...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
22
by: Nehil | last post by:
Does C follow call by value convention or call by reference? i see that there is nothing like reference in C standard but it is referenced. still, what should be the answer for the above...
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.