-2

I have an ASP.NET Web App with the back end written in C#. In the C# code I have a constant as follows:

const string MAX_NAME_LENGTH = "20";

I then a have user control named TextInput with an attribute named maxLength. I would like to use it in HTML as follows:

<MyTagPrefix:TextInputFL maxLength = "MAX_NAME_LENGTH " runat="server"/>

Please note that I would like to use the C# constant's name ("MAX_NAME_LENGTH") in the HTML and somehow convert "MAX_NAME_LENGTH" into the assigned value ("20") in the maxLength property set clause:

public string maxLength
    {
        set
        {
           // Code to convert the provide string value of the C# constant's
           // name (in this case "MAX_NAME_LENGTH") into the constant's 
           // value ("20").
        }
     }

Does anyone have any ideas on how to convert the the C# constant's name into is value?

user2797166
  • 45
  • 1
  • 9
  • Is there a particular reason you want to solve it in this convoluted, roundabout way? Why not just assign the **value** of the `const` (rather than the **name** of the `const`)? – mjwills Aug 02 '18 at 23:23
  • It's generally a bad idea to use the actual name of a variable for functionality, but if you insist you may want to check out this [SO post](https://stackoverflow.com/questions/72121/finding-the-variable-name-passed-to-a-function) – agillgilla Aug 02 '18 at 23:24

2 Answers2

0

First of all, as @mjwills said in his comment, it makes sense to just use the value of the const variable instead of the name in your tag.. but assuming for some reason you don't want to do that you could consider changing your variable to a key value pair

using System.Collections.Generic;

Dictionary<string, int> constants = new Dictionary<String, int>(){ {"MAX_NAME_LENGTH", 20} };

And then you can access the value via the key

public string maxLength
{
    set
    {
       // Code to convert the provide string value of the C# constant's
       // name (in this case "MAX_NAME_LENGTH") into the constant's 
       // value ("20").
         value = constants["MAX_NAME_LENGTH"]
    }
 }

There are ways to do what you're looking for but they aren't reliable as far as I know

mjwills
  • 23,389
  • 6
  • 40
  • 63
  • I'm trying to make my stuff easier to maintain by using constants. I have several cases where I use the same value several times. Your solution looks good. If you have any other suggestions, in light of my response, I'm all hears. Thanks. – user2797166 Aug 03 '18 at 05:00
0

Here's retrieval. Not sure how you would use this to initialize a control, but whatever.

using System;
using System.Reflection;

class Constants
{
    public const string MIN_NAME_LENGTH = "10";
    public const string MAX_NAME_LENGTH = "20";

    public static string GetValue(string index)
    {
        return typeof(Constants)
            .GetField(index, BindingFlags.Public | BindingFlags.Static)
            .GetValue(null) as string;
    }
}

public class Program
{
    public static void Main()
    {
        Console.WriteLine(Constants.GetValue("MIN_NAME_LENGTH"));
        Console.WriteLine(Constants.GetValue("MAX_NAME_LENGTH"));
    }
}

Output:

10
20

Example on DotNetFiddle

John Wu
  • 50,556
  • 8
  • 44
  • 80