Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the value of a class member variable from its name as a string

Is there anyway to do this? Its like i have a class

public class TestClass
{
   public static string myString= "123";
}

I want to some how get the value 123 from myString. If i know the name myString and put it into a textbox1 then annother textbox2 shows me the value 123.

Tried something like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var textForUrl = TestClass.GetType().GetField(txtbox1.Text).GetValue(TestClass, null)

But not working, also, my variables are all located in a seperate .cs file.

>Solution :

To get field public static string myString = "123";

var textForUrl = typeof(TestClass).GetField("myString")?.GetValue(null);

To get property public static string { get; set; } myString = "123";

var textForUrl = typeof(TestClass).GetProperty("myString")?.GetValue(null);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading