Variable Name in C#...

  • Thread starter Thread starter PL
  • Start date Start date
P

PL

I want to dynamicaly create variable and assigned it a particular name, for
exemple:

foreach (FileInfo FI in ArrayOfFiles)
{
string FI.Name = "AValueWhoDoesntMatter"
}

Of course, this sample doesn't works...

How can I set the string variable name to always be the value of the
property Name of the object FI?

Thank you

PL
 
Hi PL,

I don't know if there is a language that supports that.At least the
compiled lanuages such as C# doesn't..

Firstly, the names of the vairables does matter only at compile time. Once
the program is compiled there is no names. This is not completely true for
platforms like .NET because those platforms support reflection so the names
have to be preserved.

Secondly you cannot add variables at run time. Every variable you use has to
be decalared and the name has to be specified upon its declaration.

Something similar of what you need (as far as understand) can be done in
C/C++ using macros, but C# doesn't support macros.

So you cannot do that in C#
 
Stoitcho,

You were correct with your first statement. Variables do not retain
their names in the compiled code. Parameters and field names do need to
retain their names (for reflection), but not variables.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Stoitcho Goutsev (100) said:
Hi PL,

I don't know if there is a language that supports that.At least the
compiled lanuages such as C# doesn't..

Firstly, the names of the vairables does matter only at compile time. Once
the program is compiled there is no names. This is not completely true for
platforms like .NET because those platforms support reflection so the
names have to be preserved.

Secondly you cannot add variables at run time. Every variable you use has
to be decalared and the name has to be specified upon its declaration.

Something similar of what you need (as far as understand) can be done in
C/C++ using macros, but C# doesn't support macros.

So you cannot do that in C#

--
HTH
Stoitcho Goutsev (100) [C# MVP]


PL said:
I want to dynamicaly create variable and assigned it a particular name,
for
exemple:

foreach (FileInfo FI in ArrayOfFiles)
{
string FI.Name = "AValueWhoDoesntMatter"
}

Of course, this sample doesn't works...

How can I set the string variable name to always be the value of the
property Name of the object FI?

Thank you

PL
 
PL said:
I want to dynamicaly create variable and assigned it a particular name, for
exemple:

C# is not a scripting language like JavaScript. This kind of thing is
generally not done in compiled languages. You can use the Reflection
API to generate new classes with new members at run-time, but it's
rather complex and not really going to solve the problem I suspect
you're having.

The effect you want to acheive can be done using a number of the
Collection classes in C#. For example, you can use a
NameVauleCollection in this manner:

NameValueCollection files = new NameValueCollection();

foreach (FileInfo FI in ArrayOfFiles)
{
files.Add(FI.Name, "AValueWhoDoesntMatter")
}

string fileName = files[FI.Name];


--Mike
 
Nicholas Paldino said:
You were correct with your first statement. Variables do not retain
their names in the compiled code. Parameters and field names do need to
retain their names (for reflection), but not variables.

Well, I'd classify parameters and fields as variables too, but I assume
you meant local variables.

In that case, the name of the variable *is* maintained in debug mode,
but not in release mode.
 
Jon said:
Well, I'd classify parameters and fields as variables too, but I assume
you meant local variables.

In that case, the name of the variable *is* maintained in debug mode,
but not in release mode.

Aren't the symbol names actually stored in the PDB file, external to the
assembly itself?

--Mike
 
Jon,
Well, I'd classify parameters and fields as variables too, but I assume
you meant local variables.

In that case, the name of the variable *is* maintained in debug mode,
but not in release mode.

The names of the variables' (parameters and fields, but not local variables)
are kept in the assembly meta data for reflection purposes. Yes, I wasn't
clear enough.
However, in the method bodies (IL code) there are no fields' and paramters'
names.

As far as debug mode is concerned, the names of the local varibales are not
part of the assembly itlself. They are in separate file that the debuger
uses.
 
As far as debug mode is concerned, the names of the local varibales are not
part of the assembly itlself. They are in separate file that the debuger
uses.

Absolutely right. I hadn't noticed that ildasm takes note of the pdb...

(Thanks to Mike for pointing this out too.)
 
Back
Top