default types

M

Mike J

below is a code snuppet of a table server class i wrote
this the best way to return default values for null from a datatable or is
there a better way....
still learning all the net framwork
tia
MJ
public object ColumnGet(string cname)
{
//type thistype;
// thistype = this.Table.Rows[iRow][cname].GetType();
object uRow = this.Table.Rows[iRow][cname];
System.TypeCode utype = System.Type.GetTypeCode(GetType());
//this.Table.Rows[iRow][cname].GetType());
if (utype.GetType() == typeof(System.DBNull))
{
//if (this.TreatNullAsType)
//{
return dvNulls.DefaultTypeValue(uRow);
//this.Table.Columns[cname].DataType);
//}
}

return uRow; // this.Table.Rows[iRow][cname];

//supporting static class
public static class dvNulls
{
public static object DefaultTypeValue(object uValue)
{
if (uValue ==typeof(System.String))
{
return "";
}
else if (uValue == typeof(int))
{
return 0;
}
else if (uValue == typeof(System.Boolean))
{
return false;
}
else if (uValue == typeof(System.DateTime))
{
return new DateTime(0);

}
return null;
}
}
 
B

Brian Schwartz

Your code snippt is difficult to follow, but if I understand what you're
doing, then I might write it like this:

public object ColumnGet(string cname)
{
object value = this.Table.Rows[iRow][cname];
if (value == System.DBNull.Value)
value = GetDefaultValueByType(this.Table.Columns[cname].DataType);

return value;
}

public static object GetDefaultValueByType(System.Type type)
{
if (type == typeof(bool))
return false;
else if (type == typeof(string))
return "";
//etc.
else
return null;
}
 
M

Mike J

thanks much
MJ

Brian Schwartz said:
Your code snippt is difficult to follow, but if I understand what you're
doing, then I might write it like this:

public object ColumnGet(string cname)
{
object value = this.Table.Rows[iRow][cname];
if (value == System.DBNull.Value)
value = GetDefaultValueByType(this.Table.Columns[cname].DataType);

return value;
}

public static object GetDefaultValueByType(System.Type type)
{
if (type == typeof(bool))
return false;
else if (type == typeof(string))
return "";
//etc.
else
return null;
}

--
Brian Schwartz
FishNet Components
http://www.fishnetcomponents.com
Fish Grid .NET Light: Powerful Layouts for Small Datasets


Mike J said:
below is a code snuppet of a table server class i wrote
this the best way to return default values for null from a datatable or
is there a better way....
still learning all the net framwork
tia
MJ
public object ColumnGet(string cname)
{
//type thistype;
// thistype = this.Table.Rows[iRow][cname].GetType();
object uRow = this.Table.Rows[iRow][cname];
System.TypeCode utype = System.Type.GetTypeCode(GetType());
//this.Table.Rows[iRow][cname].GetType());
if (utype.GetType() == typeof(System.DBNull))
{
//if (this.TreatNullAsType)
//{
return dvNulls.DefaultTypeValue(uRow);
//this.Table.Columns[cname].DataType);
//}
}

return uRow; // this.Table.Rows[iRow][cname];

//supporting static class
public static class dvNulls
{
public static object DefaultTypeValue(object uValue)
{
if (uValue ==typeof(System.String))
{
return "";
}
else if (uValue == typeof(int))
{
return 0;
}
else if (uValue == typeof(System.Boolean))
{
return false;
}
else if (uValue == typeof(System.DateTime))
{
return new DateTime(0);

}
return null;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top