473,395 Members | 2,446 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,395 software developers and data experts.

How to create dynamically name of object?

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!
Nov 16 '05 #1
8 1958
Hi Peter,

"Peter" schrieb
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
Nov 16 '05 #2
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
Nov 16 '05 #3
Peter <Pe***@discussions.microsoft.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Hi Jon!

"Jon Skeet [C# MVP]" schrieb
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
Nov 16 '05 #5
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" <sp*****************@msn.com> wrote in message
news:ex**************@tk2msftngp13.phx.gbl...
Hi Jon!

"Jon Skeet [C# MVP]" schrieb
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

Nov 16 '05 #6
Arne Janning <sp*****************@msn.com> wrote:
Hi Jon!

"Jon Skeet [C# MVP]" schrieb
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?


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Hi Jon!

"Jon Skeet [C# MVP]" schrieb
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
Nov 16 '05 #8
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[i] = 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!)
"Peter" <pk******@hotmail.com> wrote in message
news:ea**************************@posting.google.c om...
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!

Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
2
by: Sean | last post by:
Hello all, I may just not be searching for the right thing, but I've been looking for a way to dynamically create controls (and name them) in my code so that I can create only the controls I...
4
by: Ray | last post by:
I want to dynamically load DLLs (created from VB) and instantiate a class with a particular name, like "ProcessClass". I am able to load the DLL and confirm there is a class by that name BUT I...
4
by: vertigo | last post by:
Hello I need to create some objects durring program execution - but it's names are dynamically generated (depends on parameters). How can i do it ? Thanx Michal
7
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
10
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item...
7
by: Rob | last post by:
This actually compiles and works but it doesn't seem like the best code, so I was wondering is there another way to do this? template <typename Tvector<T>* addDepth(T) { return new vector<T>;...
5
by: akonsu | last post by:
hello, i need to add properties to instances dynamically during run time. this is because their names are determined by the database contents. so far i found a way to add methods on demand: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.