473,396 Members | 1,895 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,396 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 )
begin
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 1272
hmm, looks like VB has the ReDim reserved word. Is that what i need
here?

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
example

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

or

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

--
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

50
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...
13
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
30
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
4
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: ...
17
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...
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
37
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...
2
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,...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.