473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C# - auto-creating variables

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.

Jun 21 '07 #1
9 1132
Would an arraylist work for you?

Jun 21 '07 #2
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:
Would an arraylist work for you?
Jun 21 '07 #3

"Jl_G_0" <jl*********@gmail.comwrote in message
news:11**********************@q69g2000hsb.googlegr oups.com...
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
Jun 21 '07 #4
"Jl_G_0" <jl*********@gmail.comwrote in message
news:11**********************@q69g2000hsb.googlegr oups.com...
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<stringMyList = 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...
--
http://www.markrae.net

Jun 21 '07 #5
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.

Jun 21 '07 #6
you guys really helped me here... Im using the arraylist, its simple
and the code is almost done.

thx again.

Jun 21 '07 #7
Hi,

Jl_G_0 wrote:
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<stringmyList = 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
Jun 21 '07 #8

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

"Laurent Bugnion, MVP" <ga*********@bluewin.chwrote in message
news:eF**************@TK2MSFTNGP05.phx.gbl...
Hi,

Jl_G_0 wrote:
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<stringmyList = 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

Jun 21 '07 #9
Hi,

sloan wrote:
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
--
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
Jun 21 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lew | last post by:
Hi all, I'm trying to create a page that has a user-selectable page auto-refresh option (IE 5.5). Essentially, it's a page that contains a checkbox, when the user checks the checkbox, I'd like...
1
by: Glabbeek | last post by:
I'm changing the layout of my site. Instead of using tables, I will use DIVs. It's working fine, except for 1 thing: In IE6 some DIVs are not the correct width. Mozilla and Opera are showing the...
5
by: Robert Downes | last post by:
I'm using the following in a page that I'm testing in Mozilla: p.actionLinkBlock {border: 1px #000000 dashed; padding: 0.2cm; width: auto} But the dashed border is extending to the right-edge...
20
by: Vijay Kumar R. Zanvar | last post by:
Hello, Unlike register, auto keyword can not be used to declare formal parameter(s). Is there any specific reason for this? Kind regards, Vijay Kumar R. Zanvar
6
by: Alpha | last post by:
I retrieve a table with only 2 columns. One is a auto-generated primary key column and the 2nd is a string. When I add a new row to the dataset to be updated back to the database. What should I...
5
by: Samuel | last post by:
Hi, I am running into a problem of mixing UICulture = auto and allowing users to select culture using a dropdown list. I am detecting a querystring, "setlang", and when found, setting the...
5
by: maya | last post by:
at work they decided to center divs thus: body {text-align:center} #content {width: 612px; text-align:left; margin: 0 auto 0 auto; } this works fine in IE & FF, EXCEPT in FF it doesn't work if...
22
by: nospam_news | last post by:
I currently get asked about my usage of "auto". What is it for? The keyword is clearly superflous here. In contrast to the huge majority of C/C++ developers I write definitions very explicitly...
2
by: Piotr K | last post by:
Hi, I've encountered a strange problem with Firefox which I don't have any idea how to resolve. To the point: I've <divelement with a style "height: auto" and I want to retrieve this value...
21
by: JOYCE | last post by:
Look the subject,that's my problem! I hope someone can help me, thanks
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.