Hi..
I am new to c#. I want to write common procedure , when I pass database field as a parameter to procedure, if field has value just return the value or if field type is numeric then reurn 0 or if it is string then return ""..depending upon datatype of field being passed.
Something like this considering CheckNull is the name of procedure..
txtCode.Text=CheckNull(rs.Fields["ID"]);
txtDate.Text=CheckNull(rs.Fields["SaleDate"]);
txtYear.Text=CheckNull(rs.Fields["NoofYears"]);
Loading
ca guyPosted Jan 8, 2008, 6:44 PM
Posted Jan 8, 2008, 3:23 PM
{
switch (theDBObject.GetType().ToString())
{
case "System.Int32":
return 0;
case "System.String":
return String.Empty;
...
default:
break;
}
}
Then just cast the object back to what you need it to be using a TryParse method.
ca guyPosted Jan 8, 2008, 1:25 PM
Yes ..sorry I meant Overloading. I have been using delphi..new to c#. In example you gave here you are returning only datatype as string. Is this possible? What return data type I need to give here?
private string CheckValue(Object theDBObject)
{
switch (theDBObject.GetType().ToString())
{
case "System.int32": return 0;
case "System.String": return "";
...
default: break;
}
}
DavePosted Jan 4, 2008, 9:11 PM
You could write overloaded methods. Another idea would be to use a switch statement.
Something like:
Posted Jan 2, 2008, 6:23 PM
Strings are reference type variables, while integers are value types. You could try doing something like:
if(myVar.GetType().IsValueType)
//Do something
else
//Do something else
ca guyPosted Jan 2, 2008, 5:52 PM
DavePosted Dec 28, 2007, 3:23 AM
I cannot think of a way off the top of my head for a method that will be able to check for both value types and reference types. But here is a suggestion for checking for nulls:
private bool CheckNull(Object anObject)
{
}
You could call this with something like:
bool isItNull = CheckNull(instanceDataReader.GetValue());
If the line if(anObject == null) does not work, you could try if(anObject is DBNull)
I have not tested my suggestions, so no guarantees that they'll work.
ca guyPosted Dec 28, 2007, 12:32 AM
DavePosted Dec 27, 2007, 11:07 PM