Results 1 to 7 of 7

Thread: C# interpret string as variable name

  1. #1
    Join Date
    Aug 2005
    Location
    UK
    Beans
    570
    Distro
    Ubuntu 7.04 Feisty Fawn

    C# interpret string as variable name

    Hey all,

    New language for me this C#, have to use it for my course at Uni. Anyway I have come to a situation where the perfect solution would be if I could interpret a string as a variable name.

    Example

    Code:
    int myInteger = 10;
    string varName = "myInteger";
    
    int returnFunc(string varName)
    {
        return <whatever variable has a name the same as the value in varName>
    }
    Any ideas? It would be great if this is actually possible.

    Thanks

    Ironfistchamp
    Check out Poweriser Pages!

    The best extreme sport. Well I think so anyway.

  2. #2
    Join Date
    Aug 2006
    Location
    Canada
    Beans
    154
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: C# interpret string as variable name

    I'm not sure where this kind of approach would ever be considered a perfect solution, but it is possible. I quickly coded up the following example of how to do something like this.

    Code:
    using System;
    
    namespace Test
    {
    	class MainClass
    	{
    		public int myInteger = 7;
    		
    		public void Test()
    		{
                           // Grabs the integer by the variables name
    			int test = (int)this.GetType().GetField("myInteger").GetValue(this);
    			
    			Console.WriteLine(test);
    		}
    		
    		public static void Main(string[] args)
    		{
    			MainClass mc = new MainClass();
    			
    			mc.Test();
    		}
    	}
    }

  3. #3
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: C# interpret string as variable name

    Quote Originally Posted by ironfistchamp View Post
    I have come to a situation where the perfect solution would be if I could interpret a string as a variable name.
    Sorry, I don't know C# so I have no idea how to do it (if it's even possible), but may I suggest that you have a design problem here?


    More info about your real goal may help to sort it out...
    Not even tinfoil can save us now...

  4. #4
    Join Date
    Aug 2006
    Location
    Canada
    Beans
    154
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: C# interpret string as variable name

    Quote Originally Posted by aks44 View Post
    Sorry, I don't know C# so I have no idea how to do it (if it's even possible), but may I suggest that you have a design problem here?


    More info about your real goal may help to sort it out...
    I would have to agree here, I can't see a situation where it would be preferable to access a variable in this way. If you tell us what you are trying to do we might be able to recommend you a better way to solve the problem at hand.

  5. #5
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: C# interpret string as variable name

    IMO the solution you're really looking for is a key-value map, if you really need to do something like this... does C#'s API come with hashmaps or treemaps like Java's?
    LambdaGrok. | #ubuntu-programming on FreeNode

  6. #6
    Join Date
    Apr 2006
    Location
    Phoenix, AZ
    Beans
    251
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: C# interpret string as variable name

    Quote Originally Posted by CptPicard View Post
    IMO the solution you're really looking for is a key-value map, if you really need to do something like this... does C#'s API come with hashmaps or treemaps like Java's?
    Code:
    using System.Collections.Generic;
    
    public class SomeClass
    {
    private Dictionary<string, int> list = new Dictionary<string, int>();
    
            public int GetValue(string varName)
            {
                return list[varName];
            }
    
            public void SetValue(string varName, int value)
            {
                list.Add( varName, value );
            }
    }
    -Skeeterbug

  7. #7
    Join Date
    Aug 2005
    Location
    UK
    Beans
    570
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: C# interpret string as variable name

    Ah it's OK now. Talked with a programmer friend of mine and he sorted me right out. Thanks for the replies.

    Ironfistchamp
    Check out Poweriser Pages!

    The best extreme sport. Well I think so anyway.

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
  •