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. -
public static void numberArray()
-
{
-
Console.WriteLine("\nEnter some numbers into an array.");
-
Console.WriteLine("A null value will display the array.\n");
-
ArrayList numbers = new ArrayList();
-
Int32? inputNum = Convert.ToInt32(Console.ReadLine()); //Made it Nullable
-
while (inputNum.HasValue) //checks to see if inputNum is null
-
{ //when it's null, the loop will end
-
try
-
{
-
numbers.Add(inputNum); //if not null, adds to array
-
Console.WriteLine("Number inserted!\n");
-
inputNum = Convert.ToInt32(Console.ReadLine());
-
//error is here, when I leave it blank to exit the loop
-
//and continue, I get a datatype error.
-
}
-
catch (Exception ex)
-
{
-
Console.WriteLine("An error occured: " + ex.Message);
-
}
-
}
-
foreach (Int32 i in numbers)
-
{
-
Console.WriteLine(i);
-
}
-
Console.Read();
-
}
-
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: -
public static void stringCollect()
-
{
-
Console.WriteLine("\nEnter some strings and senteces into a collection.");
-
Console.WriteLine("An empty string will display the collection.\n");
-
StringCollection strCol = new StringCollection();
-
string inputString = Console.ReadLine();
-
while (inputString != "") //Does this until empty string is inserted
-
{
-
try //Exception handling for each string entered.
-
{
-
strCol.Add(inputString); //Add string to collection
-
Console.WriteLine("String inserted\n"); //Tell string was inserted
-
inputString = Console.ReadLine(); //New readline for new string
-
}
-
catch (Exception ex)
-
{
-
Console.WriteLine("An error occured: " + ex.Message);
-
}
-
}
-
foreach (string str in strCol) //Displays all strings in the collection
-
{
-
Console.WriteLine(str);
-
}
-
Console.Read();
-
}
-
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!
5 1422
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.
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. -
Int32 mynum=0;
-
-
bool result = Int32.TryParse(Console.ReadLine(), out mynum);
-
-
if (result)
-
{//mynum contains a valid number
-
}
-
else
-
{// Console.ReadLine() produced a string that was either blank ("") or could not be converted to a number
-
}
-
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.
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.
Plater, the TryParse is exactly what I was looking for. Works great. Thanks alot.
Thank you everyone for your help, much appreciated.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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>
|
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...
|
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...
|
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();...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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...
|
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...
|
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...
|
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,...
| |