473,224 Members | 1,903 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,224 software developers and data experts.

initializing array without knowing it's type

If i have an array of a certain type, is there away of initializing
with without knowing it's type? for example (forgive me if some of
this is syntactically incorrect)

if i have a procedure like this

private sub addElements( target() as Object , elemsToAdd () as object )
if ( arr is Nothing ) then
{initialize with correct type}
end if

Array.resize( target, elemsToAdd.Length)
'add elements to target() array
end sub

And from my main routine i'd call it like this:

private dim arr1() as Type1 = nothing;
private dim arr2() as Type2 = nothing;

private dim arrElems1() as Type1 =....
private dim arrElems2() as Type2 =....

addElements( arr1, arrElems1)
addElements( arr2, arrElems2)

As you can see in the scenario above, if both arr1 and arr2 are
Nothing, id like to initialize them in the sub "addElements". How can
i do that?

Nov 21 '05 #1
3 1265
hmm, looks like VB has the ReDim reserved word. Is that what i need

Nov 21 '05 #2
>As you can see in the scenario above, if both arr1 and arr2 are
Nothing, id like to initialize them in the sub "addElements". How can
i do that?

You can't do it based on the "target" parameter. A Nothing reference
doesn't carry any type information so that fact that "arr1" is of type
Type1() in the calling code is lost inside the addElements method.

You could do it by using type information from "elemsToAdd". For

target =
DirectCast(Array.CreateInstance(elemsToAdd.GetType ().GetElementType(),
elemsToAdd.Length), Object())


target = DirectCast(Array.CreateInstance(elemsToAdd(0).GetT ype(),
elemsToAdd.Length), Object())

But even then the change to "target" wouldn't be reflected back to the
caller since you pass it in ByVal. You have to make it ByRef for the
new array reference to be passed back to the caller.

Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #3
perfect, thanks. if elemsToAdd were declared as an ICollection, i
assume that getting the type of any element contained within would work
just as well, correct?

Nov 21 '05 #4

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

Similar topics

by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
by: questions? | last post by:
say I have a structure which have an array inside. e.g. struct random_struct{ char name; int month; } if the array is not intialized by me, in a sense after I allocated a
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until runtime. I have a class that looks like this: ...
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
by: Richard Heathfield | last post by:
candide said: They aren't. An array is an array. An address is a pointer value. These are not the same thing. If you mean that &array and &array are the same, they aren't. They have different...
by: Fred Mellender | last post by:
I am trying to use reflection to output the fields (names and values) of an arbitrary object -- an object dump to a TreeView. It works pretty well, but I am having trouble with generic lists,...
by: efittery | last post by:
I need to modify the following code to handle a vector of pairs of floats. I was just thinking of casting my vector of floats into being a vector of pairs of floats. Alternately, I have...
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.