473,406 Members | 2,352 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,406 software developers and data experts.

C# App: Problems with ArrayList

14
This project is using .NET and C#. Although my problem is with C# syntax (I think) I didn't see a C# board to post this on. So sorry if this is in the wrong spot.

Basically my problem is trying to get my ArrayList of integers to post after someone is done entering them.

Expand|Select|Wrap|Line Numbers
  1. public static void numberArray()
  2.         {
  3.             Console.WriteLine("\nEnter some numbers into an array.");
  4.             Console.WriteLine("A null value will display the array.\n");
  5.             ArrayList numbers = new ArrayList();
  6.             Int32? inputNum = Convert.ToInt32(Console.ReadLine()); //Made it Nullable
  7.             while (inputNum.HasValue) //checks to see if inputNum is null
  8.             {                            //when it's null, the loop will end
  9.                 try
  10.                 {
  11.                     numbers.Add(inputNum); //if not null, adds to array
  12.                     Console.WriteLine("Number inserted!\n");
  13.                     inputNum = Convert.ToInt32(Console.ReadLine()); 
  14.                  //error is here, when I leave it blank to exit the loop 
  15.                  //and continue, I get a datatype error. 
  16.                 }
  17.                 catch (Exception ex)
  18.                 {
  19.                     Console.WriteLine("An error occured: " + ex.Message);
  20.                 }        
  21.             }
  22.             foreach (Int32 i in numbers)
  23.             {
  24.                 Console.WriteLine(i);
  25.             }
  26.             Console.Read();
  27.         }
  28.  
I understand why I am getting the error. I can't Convert a blank\null value entered to Int32. My question is what is another way I can do this?

I also have a StringCollection in a similar program:
Expand|Select|Wrap|Line Numbers
  1. public static void stringCollect()
  2.         {
  3.             Console.WriteLine("\nEnter some strings and senteces into a collection.");
  4.             Console.WriteLine("An empty string will display the collection.\n");
  5.             StringCollection strCol = new StringCollection();
  6.             string inputString = Console.ReadLine();
  7.             while (inputString != "")         //Does this until empty string is inserted
  8.             {
  9.                 try                             //Exception handling for each string entered.
  10.                 {
  11.                     strCol.Add(inputString);         //Add string to collection
  12.                     Console.WriteLine("String inserted\n"); //Tell string was inserted
  13.                     inputString = Console.ReadLine();     //New readline for new string
  14.                 }
  15.                 catch (Exception ex)
  16.                 {
  17.                     Console.WriteLine("An error occured: " + ex.Message);
  18.                 }
  19.             }
  20.             foreach (string str in strCol) //Displays all strings in the collection
  21.             { 
  22.                 Console.WriteLine(str);                 
  23.             }                                         
  24.             Console.Read();
  25.         }
  26.  
It works perfect and I was trying to keep to same concept for the numberArray, but I'm stuck on what to do differently.

Any help would be appreciated.

Thanks!
Dec 19 '07 #1
5 1422
r035198x
13,262 8TB
1.) Test for null using if(value == null). Don't derefence a variable if you think it could be null.
2.) No need to put that ? after Int32.
3.) Get a C# tutorial and read it.
Dec 19 '07 #2
Plater
7,872 Expert 4TB
This does not appear to have anything to do with ArrayList, so much as it does with the misunderstanding of converting values. Upon confirmation I will change the thread title.
You should be using the Parse()/TryParse() functions, not the convert functions.
Int32.TryParse() should be usefull to you.

Expand|Select|Wrap|Line Numbers
  1. Int32 mynum=0;
  2.  
  3. bool result = Int32.TryParse(Console.ReadLine(), out mynum);
  4.  
  5. if (result)
  6. {//mynum contains a valid number
  7. }
  8. else
  9. {// Console.ReadLine() produced a string that was either blank ("") or could not be converted to a number
  10. }
  11.  
Dec 19 '07 #3
sigil
4
Also I would not use ArrayList, instead use List<string> or List<int> then you don't have to be casting all the time.

Best way I feel to do validation is to use Regex, in the long run it saves you time.
Dec 19 '07 #4
dhyder
14
Thank you everyone for the input and replies. I will definitely look into everything and see what I can get to work.

Plater, I haven't used Parse and TryParse before, but I've seen it quite a bit and is definitely something I will research.

sigil, I never thought of generics. Thanks for the idea.
Dec 20 '07 #5
dhyder
14
Plater, the TryParse is exactly what I was looking for. Works great. Thanks alot.

Thank you everyone for your help, much appreciated.
Dec 20 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: jarkkotv | last post by:
Hi everyone! I'm having a little problem when sorting the ArrayList and I was wondering if there is a .NET guru who can help me out :) I'm trying to sort ArrayList alphabetically in ASP.Net...
1
by: TheCow | last post by:
Hi Group I have homemade class, lets call it firstclass, and then another class (secondclass) which contains an ArrayList of firstclass objects. When I try to xml serialize secondclass with...
13
by: msuk | last post by:
All, I have a app.config which contains repeating tags as shown below: <filelist> <file> <filetype>xxx</filetype> <hex>yyyy</hex> </file>
1
by: Matthias De Ridder | last post by:
Hello, I really hope that someone will be able to help me, because I'm desperate now! I'm a student, graduating this year, and I'm working on a thesis where C# Web Services are involved. I...
4
by: hzgt9b | last post by:
Using VB .NET 2003, I have a windows application that performs a series of file actions (copy, move, delete) but the actions are completing before the window is painted on the screen... how can I...
2
by: Mike P | last post by:
I am programmatically starting and ending a local instance of Outlook : //programmatically start local Outlook System.Diagnostics.Process proc = new System.Diagnostics.Process();...
2
by: ShaveDave27 | last post by:
Hi, I'm trying to create an Arraylist Class which will allow me to output the whole list in one method. I think i've created the arraylist correctly but when i try to get the list to output i come...
1
by: Kapps | last post by:
Hi, I was wondering how I would go about deserializing a file that was serialized from a different project. I've tried a couple things, but I can't really seem to figure it out. When looking it...
1
by: RequieM | last post by:
Hi, I am currently tearing my hair out trying to figure out how to effectivly parse a set of XML data I have in to an Arraylist of Custom Objects. I am unsure of the best method, whether to use...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.