Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7653205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:58:18+00:00 2026-05-31T11:58:18+00:00

so if the string name of a variable is passed into a method, I

  • 0

so if the string name of a variable is passed into a method, I know which variable to use.

in the following example, the area I need help with is PrintVar(string)…turning the string argument into the variable…so that it prints out “here be variable 1” and “here be variable 2” respectively…Thanks!


class ReflectionTest
{    
    class MyObj
    {
        private string myvar;

        public MyObj(string input)
        {   this.myvar = input; }

        public override string ToString()
        {   return ("here be " + myvar);    }   
    }

    class MyClass
    {
        private MyObj var1;
        private MyObj var2;

        public MyClass()
        {   
            var1 = new MyObj("variable 1");
            var2 = new MyObj("variable 2");
        }

        public void PrintVar(string theVariable)
        { 
            Console.WriteLine(theVariable); 
        }   
    }


    static void Main()
    {
        MyClass test = new MyClass();
        test.PrintVar("var1");
        test.PrintVar("var2");
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:58:20+00:00Added an answer on May 31, 2026 at 11:58 am

    If you need to fetch things by name, then personally I’d start by using a dictionary in the internal implementation, i.e.

    private readonly Dictionary<string,MyObj> fields =new Dictionary<string,MyObj>();
    
    public MyClass()
    {
        fields["var1"] = new MyObj("variable 1");
        fields["var2"] = new MyObj("variable 2");
    }
    public void PrintVar(string fieldName)
    {
        Console.WriteLine(fields[fieldName]);
    }
    

    The other option would be reflection (GetType().GetFields()):

    public void PrintVar(string fieldName)
    {
        Console.WriteLine(GetType().GetField(fieldName,
            BindingFlags.NonPublic | BindingFlags.Instance).GetValue(this));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In XS, how do I turn a string that holds a variable name into
For example, I defined a method: def hello(String name) { println(Hello + name) }
I want to add a string to a variable name that represents an integer.
I have a string passed into a function, I would like to compare the
Possible Duplicates: PHP: Get name of variable passed as argument How to get a
I have a string variable in C# aka product_name, I want to filter like
I have 4 string variables name, quest, favorite_color, speed that might be empty. I
Suppose I have string Name and Image Photo as properties of a class in
Here's the code: string name = myName; int id = (int)_myDB.ThingTable.Where(thing => thing.ThingName ==
I have one string name as id in my .aspx.cs page and one JavaScript

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.