C# - auto-creating variables

J

Jl_G_0

hey all.

Im building a webapp that writes data to a DB. But I dont know how
many fields the user will add so I need to know how can I reference
infinite variables like: var1, var2, var3, ..... var300 . The numbers
are sequential. What I want is, to use a variable to store the field
number (lets say intFields) and do something like this on the script:

var + intFields = somevalue ; //Of course this wont work...

What is the syntax in C# for that ? Or maybe what is the best way to
do it ?

Thx.
 
J

Jl_G_0

I tried using arrays... but it fails, saying that im using an
Unassigned Variable:

its like:

string[] strString;
int x=1;
for(.......){
strString[x] = ....... ;
}

It says strString is unassigned, if I remove the [x] it says "missing
identifier"...

But if the array worked, it would be perfect.

Thx.

Doogie escreveu:
 
M

Mantorok

Jl_G_0 said:
I tried using arrays... but it fails, saying that im using an
Unassigned Variable:

its like:

string[] strString;
int x=1;
for(.......){
strString[x] = ....... ;
}

It says strString is unassigned, if I remove the [x] it says "missing
identifier"...

But if the array worked, it would be perfect.

You need an ArrayList as it's dynamic, with an ArrayList you can add as many
items as you want (an array is of fixed length). Use the Add method to add
a new entry into the AL and you will end up with a complete list of field
entries.

HTH
Kev
 
M

Mark Rae

I tried using arrays... but it fails, saying that im using an
Unassigned Variable:

its like:

string[] strString;
int x=1;
for(.......){
strString[x] = ....... ;
}

It says strString is unassigned, if I remove the [x] it says "missing
identifier"...

But if the array worked, it would be perfect.

Sounds like all you need is a generic e.g.

List<string> MyList = new List<string>();
MyList.Add("One");
MyList.Add("Two");
MyList.Add("Three");

etc

Or if you need keys and values, consider a Dictionary<string, string>
generic...
 
D

Doogie

I tried using arrays... but it fails, saying that im using an
Unassigned Variable:

its like:

string[] strString;
int x=1;
for(.......){
strString[x] = ....... ;

}

It says strString is unassigned, if I remove the [x] it says "missing
identifier"...

But if the array worked, it would be perfect.

Some of the others have mentioned this, but I meant ArrayList, not an
array. Also, depending on what version of .NET you are in, you could
use generics as discussed below too - although I'm new into that
version of .NET and don't know a lot about them.
 
J

Jl_G_0

you guys really helped me here... Im using the arraylist, its simple
and the code is almost done.

thx again.
 
L

Laurent Bugnion, MVP

Hi,

Jl_G_0 said:
you guys really helped me here... Im using the arraylist, its simple
and the code is almost done.

thx again.

Using List<> instead of ArrayList is usually a good idea, unless you
have to code against .NET 1.1. ArrayList contains Objects only, so you
need to cast to the desired type (in that case, string) every time.
List<> is a generic collection, so you don't need to cast the elements.

List<string> myList = new List<string>();
myList.Add(...);

HTH,
Laurent
 
S

sloan

A (2.0) Dictionary < string , MyClass >
or
Dictionary < int , MyClass >
are options as well.





Laurent Bugnion said:
Hi,

Jl_G_0 said:
you guys really helped me here... Im using the arraylist, its simple
and the code is almost done.

thx again.

Using List<> instead of ArrayList is usually a good idea, unless you
have to code against .NET 1.1. ArrayList contains Objects only, so you
need to cast to the desired type (in that case, string) every time.
List<> is a generic collection, so you don't need to cast the elements.

List<string> myList = new List<string>();
myList.Add(...);

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft.ch
PhotoAlbum: http://www.galasoft.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
L

Laurent Bugnion, MVP

Hi,
A (2.0) Dictionary < string , MyClass >
or
Dictionary < int , MyClass >
are options as well.

The OP wants a dynamic array of Strings. I don't see the need to use a
Dictionary for this, to be honest. Dictionaries as well as Hashtables
have a hit on performances, compared to non-keyed collections.

HTH,
Laurent
 

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