none
Get the value of a variable by its name RRS feed

  • Question

  • Hi folks,

    I am trying to translate some old dBase code into C# and I have came upon a situation that Im not sure how to handle.

    Here is the dbase code:

    strNo_   = "*******"
    strNo_0  = "**ZERO*"
    strNo_1  = "***ONE*"
    strNo_2  = "***TWO*"
    strNo_3  = "*THREE*"
    strNo_4  = "**FOUR*"
    strNo_5  = "**FIVE*"
    strNo_6  = "***SIX*"
    strNo_7  = "*SEVEN*"
    strNo_8  = "*EIGHT*"
    strNo_9  = "**NINE*"
             
    c = "strNo_" + substr(chequeAmount, 1, 1) // c = "strNo_1"
    d = &c                                    // d = "***ONE*"

    Basically, c will end up being a string (one from the variable names above). But the & converts it to the VALUE of the variable.

    Can this be done in C# ?

    J


    jppnn

    Tuesday, August 27, 2019 10:09 PM

Answers

  • Greetings John.

    It's possible someone might post here with a clever and convoluted solution involving reflection, but generally the answer to your question is no, it can't be done in c#. The way to handle that sort of situation is by using an array (or List, or some other collection that has an index).

             string[] strNo = new string[10];
    
             strNo[0] = "**ZERO*";
             strNo[1] = "***ONE*";
             strNo[2] = "***TWO*";
             strNo[3] = "*THREE*";
             strNo[4] = "**FOUR*";
             strNo[5] = "**FIVE*";
             strNo[6] = "***SIX*";
             strNo[7] = "*SEVEN*";
             strNo[8] = "*EIGHT*";
             strNo[9] = "**NINE*";
    
             // TryParse is considered safer, but if you're certain a string is numeric, Convert will do the trick.
             string d = strNo[Convert.ToInt32(substr(chequeAmount, 1, 1))];


    • Edited by Ante Meridian Tuesday, August 27, 2019 11:33 PM missed a bit.
    • Marked as answer by john pp nn Wednesday, August 28, 2019 2:19 PM
    Tuesday, August 27, 2019 11:31 PM

All replies

  • Greetings John.

    It's possible someone might post here with a clever and convoluted solution involving reflection, but generally the answer to your question is no, it can't be done in c#. The way to handle that sort of situation is by using an array (or List, or some other collection that has an index).

             string[] strNo = new string[10];
    
             strNo[0] = "**ZERO*";
             strNo[1] = "***ONE*";
             strNo[2] = "***TWO*";
             strNo[3] = "*THREE*";
             strNo[4] = "**FOUR*";
             strNo[5] = "**FIVE*";
             strNo[6] = "***SIX*";
             strNo[7] = "*SEVEN*";
             strNo[8] = "*EIGHT*";
             strNo[9] = "**NINE*";
    
             // TryParse is considered safer, but if you're certain a string is numeric, Convert will do the trick.
             string d = strNo[Convert.ToInt32(substr(chequeAmount, 1, 1))];


    • Edited by Ante Meridian Tuesday, August 27, 2019 11:33 PM missed a bit.
    • Marked as answer by john pp nn Wednesday, August 28, 2019 2:19 PM
    Tuesday, August 27, 2019 11:31 PM
  • Hi john,

    Thank you for posting here.

    Since this thread is related to dBase, it is a third-party product. We do not support it.

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 28, 2019 6:46 AM
    Moderator