Click to See Complete Forum and Search --> : object naming
ujjwalmeshram
June 3rd, 2008, 06:02 AM
Hi all
I m using .net 2.0
I want to create an object of type object
but the problem is my object name has to be taken from string variable
Something like this
string bb = "obj1";
object bb.ToString() = new object();
This code creates an error
I dont know how to do this.
plz help me
thank u
boudino
June 3rd, 2008, 06:57 AM
What exactly would you like to do? Describe it in word, the sample doesn't make sense.
thedevkid
June 3rd, 2008, 07:09 AM
Why not just add the object to an ArrayList or an array...sounds like you want a dynamic variable name? Would be curious on why you would need that...I don't think you can...I know in PHP you can do something like that but not in .NET...atleast that I know of.
ujjwalmeshram
June 3rd, 2008, 07:24 AM
Sorrry for creating confusion.
Let me try again
suppose we are creating an object named 'obj1'
object obj1 = new object();
but instead of providing object name(obj1) directly, we provide a string containing 'obj1' as its data, like
string objname = "obj1";
//now create object
object objname.ToString() = new object();
since string objname contains 'obj1', the object should be created with the name 'obj1'
if string objname contains data 'obj2',the object should be created with the name 'obj2'
hope this will enough info.
thank u
Shuja Ali
June 3rd, 2008, 07:29 AM
since string objname contains 'obj1', the object should be created with the name 'obj1'
if string objname contains data 'obj2',the object should be created with the name 'obj2'This does not make much sense. Why would you do that? If you could share some more insight as to what exactly are you trying to achieve with this type of code, probably some one here might give a better solution.
darwen
June 3rd, 2008, 09:40 AM
You can't do this. C# isn't a scripting language. The best you can hope for is to use a dictionary e.g.
using System.Collections.Generic;
void testmethod()
{
Dictionary<string, object> objects = new Dictionary<string, object>();
objects["obj1"] = new Object();
// ....
object obj1 = objects["obj1"];
}
Darwen.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.