How to create dynamically name of object?

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

Arne Janning

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
 
G

Guest

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
 
J

Jon Skeet [C# MVP]

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

Arne Janning

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
 
C

Champika Nirosh

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

Jon Skeet [C# MVP]

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#.
 
A

Arne Janning

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
 
J

James Curran

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top