简体   繁体   中英

Convert System.Web.UI.WebControls.Unit to int in c#

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

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 .

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.Value will get the value as pointed out.
  • Use theconstructor 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;
 
 
 

对于 ASP.NET 单元:

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

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)

Probably he need this:

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

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

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.

Convert.Toint32( UInt );

I guess u meant UInt not Unit

EDIT : Ok thought you meant uint sorry

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM