CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: object naming

  1. #1
    Join Date
    Mar 2007
    Posts
    77

    Question 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

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  3. #3
    Join Date
    Aug 2007
    Posts
    4

    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.

  4. #4
    Join Date
    Mar 2007
    Posts
    77

    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

  5. #5
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: object naming

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

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured