Build up a variable name with a string

Hi,

I wonder if there's a way (in c#) to build up a variable name
by the concatenation of two strings...For example

int intTest = 1;

string strTest = "strTest";

string strTest+intTest.toString() = "this is my new var";

Thanks in advance




Answer this question

Build up a variable name with a string

  • Stefan Wagner

    Have you heard of a Hashtable or Dictionary<> Use it to store key(variable name) and value (value of variable) pairs. This would be better I think. C# can't do what you want except for maybe with reflection or emit.
  • kittykat80

    What does Array have to do with the subject

    How exactly is



    colName = "Column"+ALLTRIM(STR(ii))
    &colName = somevalue

    significantly different from



    Column(II) = somevalue

    (where Column() is an Array)



  • UncleSam89

    I don't think you can make a variable name based on what you enter in a string. I could be wrong but never seen this done before and when you try to do it like you have done, compiler will of course complain.



  • Hassan Ayoub

    It occured to me to take a look at the way my grid with 16 columns is defined in the constructor as well as how my table with equal number of columns or a tabControl with 24 tabpages are defined. With macro substitutions I could have compressed a few hundred statements down to perhaps a dozen. They all have some regularity in them. Those laws are easily noticeable. You set up a few macros in a loop and just add a few characters as loop rolls. That's it. These dozen or so macros are used only once during the compilation and at runtime the OS deals with its few hundred statements.

  • MMS2006

    James Curran wrote:

    Modern programming languages have a thing called an Array. Perhaps you should look into it......

    What does Array have to do with the subject



  • parosky

    >> Or in VFP: via macro substitutions-a very nice feature.

    Well.. that's debatable. I saw them a lot years ago when I was coding in Clipper (which, like FoxPro originally, was a dBase III compiler). I've never seen a good design that used them, but I've seen a lot of bad designs that did. They seem to lead to the "quick shortcut" mentality (*) that just makes mess out of code. Further, it forces the compiler to shutdown and revert back to being an interpreter (which means that your EXE must include the full interpreter with it)

    (*) An example: I would often see code like this:


    MyVar = "World"
    MyVar2 = "Hello &MyVar"

    as opposed to the equivalent (and fully compilable) version:


    MyVar = "World"
    MyVar2 = "Hello " + MyVar



  • h3mp

    >> Let's say you have a grid with 150 columns and you want to address them all by name "Column1" "Column2" etc. The macro substitutions are very handy for doing it. You define a macro in a loop: colName = "Column"+ALLTRIM(STR(ii)) where ii is the loop counter <<

    Modern programming languages have a thing called an Array. Perhaps you should look into it......

    (I've not idea how VFP handles it, but when I was working in Clipper -- late 1980s! --- creating a variable like, without previously declaring it, made a mess out of memory management, which in those days of 640K DOS, was deadly)



  • Treasa

    You can't do this because C# is a compiled language. Dynamic operations like that can be handled in interpreted languages, such as Javascript (which can do it using the eval method).

    Can you use an array or some kind of collection to achieve the result you want



  • Tanny

    Its not possible but there must be many alternates. Would you like to explain your scenerio where you need this

    Best Regards,



  • Dark Helmet

    Mark Rendle wrote:

    operations like that can be handled in interpreted languages, such as Javascript (which can do it using the eval method).

    Or in VFP: via macro substitutions-a very nice feature.



  • RichardMInSC

    Well, if the "world" is a variable as opposed to MyVar which is a string then it makes perfect sense. After you applied &MyVar like this:

    &MyVar = "Hello "

    you should get:

    world == "Hello "

    the world becomes a string with content: "Hello "

    It makes a lot of sense when the names of variables are not known in advance of too tedious to define all of them. Let's say you have a grid with 150 columns and you want to address them all by name "Column1" "Column2" etc. The macro substitutions are very handy for doing it. You define a macro in a loop: colName = "Column"+ALLTRIM(STR(ii)) where ii is the loop counter and you have your column name ready: &colName.

    There are many other situations where the macros of VFP save a lot of code space. I suspect they may be equivalent to some inline functions in C# but I am not entirely sure: I am green in C# myself.



  • splashup

    James Curran wrote:

    What does Array have to do with the subject

    (where Column() is an Array)

    I am really sorry for a misstatement. The macros are not used in the constructor in VFP but you can use them to significantly simplify the constructor as I will show on C# example. I want you, James, to figure out how I can do it with Arrays in here. It will be very helpful for me.

    I glued up 24 statements from my InitializeComponent () Form1 function.

    this.tp_A = new System.Windows.Forms.TabPage ( );
    this.tp_C = new System.Windows.Forms.TabPage ( );
    ..........................
    this.tp_Y = new System.Windows.Forms.TabPage ( );
    this.tp_Z = new System.Windows.Forms.TabPage ( );

    There are as I said 24 tabs in that tabPage2 with a letter on each tab to select a subset of individuals from a customer file. You click on "C" and people with names starting with a C will be shown in a listBox, James Curran including:)

    If I had a VFP way to do things in here I would remove all 24 lines from this procedure and a number of others and stored these lines in Form1.Load method:

    for ( int alpha = 65; alpha <= 90; alpha++ )
    {
    string macro1 = "this.tp_" + alpha.ToString ( ).Trim ( ) + " = new System.Windows.Forms.TabPage ( );";
    &macro1; // OF COURSE THIS IS NOT ALLOWED. IT IS A FANTASY C#
    }

    Instead of 24 statements I would have just 3.

    OK

    Thanks



  • altamash

    KyleB wrote:
    Have you heard of a Hashtable or Dictionary<> Use it to store key(variable name) and value (value of variable) pairs. This would be better I think. C# can't do what you want except for maybe with reflection or emit.

    How can you use a HashTable in a constructor You can use it at runtime but not to build a series of objects with some common features.



  • Kanagal Raj

    If you're doing the same thing to 16 columns, you just create a function which does it and call it sixteen times. If your grid exposes a collection of columns, you foreach through it calling the function. Yes, there are occasions where macros might enable a shortcut or two, but not enough (IMO) to outweigh the negative aspects like confuddled compiler messages and nightmarish debugging issues. After all, we apparently spend more time debugging than coding...

  • Build up a variable name with a string