Results 1 to 3 of 3

Thread: Assigning a value to a variable through variable name

  1. #1
    .NET Framework
    .NET 4.5
    Join Date
    Jul 2016
    Location
    Philippines
    Posts
    2
    Rep Power
    0

    Question Assigning a value to a variable through variable name

    Hi! I am trying to create my own ORM. Database fields will be the same name as the class variables of the model. Now, I searched for a way to get column name in SqlDataReader in C#. In my code, "ListAllVars()" lists the variables in C#:

    Code:
        public interface IBaseModel
        {
            void ListAllVars();
        }
    
        public class BaseModel : IBaseModel
        {
            protected string tableName = "old table name";
            protected string timestamp = "some stamp";
            
            public void ListAllVars()
            {
                BindingFlags bindingFlags = BindingFlags.Public |
                                BindingFlags.NonPublic |
                                BindingFlags.Instance |
                                BindingFlags.Static;
    
                foreach (FieldInfo field in this.GetType().GetFields(bindingFlags))
                {
                    Console.WriteLine(field.Name);
                }
            }
        }
    
        public interface IModel : IBaseModel
        {
            int IntegerMember { get; set; }
            string StringMember { get; set; }
            string ModelName { get; set; }
    
            void PrintHello();
        }
    
        public class Model : BaseModel, IModel
        {
            private int integerMember;
            private string stringMember;
            private string modelName;
            private string tableName = "new table name";
    
            public int IntegerMember
            {
                get => this.integerMember;
                set => this.integerMember = value;
            }
    
            public string StringMember
            {
                get => this.stringMember;
                set => this.stringMember = value;
            }
    
            public string ModelName
            {
                get => this.modelName;
                set => this.modelName = value;
            }
        }
    Now that I have variable names of the class and column names of a table, I want to assign a value to a variable in a class through the column name (which is also a string). Like so:

    Code:
    int userID = 0;
    string fieldName = "userID";
    string fieldValue = "18";
    
    _some_function_or_method(fieldValue).value(Convert.ToInt32(fieldValue));
    Console.WriteLine("User ID value: " + userID);
    // outputs "User ID value: 18"
    Is there any way to achieve it?

  2. #2
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2011
    Posts
    1,786
    Rep Power
    137

  3. #3
    .NET Framework
    .NET 4.5
    Join Date
    Aug 2017
    Location
    Cambridge, UK
    Posts
    2
    Rep Power
    0

    Use reflection: use GetField and SetValue methods

    You can use reflection.

    The following code should point you in the right direction.

    Code:
        public class User
        {
            public int UserId;
        }
    
    
        public class Program
        {
            public static void Main()
            {
                var user = new User();
    
    
                SetIntField(user, "UserId", "18");
    
    
                Console.WriteLine(user.UserId); // Print 18
            }
    
    
            public static void SetIntField<T>(T obj, string fieldName, string fieldValue)
            {
                obj.GetType().GetField(fieldName).SetValue(obj, Convert.ToInt32(fieldValue));
            }
        }

Similar Threads

  1. Question Accessing variable value from another class?
    By Dragnorian in forum C# General Discussion
    Replies: 5
    Last Post: 08-15-2017, 11:26 PM
  2. Use of unassigned local variable?
    By JungleHeart in forum C# General Discussion
    Replies: 3
    Last Post: 03-20-2017, 11:02 AM
  3. Question Session Variable
    By Knutsford in forum Windows Forms
    Replies: 2
    Last Post: 01-17-2017, 1:15 PM
  4. Used of Unassigned local variable
    By dn54321 in forum Console Application
    Replies: 5
    Last Post: 03-29-2015, 4:39 AM
  5. Question Cannot assign value to local variable...
    By bookimal in forum C# General Discussion
    Replies: 2
    Last Post: 09-26-2014, 3:49 AM

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
  •