How to create dynamically name of object?

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

Hi everybody,
I'm a new in C# and have a problem

I need to create dynamically the name of object
let's say "obj1", "obj2" etc. They're hashtables (Action Script
objects)

I'm doing like
String tempStr = "obj" + i.ToString();
then
ASObject tempStr = new ASObject();
this gives an problem, beacuse temStr is already defined as a string
and recasting in this context is not possible.

Java and VB can easy resolve it, not speaking about scripting
languages were exists special functions like Eval(), but C# has some
different way.
If somebody knows something about it?


Thanks a lot!
 
Hi Peter,

I'm doing like
String tempStr = "obj" + i.ToString();
then
ASObject tempStr = new ASObject();
this gives an problem, beacuse temStr is already defined as a string
and recasting in this context is not possible.

I don't know if I understand your problem correctly but I think you simply
have a casting-problem.

//let's define a string
string tempStr = "I am a String";

//let's cast the string to an object
object tempObj = (object) tempStr;

//let's get our string again
string helloAgain = (string) tempObj;

VB can easy resolve it

Be careful, not with Option Strict On

Cheers

Arne Janning
 
Thank you very much, Arne!
Yes, we can look at it as on Casting but this doesn't work


string tempStr = "I am a String";
object tempObj = (object) tempStr;

It tells that re-casting in this context is not possible!

Thanks,
Peter
 
Peter said:
Thank you very much, Arne!
Yes, we can look at it as on Casting but this doesn't work

string tempStr = "I am a String";
object tempObj = (object) tempStr;

It tells that re-casting in this context is not possible!

Actually, the above code works fine, although the cast isn't required.

It doesn't do what you want it to, but it compiles and runs...
 
Hi Jon!

Actually, the above code works fine, although the cast isn't required.
ACK.

It doesn't do what you want it to, but it compiles and runs...

Well, what does he want to do?

Cheers

Arne Janning
 
He needs to create the name of the object dynamically..
some thing like..

for (int i=0; i<5; i++)
{
String tempStr = "obj" + i.ToString();
ASObject tempStr = new ASObject();
}

Which is not possible .. at least to my knowledge..

But I think he should be able to get a indirect route.. some thing like
Obj.Tag or some other variable to hold the indentity of the object.

for (int i=0; i<5; i++)
{
String tempStr = "obj" + i.ToString();
ASObject temp = new ASObject();
temp.Tag = tempStr;
}

Nirosh.
 
Arne Janning said:
Hi Jon!



Well, what does he want to do?

Define a variable with a name which is calculated at runtime. That just
doesn't work in a language like C#.
 
Hi Jon!

Define a variable with a name which is calculated at runtime. That just
doesn't work in a language like C#.

OK. Full ACK. Sorry, I didn't read it out of his post.

Cheers

Arne
 
First ignore my message in the other thread for your other message.
This thread clears things up.

What you would have to do it is use a Hashtable.

ListDictionary Objs = new ListDictionary();

for (int i=0; i<5; i++)
{
String tempStr = "obj" + i.ToString();
ASObject temp = new ASObject();
objs[tempStr] = temp;
}

Unfortunately, .Net doesn't offer a string-to-object hashtable class, just a
string-to-string, and an object-to-object. There is one string-to-object
abstract class (NameObjectCollectionBase), which includes direction to
derive a string-to-your own object class, which is probably what you want.

ALternately, if you know that names will follow the "objN" pattern, you
could just as well forgot of the "obj" part, and use an array:

ASObject[] objs = new ASObject[5];
for (int i=0; i<5; i++)
{
ASObject temp = new ASObject();
objs = temp;
}

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
Back
Top