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
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
Build up a variable name with a string
hrubesh
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.
Wout
How exactly is
colName = "Column"+ALLTRIM(STR(ii))
&colName = somevalue
significantly different from
Column(II) = somevalue
(where Column() is an Array)
Sudheer George
Nandagopal
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.
Tony Fabian
Larry30813217
Or in VFP: via macro substitutions-a very nice feature.
SarahJ
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
AksaiGora
Its not possible but there must be many alternates. Would you like to explain your scenerio where you need this
Best Regards,
kfrost
>> 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)
lavez
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 ( );";
¯o1; // OF COURSE THIS IS NOT ALLOWED. IT IS A FANTASY C#
}
Instead of 24 statements I would have just 3.
OK
Thanks
Sudhakar.hcitek
>> 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
saguaro31
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.
TheresaKad
What does Array have to do with the subject
Fred Herring