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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic instance declaration 1

Status
Not open for further replies.

Bygbobbo

Programmer
Apr 23, 2002
145
US
how can I declare the name of a vector instance dynamically?

I am passing an array of Strings to a function, each element of the String array needs to create a vector.

for(int i; i < array.size(); i++){
//Vector array.toString() = new Vector;
Vector ???? = new Vector();
}

I am looking for the correct syntax. I know mine is not correct.

thanks in advance,
Bygs


 
You cannot create code level variables dynamically in a compiled language. Some scripting languages have something like it, for instance Javascript has eval().

The closest thing to it would be something like a map of string to object for looking the object up by some string key value.
Code:
Hashtable mapofobjs = new Hashtable();
mapofobjs.put( &quot;one&quot;, new myobject());
mapofobjs.put( &quot;two&quot;, new myobject());

-pete
 
Bygbobbo, let me rephrase what you are trying to say and correct me if I'm wrong... What you want to do is to create object names dynamically... not the objects but object variable names!!! As far as I know this is not possible, I could be wrong.

lets say that you have the following array of strings

String vectorNames[3] = {books, cds, mags};

And you want this to happen in the for loop,

vector books = new vector();
vector cds = new vector();
vector mags = new vector();

This is not possible because object/variable names must be decided at compile time. However, the type of objects could be decided at runtime which is a polymorphic effect called &quot;late binding&quot; or &quot;dynamic binding&quot;. If you find a technique to do what you are trying to do then do let us know.

Furqan

YEH GHAZI YEH TERAY PUR-ASRAR BANDAY
 
Thanks for your input Furgan..

And yes that is what Im trying to do.

Well, I hate limitations and always try to break the boundary. I think it's funny how an OOP will not let you dynamically generate the object's name.

The way around it for now is to put the name in the vector's first element. Then add the data, when all is done put all the Vector's into one Vector.

I was trying to avoid unneccessary overhead while keeping my new invention Decoupled.

There has to be a way to do this though. Almost like the eval function in Javascript.

thanks,

Bygs
 
>>>> Well, I hate limitations and always try to break the boundary. I think it's funny how an OOP will not let you dynamically generate the object's name.


Its not a limitation, its a feature. The reason you can't do it in the same way that you can in some 'scripting' languages is that it protects against programmer error resulting in Runtime errors. A similar case is the problem of casting objects at runtime. This will be addressed in release 1.5, when you must declare explicitly what kind of object another object like ArrayList holds. Some may call this a limitation, but if you don't know what object you should be recieving, then your code is dangerous.

Similarly, if your code needs to rely on what an object is named, then you are not programming properly !
 
My apologies sedj,

I wasn't aware that you were the expert. Do you even know what MCD is?

Well, when you figure out what it means then you will understand my question!!!!

thanks,
Bygs

:)...It's people like you that make me look good without even trying...:)

 
Fair play, point made. Apologies for sarcastic last sentence.

However, I do stand by the bulk of my point, that it is a feature, not a limitation. Care to enter a more adult discussion ? We may both get something out of it.

 
Absolutely..

In the meantime I created an array of Vectors, with the first element of the Vector labeled by the naming convention I needed to make my new class decoupled..

It's a little more overhead than I wanted, but will do for now.

Bygs
 
>> how can I declare the name of a vector instance dynamically?

Is that first question you asked representative of what you are actually trying to accomplish, or has the question now changed somehow?

If it is accurate then the post I originally provided is still the answer. Something exactly like that is how a scripting engine like javascript would handle that situation. When you declare a var in javascript it does not become a compiled symbol but instead is tracked by the engine at runtime. Using something similar to the java.util.Hashmap would be that way that is handled.

This situation has nothing to do with the eval() function in javascript which evaluates entire expressions. The eval() function is not a huge deal for a scripting engine since it must interpret the code at runtime anyway it just reuses the same mechanism for eval() that it uses on the entire script to begin with.


-pete
 
Hi Bygbobbo

Do you really want to declare the name of a vector dynamically.
This is hardly the objective.

I recently had a similar problem, building a vector of vectors dynamically.
The objective was to create a Vector of row data, poplated by vectors of column data for each row in a database resultset
first I populate a vector, with columns of a result set.
Then I populate the Rows vector with a clone of the columns vector
Then I clear the Columns vector, and reuse it
See this code,
Vector thisRow = new Vector();
Vector allRows = new Vector();
int row;

// looping through the resultset
while (myResults.next()== true){

// on this row loop through the columns to pick up the data values
for(int i = 1; i<= numberOfColumns;i++) {
// add the column element as a string , for display purposes
thisRow.addElement((Object)myResults.getString(i));
//System.out.println (thisRow.toString());

} // for

// add this row to rows
allRows .addElement(thisRow.clone());

// the clear this row so we can populate it again
thisRow.clear();

} // while we are looping through the resultset

I was quite pleased that it worked, and I didnt have to name any of the dynamically created vectors.

Hope this helps
 
Your a day late and a dollar short.

palbano gave me what I needed, he's got the star for this one thanks anyway..

Bygs
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top