Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2007

Tip: Looking for answers? Try searching our database.

C# - auto-creating variables

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jl_G_0 - 21 Jun 2007 14:44 GMT
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.
Doogie - 21 Jun 2007 14:58 GMT
Would an arraylist work for you?
Jl_G_0 - 21 Jun 2007 15:11 GMT
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.

> Would an arraylist work for you?
Mantorok - 21 Jun 2007 15:15 GMT
> I tried using arrays... but it fails, saying that im using an
> Unassigned Variable:
[quoted text clipped - 11 lines]
>
> 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
Mark Rae - 21 Jun 2007 15:21 GMT
> I tried using arrays... but it fails, saying that im using an
> Unassigned Variable:
[quoted text clipped - 11 lines]
>
> 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...

Signature

http://www.markrae.net

Doogie - 21 Jun 2007 15:31 GMT
>  I tried using arrays... but it fails, saying that im using an
> Unassigned Variable:
[quoted text clipped - 12 lines]
>
> 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.
Jl_G_0 - 21 Jun 2007 15:52 GMT
you guys really helped me here... Im using the arraylist, its simple
and the code is almost done.

thx again.
Laurent Bugnion, MVP - 21 Jun 2007 16:48 GMT
Hi,

>  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
Signature

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

sloan - 21 Jun 2007 17:12 GMT
A (2.0) Dictionary < string , MyClass >
or
Dictionary < int , MyClass >
are options as well.

> Hi,
>
[quoted text clipped - 13 lines]
> HTH,
> Laurent
Laurent Bugnion, MVP - 21 Jun 2007 18:26 GMT
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
Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.