Re: How to initialize a variable as a array structure / structure (type) array (see example)
- From: "Bill McCarthy" <Bill@xxxxxxxxxxxxx>
- Date: Thu, 11 Dec 2008 14:54:09 +1100
hi Sarah,
"Sarah M. Weinberger" <mweinberger@xxxxxxxxxxx> wrote in message news:%234HGobxWJHA.5064@xxxxxxxxxxxxxxxxxxxxxxx
Hi,
Yes, I know of that way, but is there no compact nice and elegant way, such as what I described in the C example that would do that? I know that in VB.Net there is a way.
VB.Net:
Dim MyVariable = new MYTYPE(,,) = {{"a", 1, 1}, {"b", 2, 2}, {"c", 3, 3}}
That's definitely not VB code. (.NET or otherwise) . In VB10 there is array initializer syntax that is close (some of which is in VB9), but nothing that doesn't require the type of each item to be specified. eg:
Dim MyVariable() as MYTYPE = { New MYTYPE("a",1), New MYTYPE("b",2), New MYTYPE("c",3)}
That of course would require a parameterized constructor. you could also use object initializers there, but it does get wieldy
Dim MyVariable() as MYTYPE = { New MYTYPE() With {.Name = "a", .itype = 1}, New MYTYPE() With {.Name ="b", .itype = 2)}
Again in VB10 there are list initializer, where you could write an Add extension that would bring the syntax to something like:
Dim MyList As new List(Of MYTYPE) From {("a", 1), ("b", 2), ("c", 3)}
Picking apart what you have : new MYTYPE(,,) would either be looking for a parameterised constructor with 3 optional parameters or you are trying to define a 3 dimensional array. The {{"a", 1, 1}, {"b", 2, 2}, {"c", 3, 3}} can be used to assign to a two dimensional arrya of type Object, which isn't what you are after.
I tried the VB.Net approach in VB6, but I got a complaint on the "new", plus I already know that VB6 does not like braces, so it seems that there is no nice compact and elegant way to populate UDT (User Defined Types) in VB6. VB6 implements UDTs (structures, really) to a basic level, but not all the way. I did a search on Google and came up empty.
Honestly there isn't in VB.NET either really. It would also have complained over the code you posted ;)
Am I right that there is no nice clean elegant way as C and VB.Net has?
There's nothing as terse as C in VB. VB10 does make it a bit easier but it will still be a runtime creation and assignment of each item, really not that different from the VB6 code solutions posted, at least as far as functionality does. It won't be compiled as a block to be blitted into memory.
.
- Follow-Ups:
- References:
- VB6: How to initialize a variable as a array structure / structure (type) array (see example)
- From: Sarah M. Weinberger
- Re: How to initialize a variable as a array structure / structure (type) array (see example)
- From: Henning
- Re: How to initialize a variable as a array structure / structure (type) array (see example)
- From: Sarah M. Weinberger
- VB6: How to initialize a variable as a array structure / structure (type) array (see example)
- Prev by Date: Re: Problem converting string to number: "Type mismatch" error
- Next by Date: Re: Problem converting string to number: "Type mismatch" error
- Previous by thread: Re: How to initialize a variable as a array structure / structure (type) array (see example)
- Next by thread: Re: How to initialize a variable as a array structure / structure (type) array (see example)
- Index(es):
Relevant Pages
|