C# - auto-creating variables

  • Thread starter Thread starter Jl_G_0
  • Start date Start date
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.
 
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:
 
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
 
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...
 
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.
 
you guys really helped me here... Im using the arraylist, its simple
and the code is almost done.

thx again.
 
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
 
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
 
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
 
Back
Top