-
November 12th, 2010, 06:05 PM
#1
Assigning a random number to an array List c#?
please help, im trying to assign a random number variable which ive created called 'custID' in a Customer Class to an array list of customers called 'customerList' which is in the program.
How can I do this?
Any help is appreciated.
Thanks
-
November 12th, 2010, 06:56 PM
#2
Re: Assigning a random number to an array List c#?
First off, if you are trying to use random numbers to generate unique id's.. don't. Use a GUID.
Anyway, look at the Random class.
-
November 13th, 2010, 03:18 AM
#3
Re: Assigning a random number to an array List c#?
Code:
Random rnd = new Random();
System.Collections.ArrayList rndID = new System.Collections.ArrayList();
int numsID = 20;
int minID = 1;
int maxID = 100;
for (int c = 1; c <= numsID; c++)
{
rndID.Add(rnd.Next(minID, maxID));
}
-
November 13th, 2010, 04:51 AM
#4
Re: Assigning a random number to an array List c#?
Originally Posted by ClassCoder
Code:
Random rnd = new Random();
System.Collections.ArrayList rndID = new System.Collections.ArrayList();
int numsID = 20;
int minID = 1;
int maxID = 100;
for (int c = 1; c <= numsID; c++)
{
rndID.Add(rnd.Next(minID, maxID));
}
I don't think you should be encouraging people to use ArrayList as it is deprecated. Generic List<> is strongly typed, has increased functionality and greater performance and memory usage. It's available on all versions of .NET since 2.0 I believe.
-
November 13th, 2010, 08:14 AM
#5
Re: Assigning a random number to an array List c#?
Originally Posted by Chris_F
I don't think you should be encouraging people to use ArrayList as it is deprecated. Generic List<> is strongly typed, has increased functionality and greater performance and memory usage. It's available on all versions of .NET since 2.0 I believe.
i agree with you .. i don't never use ArrayList in my applications .. i prefer using List<>,HashTable<,>,Dictionary<,>
when we use LINQ IEnumrable<>
-
November 13th, 2010, 03:11 PM
#6
Re: Assigning a random number to an array List c#?
thanks for the feedback - i've been told to use array list so i'm guessing i have to.
I have an arraylist called customerList and all i want to do is give each of the customers a Random number
so that when i execute the command to display all the customers in that array, it displays its ID alongside each individual customer.
Ive researched GUID's and they would have been ideal but they are too long and Random numbers are sure to be fine as its just a mini project with a few customers in the array.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|