Click here to Skip to main content
15,946,525 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello experts,

my query today is not a simple one. I made a simple program where the user inputs a string with Comma separated variables, and then I implemented a string split which i am saving each variable in an elemt of an array. It works fine, but if by exedent i type nothing, an exempsion error pops. How can i solve it pls??

string input = Console.ReadLine();
            string[] dataBroken = null;
            byte[] temp = new byte[6];
            dataBroken = input.Split(',');

            for (int i = 1; i <= 4; i++)
                temp[i] = Convert.ToByte(dataBroken[i]);

            myItem.Name = dataBroken[0];
            myItem.Size1 = temp[1];
            myItem.Size2 = temp[2];
            myItem.Size3 = temp[3];
            myItem.Size4 = temp[4];
Andreas Gieriet 20-Jan-13 9:40am    
Your code does not make much sense. I.e. if I had to take over that code I would have not clue what the meaning of it is. Say in prose/pseude-oode what the program is supposed to do.
E.g. what is dataBroken? Sounds like corrupt data.
E.g. what is temp? That's the worst name you can "invent" - never ever use that. If you cannot think of a better name, your algorithm is probably broken!
E.g. why an array of 6 bytes, but you only use 4 of them.
E.g. what do you expect Convert.ToByte(...) does for you?
E.g. what is the type definition of myItem? Especially, what is the meaning of SizeX (BTW: counting up variables with numbers is the second worst naming scheme...)?

So, please clarify what you want to achieve in general, give desent names to everything in your code tellung (you and) us what it stands for. With that, we have a resonambe base for discussion.

Gilmore Sacco 20-Jan-13 9:49am    
hello sir, your points are mostly valid, but this is a just part of the whole code.
databroken, refers to the data been broken down into smaller parts and saved in an array. Correct the array has 6 value and i am using 5 of them. I had to do this since i needed to shift the array elemts to macth the list so that i use a loop, not doing it manually. Also each Size1, Size2... are properties not variables, in which i am encapsulating the input. temp as the name suggest is a temporay array so that i can sort the values and store the resulting sorted array in the list variables. my Item is an instance of a class with an empty constractor and the variables are name, size1,size2...size5.

Hope this clarifies more my code

1 solution

Test for null.

Also, don't assume there'll be five items, check.

Don't use Convert (ever); use Byte.TryParse.
Share this answer
Gilmore Sacco 20-Jan-13 9:28am    
thanks for the pro tips. yes my difficulty is, how can i not assume that there will be 5 inputs, If i make 4 inputs it will crash. So how can avoid that?
PIEBALDconsult 20-Jan-13 16:42pm    
Check how many there are -- think about it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900