Results 1 to 7 of 7

Thread: How to Convert String to Variable Name in C#?

  1. #1
    Join Date
    Jul 2006
    Posts
    128

    How to Convert String to Variable Name in C#?

    Does anyone know how to Convert String to Variable Name in C#? Since I am trying to do lot of things, but not getting the desired result, so thought to post here. I want to convert string to the name of an object or variable such as in the code below
    Code:
    string VariableName = "Hello";
    string Convert.ToObj(VariableName) = "Made from VariableName string"
    Is it possible to convert this,.?? Please help me as soon as possible.!@!
    "Every man is guilty of all the good he did not do". - Voltaire

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: How to Convert String to Variable Name in C#?

    I would like to suggest you that instead of converting string to variable name, it will be much easy if you create an ArrayList with the type of your Objects. I am giving you the code and hope that you will find better than using the conversion in C#.
    Code:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;
    
    class Program {
        static void Main(string[] args) {
            ArrayList cricketTeams = new ArrayList();
            cricketTeams.Add("s");
            cricketTeams.Add("h");
            cricketTeams.Add("i");
    
            foreach (string item in cricketTeams) {
                Console.Write(item + "\n");
            }
    
        }
    }

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to Convert String to Variable Name in C#?

    According to me you will have to create a class RenderObject or NamedObject that inherits your Object. After creating the mentioned class you will have to add a name field to it. So that you can use these objects to add to your array. Or else if you don't have an object, you could make an ArrayList of strings. Also try to change the function to accept strings. Maybe by changing the function to accept strings can solve your problem.

  4. #4
    Join Date
    Nov 2008
    Posts
    996

    Re: How to Convert String to Variable Name in Python?

    I know the Python, the code provided by me will work on Python to convert String to Variable Name. Here is the code for that :

    Code:
    L=['a','b']
    V=[4,5]
    for var in zip(L,V):
    exec "%s=%s" % (var[0], var[1])
    a
    4
    b
    5
    V=[6,7]
    dd = dict(zip(L,V))
    dd
    {'a': 6, 'b': 7}
    globals().update(dd)
    a
    6
    b
    7

  5. #5
    Join Date
    Mar 2008
    Posts
    349

    Re: How to Convert String to Variable Name in C#.Net?

    Code:
    public new Dictionary<string, string> Attributes { get; set; }
    public string StringAttributes = string.Empty;
    
    public int? MaxLength { get; set; }
    public int? Size { get; set; }
    public int? Width { get; set; }
    public int? Height { get; set; }
    
    protected override void OnInit(EventArgs e) {
        Attributes = new Dictionary<string, string>();
        Attributes.Add("MaxLength", MaxLength.ToString());
        Attributes.Add("Size", Size.ToString());
        Attributes.Add("Width", Width.ToString());
        Attributes.Add("Height", Height.ToString());
        base.OnInit(e);
    }
    
    protected override void OnPreRender(EventArgs e) {
        if (Attributes != null) {
            StringBuilder attributes = new StringBuilder();
            foreach (var item in Attributes) {
                if (!string.IsNullOrWhiteSpace(item.Value)) {
                    attributes.Append(item.Key + "=\"" + item.Value + "\" ");
                }
            }
            StringAttributes = attributes.ToString();
        }
    }
    The above code can be used for converting the String to Variable Name in C#.Net.Hope that you will get some useful points from this coding.

  6. #6
    Join Date
    Mar 2008
    Posts
    672

    Re: How to Convert String to Variable Name in C#?

    I am not able to understand your query properly.!! I think that a variable name technically is a string. Can you explain exactly what you want to do or can you provide any relevant code samples.?? Do you want to say
    Code:
    MyClass class1 = new MyClass();
    MessageBox.Show(class1.ToString());
    I am not understanding where you want to show "class1" instead of the Type of class as a string representable format?

  7. #7
    Join Date
    Apr 2010
    Posts
    1

    Re: How to Convert String to Variable Name in C#?

    Quote Originally Posted by Allan.d View Post
    I am not able to understand your query properly.!! I think that a variable name technically is a string. Can you explain exactly what you want to do or can you provide any relevant code samples.?? Do you want to say
    Code:
    MyClass class1 = new MyClass();
    MessageBox.Show(class1.ToString());
    I am not understanding where you want to show "class1" instead of the Type of class as a string representable format?
    What he wants (and what I need too) is a way to make a string a variable name.

    Example:

    Code:
    string Anything = "myCheckBox";
    
    //then use Anything to behave as myCheckBox.
    
    Anything.Checked = true;

    @Warner: Hello, I don't understand how to use your code. Could you give an example?

Similar Threads

  1. Convert string into int in C
    By screwball in forum Software Development
    Replies: 4
    Last Post: 22-12-2011, 08:47 PM
  2. Assign a text file from hard disk into a VBA string variable
    By AbiCgail in forum Software Development
    Replies: 5
    Last Post: 15-07-2011, 10:49 AM
  3. Convert XML string into DOM
    By GreatThinker in forum Software Development
    Replies: 6
    Last Post: 22-07-2010, 09:48 AM
  4. How to input variable in string.find method in C++
    By Caelaan in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 10:50 PM
  5. How to convert string to int
    By Zavier in forum Software Development
    Replies: 3
    Last Post: 04-06-2009, 06:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,573,883.40562 seconds with 16 queries