newbie question on dynamic variable creation

S

SharpieNewbie

i'm a newbie in C#. Was in programming many years ago with dBase/
Foxpro/Clipper and some VB.

Perhaps my thinking is still stuck in that kind of mindset..

here's something i'm stuck at..

via the
main(string args), and assuming that the argument passed is ABCD,

how can i create another string variable called ABCD?

is that doable ?
 
D

DDD

i'm a newbie in C#. Was in programming many years ago with dBase/
Foxpro/Clipper and some VB.

Perhaps my thinking is still stuck in that kind of mindset..

here's something i'm stuck at..

via the
main(string args), and assuming that the argument passed is ABCD,

how can i create another string variable called ABCD?

is that doable ?

You just need to declare a string ABCD in your main scope, no matter
what the parameter value is.
 
J

Jon Skeet [C# MVP]

SharpieNewbie said:
i'm a newbie in C#. Was in programming many years ago with dBase/
Foxpro/Clipper and some VB.

Perhaps my thinking is still stuck in that kind of mindset..

here's something i'm stuck at..

via the
main(string args), and assuming that the argument passed is ABCD,

how can i create another string variable called ABCD?

is that doable ?

No. Variable names need to be known at compile-time. Now, take a step
back - what are you actually trying to achieve? Why does the name of
the variable matter to you?
 
J

Jeroen

via the main(string args), and assuming that the
argument passed is ABCD, how can i create
another string variable called ABCD?

is that doable ?

Most likely you want arguments passed into a program to become the
_value_ of a variable, and not the _name_ of a variable. For example,
if your first argument is the name of a client, you might have some
code like this:

string clientName = args[0];
System.Out.WriteLine("Client's name you entered is: " + clientName);

This would generate for your example some output:

Client's name you entered is: ABCD

If you still feel the need to have 'ABCD' where my example has
'clientName', reply with an example with some more meaning than
'ABCD'.

Regards,
Jeroen
 
D

DSK Chakravarthy

Dude,

First thing first.. Never ever get confused with Variable names with the
INPUT from the User.

Second thing, Compare apples to apples but not oranges.

Hope you are clear, if not. Forget about the input that is coming from user.
Just declare a variable as abcd. the input will be stored in the args
collection.

HTH
Chakravarthy
 
A

Arne Vajhøj

SharpieNewbie said:
i'm a newbie in C#. Was in programming many years ago with dBase/
Foxpro/Clipper and some VB.

Perhaps my thinking is still stuck in that kind of mindset..

here's something i'm stuck at..

via the
main(string args), and assuming that the argument passed is ABCD,

how can i create another string variable called ABCD?

is that doable ?

No.

But you can achieve a similar effect using Dictionary.

string s = "ABCD";
string ???? = "XYZ";
// use abcd to get "XYZ"

is not possible. But:

Dictionary<string, string> d = new Dictionary<string, string>();
string s = "ABCD";
d.Add(s, "XYZ");
// use d["ABCD"] to get "XYZ"

is possible.

Arne
 
S

SharpieNewbie

SharpieNewbie said:
i'm a newbie in C#. Was in programming many years ago with dBase/
Foxpro/Clipper and some VB.
Perhaps my thinking is still stuck in that kind of mindset..
here's something i'm stuck at..
via the
main(string args), and assuming that the argument passed is ABCD,
how can i create another string variable called ABCD?
is that doable ?

No.

But you can achieve a similar effect using Dictionary.

string s = "ABCD";
string ???? = "XYZ";
// use abcd to get "XYZ"

is not possible. But:

Dictionary<string, string> d = new Dictionary<string, string>();
string s = "ABCD";
d.Add(s, "XYZ");
// use d["ABCD"] to get "XYZ"

is possible.

Arne

Thanks all.. the replies are helping me to think further.

Much appreciated!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top