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

前端 未结 7 1576
我在风中等你
我在风中等你 2021-01-20 15:07

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

相关标签:
7条回答
  • 2021-01-20 15:38

    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.

    0 讨论(0)
  • 2021-01-20 15:39

    For ASP.NET Unit:

    unit.IsEmpty ? 0 : Convert.ToInt32(unit.Value);
    
    0 讨论(0)
  • 2021-01-20 15:39

    Probably he need this:

     int myInt = 1;
     uint myUint = (uint)myInt;
    
     uint myUint = 1;
     int myInt = (int)myUint;
    
    0 讨论(0)
  • 2021-01-20 15:41
    Convert.Toint32( UInt );
    

    I guess u meant UInt not Unit

    EDIT : Ok thought you meant uint sorry

    0 讨论(0)
  • 2021-01-20 15:47

    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)

    0 讨论(0)
  • 2021-01-20 15:53

    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.

    0 讨论(0)
提交回复
热议问题