Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi experts, i wanna ask you about the best way convert object to integer. This is my trick :) :

C#
int value = ((IConvertible)ExecuteScalarQuery(partID, sid)).Int32(null));


Is there any trick that certainly better than my trick experts.. ?? :confused:
Posted
Comments
CPallini 27-Aug-10 5:20am    
What is 'ExecuteScalarQuery'?
Teamsar Muliadi 27-Aug-10 5:37am    
ExecuteScalarQuery is a method that return an object as value.

Looks messy. I always call ToString and use int.TryParse, so that if it's not an integer, no exception is thrown. But ExecuteScalar returns an int, so why does this method not return one ?
 
Share this answer
 
Comments
Teamsar Muliadi 27-Aug-10 5:11am    
Mmm.. are u sure it is more faster than my trick in convert an object to int expert :confuse: ??
Christian Graus 27-Aug-10 5:31am    
It's DEFINITELY faster if your object is not an int, as an exception is avoided. If it IS an int, why does your method not just return an int ?
Teamsar Muliadi 27-Aug-10 5:41am    
DEFINITELY faster ?? wooww.. That sound seems good.. :) Mmm.. ok.. i will try as u tell me.. Thank you. :)
You can try this

int value= (int)<object value="">;
or
int value=Convert.ToInt32(<object value="">);
 
Share this answer
 
<pre lang="cs">int test = int.Parse(string.Format("{0}", myobject))

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900