|
-
June 3rd, 2008, 06:02 AM
#1
plz help
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
Code:
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
Last edited by ujjwalmeshram; June 3rd, 2008 at 06:40 AM.
Reason: correction
-
June 3rd, 2008, 06:57 AM
#2
Re: plz help
What exactly would you like to do? Describe it in word, the sample doesn't make sense.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
June 3rd, 2008, 07:09 AM
#3
Re: object naming
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.
-
June 3rd, 2008, 07:24 AM
#4
Re: object naming
Sorrry for creating confusion.
Let me try again
suppose we are creating an object named 'obj1'
Code:
object obj1 = new object();
but instead of providing object name(obj1) directly, we provide a string containing 'obj1' as its data, like
Code:
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
-
June 3rd, 2008, 07:29 AM
#5
Re: object naming
 Originally Posted by ujjwalmeshram
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.
-
June 3rd, 2008, 09:40 AM
#6
Re: object naming
You can't do this. C# isn't a scripting language. The best you can hope for is to use a dictionary e.g.
Code:
using System.Collections.Generic;
void testmethod()
{
Dictionary<string, object> objects = new Dictionary<string, object>();
objects["obj1"] = new Object();
// ....
object obj1 = objects["obj1"];
}
Darwen.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|