Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Default Values for Structure 1

Status
Not open for further replies.

jalbao

Programmer
Nov 27, 2000
413
US
I have a Public Module that holds a Public Structure. Different forms within the solution can/will instantiate a new instance of the structure.

I would like to set default values for the structure at the same time I define the structure itself. For example, I would like to do something like:

Code:
Public Structure myStruct
Dim var1 As Int32 = 111
Dim var2 As Int32 = 222
Dim var3 As Date = DateTime.Now
End Structure

The problem is, when I set values for the vars (as shown above), I get squigglies (underneath the var) along w/ a popup message saying; "Initializers on structure members are valid only for constants."

I guess my question can be boiled down to this; What's the best way to set default values for a structure?

tia
 
Use the Const directive:
Code:
Public Structure myStruct
Const var1 As Int32 = 111
Const var2 As Int32 = 222
Const var3 As Date = DateTime.Now
End Structure
This may not work for all of your items. If not, you'll probably want to use a class instead of a structure.

Chip H.





____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks for your input Chip.

It appears as though I'll have to take the class route.

When I tried your suggestion, I get a sqiggly under "myStruct" - message says; "Structure 'myStruct' must contain at least one instance member variable or event declaration". I can get it to work if I add a dummy Dim to the Struct ...

Code:
Public Structure myStruct
Dim imDumb As String = ""
Const var1 As Int32 = 111
Const var2 As Int32 = 222
Const var3 As Date = DateTime.Now
End Structure

Using the dummy just doesn't feel right, so I guess I'll have to start over and implement the class.

thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top