Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
I have class "Vars" in folder in my project like MyProject.Models.Vars.
and this class contains some static variable

class Vars
{
        public static string var1= "value1";
        public static string var2= "value2";
        public static string var3= "value3";
        public static string var4= "value4";
}


In another folder MyProject.Forms.Mis i have form form1 that contains textbox. when the user write in this textbox the var name ex:- var1
how to show its value (value1)

What I have tried:

i searched but i don't know what i can search about.

there are any solution to combine the class path "MyProject.Models.Vars" with the var name "Vars" and show its value
Posted
Updated 27-Jul-20 6:51am

using System;
using System.Collections.Generic;

namespace MyProject
{
    public class Models
    {
        public static class Vars
        {
            public static readonly Dictionary<string, string> VarToValue = new Dictionary<string, string>
            {
                {"var1", "value1"},
                {"var2", "value2"},
                {"var3", "value3"},
                {"var4", "value4"}
            };

            public static string GetValueFromVar(string svar)
            {
                if (VarToValue.ContainsKey(svar))
                {
                    return VarToValue[svar];
                }

                throw new ArgumentException($"{svar} is not a valid  key");
            }
        }
    }
}
The value of any variable in 'Vars can now be accessed from anywhere in the 'MyProject namespace:

string test1 = MyProject.Models.Vars.GetValueFromVar("var1");
 
Share this answer
 
v2
Comments
Golden Basim 26-Jul-20 11:35am    
yes this in case i know what is the variable that the user will call it. the user will write just variable name in textbox.text = var1
BillWoodruff 26-Jul-20 23:17pm    
see revised answer
Golden Basim 27-Jul-20 9:25am    
Yes, I thought about that before but this variables already was called in many other locations. but still your solution is the best for new projects. thanks
Richard Deeming 27-Jul-20 12:46pm    
A dictionary of delegates would work for your existing code.
Golden Basim 27-Jul-20 13:13pm    
this class contains 4000 line maybe 80-100 var, var name is Query name, var value is Query Text ex:-
// get Shortages count
public static string getItems_Shortages_Count_QUERY = @"
CREATE PROCEDURE `getItemsShortagesCount`(in searchVar varchar(200))
BEGIN
Select * from st_items s, st_plug_manufacturer m where stitems_Name LIKE CONCAT('%', searchVar , '%') or
s.stitems_Pharma_ActiveIngredient LIKE CONCAT('%', searchVar , '%') or
m.manu_Name LIKE CONCAT('%', searchVar , '%') or
s.stitems_Code =searchVar;
END ";
A variation on Bill's answer, which will work with your existing fields and should still be faster than reflection:
C#
static class Vars
{
    public static string var1= "value1";
    public static string var2= "value2";
    public static string var3= "value3";
    public static string var4= "value4";
    
    private static readonly IReadOnlyDictionary<string, Func<string>> Map = new Dictionary<string, Func<string>>(StringComparer.OrdinalIgnoreCase)
    {
        [nameof(var1)] = () => var1,
        [nameof(var2)] = () => var2,
        [nameof(var3)] = () => var3,
        [nameof(var4)] = () => var4,
    };
    
    public static string GetValueFromVar(string varName)
    {
        if (string.IsNullOrEmpty(varName)) throw new ArgumentNullException(nameof(varName));
        if (!Map.TryGetValue(varName, out var fn)) throw new ArgumentException($"'{varName}' is not a valid key.", nameof(varName));
        return fn();
    }
}
 
Share this answer
 
i used reflection

FieldInfo fld = typeof(Vars).GetField("var1");
string val = fld.GetValue(null).ToString();
 
Share this answer
 
Comments
BillWoodruff 26-Jul-20 0:40am    
why are you using such expensive (reflection) methods when there are simple solutions ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900