CType in C#?
-
Is there any function equivalent to the VB function CType (it converts an expression into a type) in C#? - monrobot13
-
Is there any function equivalent to the VB function CType (it converts an expression into a type) in C#? - monrobot13
Have you tried using the static class with Convert.ChangeType?
-
Is there any function equivalent to the VB function CType (it converts an expression into a type) in C#? - monrobot13
If all you want to do is convert say an object of type "A" in to an object of type "B" (assuming the conversion is valid), then you can just cast like this: A a1 = new A(); B b1 = (A)a1;
-
Is there any function equivalent to the VB function CType (it converts an expression into a type) in C#? - monrobot13
Hi, Activator.CreateInstance Method[^] Might be what you're looking for. Andres Manggini. Buenos Aires - Argentina.
-
Is there any function equivalent to the VB function CType (it converts an expression into a type) in C#? - monrobot13
Hi there,
Your problem seems very simple and hence many quick answers are there. But I would elaborate more on ur requirement.
Changing an Expression to a type could be of 2 types...
1. "1" as a String to 1 as an Integer(int)
2. "a" as a String to 97 as an integer(int).
3. obj as an Object to a Specific type like "Customer"
You need to find out ur requirement and accordingly use the Solutions given to u.
Answer 1.: UseConvert.ToInt32
orInt32.Parse
functions.
Answer 2.: Use a typecast i.e.(int)num1
Answer 3.: Use a typeCast i.e.(Customer)obj
or UseConvert.ChangeType
function.I hope this helps.
regards
Atul p.s.:Convert.ChangeType
could be the sole solution sometimes. Atul Kale MCSD, MCT Sr. Software Engineer XcelVision Technologies Ltd. -
Is there any function equivalent to the VB function CType (it converts an expression into a type) in C#? - monrobot13
Thank you for all the replies, but unfortunately none are really what I can use. Here is basically what I'm trying to do. I take two variables and divide them, then I multiply by another variable. The division usually yeilds a number below zero so multiplying still gives me zero. Now with the CType function you can convert the expression to an int and it will round the number up to one instead of zero. Like this for example:
[Visual Basic]
// var1 = 7 [int]
// var2 = 10 [int]
var3 = CType ((var1/var2), Integer)
// var3 [int] will equal 1, instead of 0The variables mentioned above are all ints and changing their type is not an option. I guess what I really need is something that will round an expression to the nearest whole number. - monrobot13
-
Thank you for all the replies, but unfortunately none are really what I can use. Here is basically what I'm trying to do. I take two variables and divide them, then I multiply by another variable. The division usually yeilds a number below zero so multiplying still gives me zero. Now with the CType function you can convert the expression to an int and it will round the number up to one instead of zero. Like this for example:
[Visual Basic]
// var1 = 7 [int]
// var2 = 10 [int]
var3 = CType ((var1/var2), Integer)
// var3 [int] will equal 1, instead of 0The variables mentioned above are all ints and changing their type is not an option. I guess what I really need is something that will round an expression to the nearest whole number. - monrobot13
I was just wondering, the solution to ur problem is so Basic, in fact u must understand here, that u did not phrase ur question properly! U were just asking for ways to Convert the data.
Its a simple logic that while doing such operations, U never store the intermediate results in variables.
int num1 = 6, num2 = 10, num3 = 5, result; here if u want, (num1 / num2) * num3 which yields (6/10) * 5 = 0.6 * 5
Then boss, you should not store num1/num2 in an int variable. That is ur problem.
Do the Direct calculation,
result = (num1 / num2) * num3;
or,
use an intermediate double or float variable it u still wanna store the division.
double div = num1/ num2;
result = div * num3;
Sometimes one should just concenrate on the basics first and not just jump into complexities.
Atul Kale MCSD, MCT Sr. Software Engineer XcelVision Technologies Ltd.