-2

Please note:- I already have gone few links answered but they are leading to controls only. I can use objects easily to access into objects, no issues about that. Issue is with variables accessing at runtime. One is directing to variable, but I found it very tough and rigid one. So want to know any simple and easy way.

I have a requirement following:-

Scenario for e.g.:-

 1. TextBox_mobile is a control object in aspx page
 2. mobile is a variable stored in c# .cs file
 3. I have a 135+ such controls in aspx page and want to store them in variables in .cs file on say submit button.
 4. I have stored in a table having two fields control_objects and against it variable names
 5. So when submitt is fired (click event) I want to run the assigning task with running a process to retriew control names and
variables names and assinging the values of control objects to appropriate variables.

For e.g. to more going in practical:-

// create a variable to store textbox control
string mobile = "" 
// Text_mobile is a TextBox in aspx page
// A temporary variable to store variable in loop from table 
string var_mobile = "mobile"; // Currently I am hard coding
// now I wish to use this var_mobile to make automatically assign the value into main variable
<<var_mobile>> = TextBox_mobile.Text.Trim();
//and backend it should be actually storing same in earlier mobile variabl
mobile = TextBox_mobile.Text.Trim();

Since I have lot of objects and variables and later to work on variables, I want this to do in a loop like logic at a time instead of assigning them at individual basis.

Is there any such possible in Asp.net C#?

Yeshwant Mudholkar
  • 107
  • 1
  • 2
  • 10
  • Does this answer your question? [Get a Windows Forms control by name in C#](https://stackoverflow.com/questions/1536739/get-a-windows-forms-control-by-name-in-c-sharp) –  Apr 23 '20 at 10:03
  • @ElmoDev001 Its only for control objects. I do not have issue on that. I want to access variables remotely and assign values from appropriate control objects. – Yeshwant Mudholkar Apr 23 '20 at 10:09

2 Answers2

1

In c# this and all things which require code to interact with data about types and their members declared within assemblies can be achieved using reflection.

To get field using string containing its name you can use method GetField and then call SetValue on FieldInfo returned by GetField.

For example, if you have class named MyClass declared like this

class MyClass
{
    public int myField = 0;
}

You could use following code to set value of myField

MyClass myClass = new MyClass();
string fieldName = "myField";
int valueToSet = 10;

typeof(MyClass).GetField(fieldName).SetValue(myClass, valueToSet);
Kacper
  • 520
  • 5
  • 20
  • This worked superb. You exactly found what I needed. Really appreciate it. I would place my code for more details, so that it can help many others. – Yeshwant Mudholkar Apr 23 '20 at 10:28
  • I'm glad to hear that ;) If that's it then you can accept my answer as the answer to your question – Kacper Apr 23 '20 at 10:30
0

As per Kacper prompt quick answer, I was able to resolve with his point to point resolution.

CommonClass.cs

public class CommonClass
{
     public string mobile = "";
     public CommonClass()
     {
         //
         // TODO: Add constructor logic here
         //
      }
}

My code in .cs file:-

    string mobile = "";

    CommonClass obj_class = new CommonClass();
    string fieldname = "mobile";
    typeof(CommonClass).GetField(fieldname).SetValue(obj_class, TextBox_mobile.Text);

    mobile = obj_class.mobile.Trim();
Yeshwant Mudholkar
  • 107
  • 1
  • 2
  • 10