d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C# Vs Problem
12Next
Add Reply New Topic New Poll
Member
Posts: 6,175
Joined: Sep 4 2009
Gold: Locked
Trader: Scammer
Oct 12 2014 08:36pm
If access is denied the date, time
and a message “Access Denied” is written to the window. Furthermore
user can enter one digit access code to summon a security guard for
assistance. The date, the time and a message “Received Request – Will INFO 1112 – Assignment 2 2
be there shortly” are then written to the window to indicate that the
request has been received. If Enter button # is pressed without entering
any data, the date, time and a message "Pl Enter a Valid code" are written
to the window

I am completely finished with this program except I have one small problem and I can't seem to figure out. I want to have my listbox ( test output ) display only the certain statement it asks for when i do a specific action:

E.x When i click #, only "Please enter a valid code" is shown on my output. However, when i click # on my program, it shows both Access denied and "Please enter a valid code". I want it to only show the first one.

I know the coding is very repetitive, but that is how my proff wants it to be formatted. I tried adding more {} around each if statements that i want to bound, but that doesn't seem to work. Any advice would surely help me ! thanks

private void btnnumber_Click(object sender, EventArgs e)
{

try
{

if (txtInputCode.Text == "1432")
{
listBoxCodes.Items.Add(DateTime.Now + " " + "INFO First Year Students");
}
else if (txtInputCode.Text == "2543")
{
listBoxCodes.Items.Add(DateTime.Now + " " + "INFO Second Year Students");
}

else if (txtInputCode.Text == "3543")
{
listBoxCodes.Items.Add(DateTime.Now + " " + "B.Tech Students");
}
else if (txtInputCode.Text == "2645")
{
listBoxCodes.Items.Add(DateTime.Now + " " + "CSIT faculty");
}
else if (txtInputCode.Text == "2646")
{
listBoxCodes.Items.Add(DateTime.Now + " " + "CSIT faculty");
}
else if (txtInputCode.Text == "2647")
{
listBoxCodes.Items.Add(DateTime.Now + " " + "CSIT faculty");
}
else if (txtInputCode.Text == "2648")
{
listBoxCodes.Items.Add(DateTime.Now + " " + "CSIT faculty");
}
else if (txtInputCode.Text == "8888")
{
listBoxCodes.Items.Add(DateTime.Now + " " + "IET Staff");
}
else
{
listBoxCodes.Items.Add(DateTime.Now + " " + "Access Denied");
}
{
int s = int.Parse(txtInputCode.Text);
if (s < 10)
{
listBoxCodes.Items.Add(DateTime.Now + " " + "Received, will be there shortly");
}
}
}
catch
{
listBoxCodes.Items.Add(DateTime.Now + " " + "Please Enter a Valid Number");
}
}
Member
Posts: 6,562
Joined: Oct 29 2007
Gold: 4.00
Oct 12 2014 08:53pm
use code tags
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 12 2014 09:04pm
Try catch blocks only catch specific errors during runtime unless you use a keyword such as `throw()` to throw an error.

You have to rework your logic for your desired output to work.

What you should do is check if the access code is actually an integer, if not assign your text removing the error handling all together.

Although you are defiantly not using try,catch,throw properly. I suggest reading up on it.

Also you seem to have stray braces near your last chunk of code before the catch, does this application even compile?

The easiest way to check for stray input is to simply convert your string into an integer and do a wide logic check on it as follows:

Code
if(int.parse(txtInputCode.Text) <= 0 && int.parse(txtInputCode.Text)i >= 9999) {
//output error.
}


Although this in itself may cause a runtime error which will need to be caught depending on the conversions function process of handling non integer input.

Quote (Minkomonster @ Oct 12 2014 11:08pm)
int.parse will through a FormatException if he tries to parse a string that isn't an int. In which case, his try catch will work. It just isn't very good design. His real problem is the logic he is implementing in his if-else chain. That needs to be reworked.


He brings up a good point and the correct function to call to attempt to make this work.

This post was edited by AbDuCt on Oct 12 2014 09:12pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 12 2014 09:08pm
Quote (AbDuCt @ Oct 12 2014 10:04pm)
Try catch blocks only catch specific errors during runtime unless you use a keyword such as `throw()` to throw an error.

You have to rework your logic for your desired output to work.

What you should do is check if the access code is actually an integer, if not assign your text or thrown an error.

Although you are defiantly not using try,catch,throw properly. I suggest reading up on it.


int.parse will through a FormatException if he tries to parse a string that isn't an int. In which case, his try catch will work. It just isn't very good design. His real problem is the logic he is implementing in his if-else chain. That needs to be reworked.
Member
Posts: 6,175
Joined: Sep 4 2009
Gold: Locked
Trader: Scammer
Oct 12 2014 10:23pm
The program is working perfectly,except the output I desire is not coming the way i want it to be

Quote (Minkomonster @ Oct 12 2014 07:08pm)
int.parse will through a FormatException if he tries to parse a string that isn't an int. In which case, his try catch will work. It just isn't very good design. His real problem is the logic he is implementing in his if-else chain. That needs to be reworked.


any tips or advice on how I can improve my if-else chain?
Member
Posts: 6,175
Joined: Sep 4 2009
Gold: Locked
Trader: Scammer
Oct 12 2014 10:36pm
never mind, figured it out !

I just added:

else if (int.Parse(txtInputCode.Text) > 10)
{
listBoxCodes.Items.Add(DateTime.Now + " " + "Access Denied");
}

and all the functions worked normally in my test outputs now
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Oct 14 2014 07:25am
use a HashTable

Code

Hashtable codes= new Hashtable();
codes.add("1432","INFO First Year Students");
codes.add("2543","INFO Second Year Students");
//add rest of codes

String code = codes[txtInputCode.Text];
if(code!=null){
listBoxCodes.Items.Add(DateTime.Now + " " + code;
}else{
listBoxCodes.Items.Add(DateTime.Now + " " + "Please Enter a Valid Number");
}

Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 14 2014 04:51pm
Quote (labatymo @ Oct 14 2014 09:25am)
use a HashTable

Code
Hashtable codes= new Hashtable();
codes.add("1432","INFO First Year Students");
codes.add("2543","INFO Second Year Students");
//add rest of codes

String code = codes[txtInputCode.Text];
if(code!=null){
listBoxCodes.Items.Add(DateTime.Now + " " + code;
}else{
listBoxCodes.Items.Add(DateTime.Now + " " + "Please Enter a Valid Number");
}


Calm down speed racer. He was having enough troubles with if statements.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 15 2014 07:11am
Quote (labatymo @ Oct 14 2014 08:25am)
use a HashTable

Code
Hashtable codes= new Hashtable();
codes.add("1432","INFO First Year Students");
codes.add("2543","INFO Second Year Students");
//add rest of codes

String code = codes[txtInputCode.Text];
if(code!=null){
listBoxCodes.Items.Add(DateTime.Now + " " + code;
}else{
listBoxCodes.Items.Add(DateTime.Now + " " + "Please Enter a Valid Number");
}


I am pretty sure that won't even compile. Hashtable's are not type safe. You will get a compile error here:

Code
String code = codes[txtInputCode.Text]


Because you are trying to implicitly convert an object to a string.
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Oct 15 2014 07:41am
Quote (Minkomonster @ Oct 15 2014 09:11am)
I am pretty sure that won't even compile. Hashtable's are not type safe. You will get a compile error here:

Code
String code = codes[txtInputCode.Text]


Because you are trying to implicitly convert an object to a string.


Fixed it

Code
Hashtable codes= new Hashtable();
codes.Add("1432","INFO First Year Students");
codes.Add("2543","INFO Second Year Students");
//add rest of codes

String code = (String)codes[txtInputCode.Text];
if(code!=null){
listBoxCodes.Items.Add(DateTime.Now + " " + code);
}else{
listBoxCodes.Items.Add(DateTime.Now + " " + "Please Enter a Valid Number");
}



Quote (AbDuCt @ Oct 14 2014 06:51pm)
Calm down speed racer. He was having enough troubles with if statements.


He might as well learn them as soon as possible as maps are one of the most useful things in programming. Plus there's an if else statement in there.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll