Click here to Skip to main content
15,912,021 members
Home / Discussions / C#
   

C#

 
GeneralRe: Click Event Problem Pin
FocusedWolf9-Feb-07 9:19
FocusedWolf9-Feb-07 9:19 
GeneralRe: Click Event Problem Pin
Semion_N9-Feb-07 9:21
Semion_N9-Feb-07 9:21 
GeneralRe: Click Event Problem Pin
FocusedWolf9-Feb-07 9:46
FocusedWolf9-Feb-07 9:46 
GeneralRe: Click Event Problem Pin
Semion_N9-Feb-07 9:55
Semion_N9-Feb-07 9:55 
GeneralRe: Click Event Problem Pin
FocusedWolf9-Feb-07 11:44
FocusedWolf9-Feb-07 11:44 
QuestionInserting text at the current location in a text box Pin
Wolf929-Feb-07 7:21
Wolf929-Feb-07 7:21 
AnswerRe: Inserting text at the current location in a text box Pin
Stefan Troschuetz9-Feb-07 7:28
Stefan Troschuetz9-Feb-07 7:28 
QuestionMethod I wrote to safely parse data. Pin
siccsyc9-Feb-07 5:50
siccsyc9-Feb-07 5:50 
Hey guys, I just wanted to get some input on this method I wrote to parse data. Normally every time you want to parse data coming from a datasource, we need to do something like:

int myInt = 0;<br />
try<br />
{<br />
    myInt = (int)ds["columnName"];<br />
}<br />
catch{}<br />
<br />
bool myBool = false;<br />
try<br />
{<br />
    (bool)ds["columnName"];<br />
}<br />
catch{}<br />

etc....

This is of course gets very ugly the more data you are grabbing. Initially in c# 1.1 I had written a class with a bunch of methods for each variable type. So for the int example above I would use:

public int ParseInt( string sValue, int defaultValue )<br />
{<br />
    try<br />
    {<br />
        return int.Parse( sValue );<br />
    }<br />
    catch<br />
    {<br />
        return defaultValue;<br />
    }<br />
}


As well as an overloaded method

public int ParseInt( string sValue )<br />
{<br />
    return ParseInt( sValue, 0 );<br />
}


So the above example would now look like:
<br />
int myInt = ParseInt( ds["columnName"].ToString() );<br />
bool myBool = ParseBool( ds["columnName"].ToString() );<br />

For the record, I realize this was not a good solution, as the .ToString() method would throw an exception if ds["columnName"] was null. I should have had the methods take an object as the argument, and try (int)object inside the method instead of int.Parse(). Regardless, this worked well but you needed to write 2 methods for each variable type. With the release of c# 2.0 and generics, I decided to make a more flexible version. I came up with the following 2 methods that will handle any variable type.

<br />
public static T Parse<T>( object objToParse, T defaultValue )<br />
{<br />
    try<br />
    {<br />
        return (T)Convert.ChangeType( objToParse, typeof( T ) );<br />
    }<br />
    catch<br />
    {<br />
	return defaultValue;<br />
    }<br />
}


And the overloaded method

public static T Parse<T>( object objToParse )<br />
{<br />
    return Parse<T>( objToParse, default( T ) );<br />
}


So now my above examples become:

int myInt = Parse<int>( ds["columnName"] );<br />
bool myBool = Parse<bool>( ds["columnName"] );


MUCH cleaner than before, and you can pass it any type of object. This seems to be a perfect solution, but I am posting to see if anybody sees anything wrong with this. We are using this method pretty heavy now @ my current job, and I just want to be sure I haven't overlooked anything. Thanks for the help.
AnswerRe: Method I wrote to safely parse data. Pin
led mike9-Feb-07 8:34
led mike9-Feb-07 8:34 
GeneralRe: Method I wrote to safely parse data. Pin
siccsyc9-Feb-07 9:44
siccsyc9-Feb-07 9:44 
QuestionHow can i check if a server exists? Pin
sharpiesharpie9-Feb-07 5:45
sharpiesharpie9-Feb-07 5:45 
AnswerRe: How can i check if a server exists? Pin
Guffa9-Feb-07 7:23
Guffa9-Feb-07 7:23 
GeneralRe: How can i check if a server exists? Pin
sharpiesharpie9-Feb-07 8:05
sharpiesharpie9-Feb-07 8:05 
GeneralRe: How can i check if a server exists? Pin
Dave Kreskowiak9-Feb-07 9:13
mveDave Kreskowiak9-Feb-07 9:13 
AnswerRe: How can i check if a server exists? Pin
Guffa9-Feb-07 13:08
Guffa9-Feb-07 13:08 
Questionhow to enumarate SQL Server local instances in C#? Pin
bouli9-Feb-07 3:52
bouli9-Feb-07 3:52 
AnswerRe: how to enumarate SQL Server local instances in C#? Pin
Blue_Boy9-Feb-07 4:10
Blue_Boy9-Feb-07 4:10 
GeneralRe: how to enumarate SQL Server local instances in C#? Pin
bouli9-Feb-07 4:29
bouli9-Feb-07 4:29 
GeneralRe: how to enumarate SQL Server local instances in C#? Pin
Blue_Boy9-Feb-07 4:33
Blue_Boy9-Feb-07 4:33 
GeneralRe: how to enumarate SQL Server local instances in C#? Pin
bouli9-Feb-07 4:44
bouli9-Feb-07 4:44 
GeneralRe: how to enumarate SQL Server local instances in C#? Pin
Blue_Boy9-Feb-07 5:22
Blue_Boy9-Feb-07 5:22 
QuestionLoading Report (Crystal Report) from my Application, after deployment. Pin
Banjo Ayorinde9-Feb-07 3:37
Banjo Ayorinde9-Feb-07 3:37 
QuestionProblem in a webservice!!! [modified] Pin
kmuthuk9-Feb-07 3:29
kmuthuk9-Feb-07 3:29 
AnswerRe: Problem in a webservice!!! Pin
ednrgc9-Feb-07 4:36
ednrgc9-Feb-07 4:36 
AnswerRe: Problem in a webservice!!! Pin
m@u9-Feb-07 5:06
m@u9-Feb-07 5:06 

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.