How can I convert from an ASP.NET Unit structure to int in c#? Or reverse?
-
What unit? do you mean the ASP.NET Unit class?Chris S– Chris S2010-05-18 08:45:08 +00:00Commented May 18, 2010 at 8:45
-
1(it's a structure) msdn.microsoft.com/en-us/library/…Chris S– Chris S2010-05-18 08:45:56 +00:00Commented May 18, 2010 at 8:45
-
asp.net unit like webcontrol.heightkartal– kartal2010-05-18 08:46:31 +00:00Commented May 18, 2010 at 8:46
7 Answers
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.
Comments
If you mean the Unit class:
The Unit class can represent values only between -32768 and 32767.
But it depends if you want the Pixel or Percentage value.
myUnit.Valuewill get the value as pointed out.- Use the constructor
public Unit(int value)to convert back.
If you mean a uint: there's 2 possible obvious ways:
int n = Convert.ToInt32(myUint);
int n = (int)myUint;
Comments
Probably he need this:
int myInt = 1;
uint myUint = (uint)myInt;
uint myUint = 1;
int myInt = (int)myUint;
1 Comment
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.
Comments
Convert.Toint32( UInt );
I guess u meant UInt not Unit
EDIT : Ok thought you meant uint sorry