Click here to Skip to main content
15,887,214 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: I wonder what the programmer was going to do.. Pin
inch8-Jan-14 3:58
inch8-Jan-14 3:58 
GeneralRe: I wonder what the programmer was going to do.. Pin
OriginalGriff8-Jan-14 3:54
mveOriginalGriff8-Jan-14 3:54 
GeneralRe: I wonder what the programmer was going to do.. Pin
tgrt8-Jan-14 4:06
tgrt8-Jan-14 4:06 
GeneralRe: I wonder what the programmer was going to do.. Pin
MarkTJohnson8-Jan-14 4:46
professionalMarkTJohnson8-Jan-14 4:46 
GeneralRe: I wonder what the programmer was going to do.. Pin
BillW339-Jan-14 3:29
professionalBillW339-Jan-14 3:29 
GeneralRe: I wonder what the programmer was going to do.. Pin
phil.o9-Jan-14 5:51
professionalphil.o9-Jan-14 5:51 
GeneralRe: I wonder what the programmer was going to do.. Pin
Lutosław12-Jan-14 6:29
Lutosław12-Jan-14 6:29 
GeneralRe: I wonder what the programmer was going to do.. Pin
Prabhu S M12-Jan-14 19:30
Prabhu S M12-Jan-14 19:30 
It's always good to check whether a particular record is existing or not before accessing that record.
Specially in collections, sometimes it maybe empty or less than the number of required records that you want to access.

The following code will execute based on the records available
C#
DataTable table = new System.Data.DataTable();
table.Columns.Add("ID");
table.Rows.Add("1");
string test = string.Empty;

int num_of_records = table.Rows.Count;
if (num_of_records > 0)
{
    test = table.Rows[0][0].ToString();
}
else
{
    return;
}


The following code is directly trying to access a record which may or may not be existing though the code is syntactically correct it may throw error in the event of unavailablity of records.
C#
DataTable table = new System.Data.DataTable();
table.Columns.Add("ID");
string test = string.Empty;
test = table.Rows[0][0].ToString();

IndexOutOfRangeException - There is no row at position 0.
GeneralRe: I wonder what the programmer was going to do.. Pin
Paul Conrad13-Jan-14 7:40
professionalPaul Conrad13-Jan-14 7:40 
GeneralPassword Checking Pin
Rob Grainger3-Jan-14 5:29
Rob Grainger3-Jan-14 5:29 
GeneralRe: Password Checking Pin
Brisingr Aerowing3-Jan-14 6:18
professionalBrisingr Aerowing3-Jan-14 6:18 
GeneralRe: Password Checking Pin
Sentenryu3-Jan-14 7:55
Sentenryu3-Jan-14 7:55 
GeneralRe: Password Checking Pin
OriginalGriff3-Jan-14 9:26
mveOriginalGriff3-Jan-14 9:26 
GeneralError Messages 101 Pin
Brisingr Aerowing20-Dec-13 5:27
professionalBrisingr Aerowing20-Dec-13 5:27 
GeneralRe: Error Messages 101 Pin
PIEBALDconsult20-Dec-13 7:39
mvePIEBALDconsult20-Dec-13 7:39 
GeneralRe: Error Messages 101 Pin
LloydA11120-Dec-13 10:53
LloydA11120-Dec-13 10:53 
GeneralRe: Error Messages 101 Pin
Brisingr Aerowing20-Dec-13 13:57
professionalBrisingr Aerowing20-Dec-13 13:57 
GeneralRe: Error Messages 101 Pin
Sentenryu22-Dec-13 22:20
Sentenryu22-Dec-13 22:20 
GeneralRe: Error Messages 101 Pin
PIEBALDconsult23-Dec-13 5:10
mvePIEBALDconsult23-Dec-13 5:10 
GeneralRe: Error Messages 101 PinPopular
Sentenryu22-Dec-13 22:17
Sentenryu22-Dec-13 22:17 
GeneralRe: Error Messages 101 Pin
Brisingr Aerowing23-Dec-13 3:08
professionalBrisingr Aerowing23-Dec-13 3:08 
GeneralRe: Error Messages 101 Pin
Albert Holguin28-Dec-13 12:06
professionalAlbert Holguin28-Dec-13 12:06 
GeneralRe: Error Messages 101 Pin
Albert Holguin28-Dec-13 12:07
professionalAlbert Holguin28-Dec-13 12:07 
GeneralRe: Error Messages 101 Pin
Brisingr Aerowing29-Dec-13 9:44
professionalBrisingr Aerowing29-Dec-13 9:44 
GeneralRe: Error Messages 101 Pin
Rob Grainger31-Dec-13 0:38
Rob Grainger31-Dec-13 0:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.