d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C# Vs Problem
Prev12
Add Reply New Topic New Poll
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 15 2014 07:46am
Quote (labatymo @ Oct 15 2014 08:41am)
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");
}





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.


No he shouldn't. For the very reason I just pointed out. He's struggling learning basic fundamental coding constructs and you are throwing in collections. And in comes me pointing out a concept called type safety, implicit vs explicit type conversion, and boxed and unboxed Objects. There is very little value in telling a student. "I know you're only starting out but here's an advanced concept you might as well pick up."
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 15 2014 09:52pm
Quote (labatymo @ Oct 15 2014 09:41am)
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");
}





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.


btw, c# uses dictionaries for generics. small gotcha is a dictionary will throw an exception if they key isn't there, whereas hashtables return null iirc. if this was java, i wouldn't bother mentioning it since generics is just syntax sugar and don't offer runtime benefits, but there are real benefits in .NET
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll