471,343 Members | 1,280 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,343 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 1197
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

50 posts views Thread by Dan Perl | last post: by
13 posts views Thread by simondex | last post: by
30 posts views Thread by questions? | last post: by
17 posts views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by
13 posts views Thread by WaterWalk | last post: by
2 posts views Thread by Fred Mellender | last post: by
reply views Thread by rosydwin | last post: by

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.