Reputation: 155
Datatable dtProduto; (Filled)
int cdProduto = Convert.ToInt16(dtProduto.Rows[0]["cdProduto"]);
int cdReferencia = Convert.ToInt16(dtProduto.Rows[0]["cdReferencia"]);
The syntax dtProduto.Rows[i][Column] always returns an object, which one is the best way to convert'em to integers?
Regards, Jorge.
Upvotes: 1
Views: 99
Reputation: 33010
The way you have currently converted them is probably the safest approach, assuming the expected data type is fixed. The Convert class performs a variety of checks to make sure that it can convert properly without unexpected exceptions...and in the cases where it can not convert properly, you get specific documented exceptions that you can directly support.
It should be noted that the additional verification adds overhead, and as such, may not be the most performant approach. If you know for sure that the underlying value in those datatable fields are always a specific type, a direct cast is probably the most performant approach...but it could be riskier, and may result in unexpected exceptions.
Upvotes: 1
Reputation: 107626
You're currently converting them to what would be a "short" -- or a 16-bit integer. I don't think there's anything wrong with the way you're doing it now. Simply substitute Convert.ToInt32()
if you need a 32-bit Integer instead.
If you aren't sure what may be contained in your data source, you might want to be more careful about directly converting your source record data to concrete values by checking for nulls and/or parsing the result. Something like:
int value;
if (dtProduto.Rows[0]["cdProduto"] != null)
{
if (!int.TryParse(dtProduto.Rows[0]["cdProduto"].ToString(), out value))
{
// Log exception, throw exception, do nothing, etc.
} // else row value is now an integer stored in the value variable
}
Substitute short
for int
if you do want 16-bit integers.
Upvotes: 3
Reputation: 27894
What does this give you?
string type1 = dtProduto.Rows[0]["cdProduto"].GetType().Name
string type2 = dtProduto.Rows[0]["cdReferencia"].GetType().Name
The database is probably returning the proper object type, in which case you simply need to cast the object to the proper type. This will tell you what that type is, just cast to it, and go. No need to Convert.ToAnything, that's probably doing more work than you need.
You will probably end up with something like this:
int cdProduto = (int)dtProduto.Rows[0]["cdProduto"];
int cdReferencia = (int)dtProduto.Rows[0]["cdReferencia"];
or if you want something slightly different in C# than the database gives you, something like this:
int cdProduto = (int)(ushort)dtProduto.Rows[0]["cdProduto"];
int cdReferencia = (int)(ushort)dtProduto.Rows[0]["cdReferencia"];
(In the above code block, the (ushort)
is a cast of object to ushort, the (int)
is a conversion of ushort to int.)
Upvotes: 0
Reputation: 4082
It simply depends on how you want to handle it. Convert will throw an exception, You could also use short.TryParse which doesn't throw an exception.
Read some info here: http://blogs.msdn.com/b/ianhu/archive/2005/12/19/505702.aspx
I mostly tend to gravitate towards int.TryParse for the performance reasons.
Upvotes: 0