About SmartDataReader

  • Thread starter Thread starter Harvey Triana
  • Start date Start date
H

Harvey Triana

Hello -
I has use SmartDataReade, of
http://www.codeproject.com/KB/database/SmartReader.aspx

Sample (original class)
(1)
public float GetFloat(String column)
{
float data = (reader.IsDBNull(reader.GetOrdinal(column)))
? 0 : float.Parse(reader[column].ToString());
return data;
}

I has rewrite to:
(2)
public float GetFloat(String Name)
{
if (rd.IsDBNull(rd.GetOrdinal(Name))) return 0f; else return
(float)rd[Name];
}

Questions:
It is necessary to use casting?
Which is the most efficient one - (1) or (2) -?
 
Hello Harvey,
Hello -
I has use SmartDataReade, of
http://www.codeproject.com/KB/database/SmartReader.aspx
Sample (original class)
(1)
public float GetFloat(String column)
{
float data = (reader.IsDBNull(reader.GetOrdinal(column)))
? 0 : float.Parse(reader[column].ToString());
return data;
}
I has rewrite to:
(2)
public float GetFloat(String Name)
{
if (rd.IsDBNull(rd.GetOrdinal(Name))) return 0f; else return
(float)rd[Name];
}
Questions:
It is necessary to use casting?
Which is the most efficient one - (1) or (2) -?

Because rd[indexer] returns type object, you'll have to cast it to float
in order to return it as such. Casting, however is much more efficient that
Parse(ToString). In almost any case you see this construct it is due to the
fact that the original developer has noclue about types and casting. It can
even lead to strange bugs when trying to parse the value again, as sometimes
rounding occurs when trying to write the string, or th evalue is written
in such a format, that it cannot be parsed again.

A nice discussion on this subject can be found here:
http://winterdom.com/2006/09/castingoperatorsandtostring
 
Thanks Jesse
--
<Harvey Triana />
PS. Sorry for the "has"

Jesse Houwing said:
Hello Harvey,
Hello -
I has use SmartDataReade, of
http://www.codeproject.com/KB/database/SmartReader.aspx
Sample (original class)
(1)
public float GetFloat(String column)
{
float data = (reader.IsDBNull(reader.GetOrdinal(column)))
? 0 : float.Parse(reader[column].ToString());
return data;
}
I has rewrite to:
(2)
public float GetFloat(String Name)
{
if (rd.IsDBNull(rd.GetOrdinal(Name))) return 0f; else return
(float)rd[Name];
}
Questions:
It is necessary to use casting?
Which is the most efficient one - (1) or (2) -?

Because rd[indexer] returns type object, you'll have to cast it to float
in order to return it as such. Casting, however is much more efficient that
Parse(ToString). In almost any case you see this construct it is due to the
fact that the original developer has noclue about types and casting. It can
even lead to strange bugs when trying to parse the value again, as sometimes
rounding occurs when trying to write the string, or th evalue is written
in such a format, that it cannot be parsed again.

A nice discussion on this subject can be found here:
http://winterdom.com/2006/09/castingoperatorsandtostring
 
Back
Top