tags:

views:

453

answers:

7

How can I convert from an ASP.NET Unit structure to int in c#? Or reverse?

A: 
Convert.Toint32( UInt );

I guess u meant UInt not Unit

EDIT : Ok thought you meant uint sorry

KroaX
Just keep in mind that there is a big difference between UInt and Int32. When converting them you might end up with a wrong result (if the UInt exceeds 2147483647).
Bastiaan Linders
Thats always the risk when converting different numbers
KroaX
@KroaX: Not when converting Int32 to Int64 or short to long etc. ;)
Bastiaan Linders
Of course , Int64 and long has enough bytes to inherit Int32
KroaX
A: 

Probably he need this:

 int myInt = 1;
 uint myUint = (uint)myInt;

 uint myUint = 1;
 int myInt = (int)myUint;
Vasiliy Borovyak
Your code will not work properly in many cases ... You need to use the Convert Object to convert from int to Uint and reverse
KroaX
A: 

Use Unit.Value property. It will return double and you can cast it to int

Something like (int)xyz.Value

WEhere xyz is the unit variable

To convert int to unit use new Unit(value)

Midhat
+2  A: 

The Unit type has a Value property. This is a double, but you can cast it to an int if you want. The casting may cause a loss of precision, but you are probably aware of that.

To create a Unit just use the constructor that takes an int.

Rune Grimstad
+2  A: 
Chris S
A: 

For ASP.NET Unit:

unit.IsEmpty ? 0 : Convert.ToInt32(unit.Value);
UserControl
A: 

The Value property returns a dobule, that you can convert to an integer:

int h = (int)someControl.Height.Value;

However, the conversion might not make sense for some unit types. If you don't know for certain that the unit is of a specific type, you would want to check the Type property first.

Guffa