How do I translate VB.Net's CType() to C#

前端 未结 4 1515
南方客
南方客 2020-12-20 15:50

I Have this code segment in VB NET:

CType(pbImageHolder.Image, Bitmap).SetPixel(curPoint.X, curPoint.Y, Color.Purple)

what is

相关标签:
4条回答
  • 2020-12-20 15:59

    Hi this is the code after conversion VB to C# code:

    ((Bitmap)pbImageHolder.Image).SetPixel(curPoint.X, curPoint.Y, Color.Purple);
    

    And if you want code conversion from VB to C# and vice verse go through the following link: http://www.developerfusion.com/tools/convert/vb-to-csharp/

    0 讨论(0)
  • 2020-12-20 16:01

    In VB.Net CType(object, type) casts an object to a specific type.

    There are two ways to accomplish this in C#:

    Bitmap image = pbImageHolder.Image as Bitmap;
    image.SetPixel(curPoint.X, curPoint.Y, Color.Purple);
    

    or

    Bitmap image = (Bitmap)(pbImageHolder.Image);
    image.SetPixel(curPoint.X, curPoint.Y, Color.Purple);
    
    0 讨论(0)
  • 2020-12-20 16:18
    ((Bitmap)pbImageHolder.Image).SetPixel(curPoint.X, curPoint.Y, Color.Purple)
    
    0 讨论(0)
  • 2020-12-20 16:21

    I'm surprised that none of the answers on this subject are correct so I decided to post here. You can verify what is going on by decompiling a VB.NET program. The answer depends on the type being converted to.

    For reference types:

    Dim value = CType(x, ReferenceType)

    var value = (ReferenceType)x;

    For structs:

    Dim value = CType(x, StructType)

    var value = (x != null) ? ((StructType)x) : default(StructType);

    For predefined casts:

    Dim value = CDbl(x)

    var value = Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(x)

    Which looks like this:

    internal static double ToDouble(object Value, NumberFormatInfo NumberFormat)
    {
        if (Value == null)
        {
            return 0.0;
        }
        IConvertible convertible = Value as IConvertible;
        if (convertible != null)
        {
            switch (convertible.GetTypeCode())
            {
            case TypeCode.Boolean:
                if (Value is bool)
                {
                    return 0 - (((bool)Value) ? 1 : 0);
                }
                return 0 - (convertible.ToBoolean(null) ? 1 : 0);
            case TypeCode.SByte:
                if (Value is sbyte)
                {
                    return (sbyte)Value;
                }
                return convertible.ToSByte(null);
            case TypeCode.Byte:
                if (Value is byte)
                {
                    return (int)(byte)Value;
                }
                return (int)convertible.ToByte(null);
            case TypeCode.Int16:
                if (Value is short)
                {
                    return (short)Value;
                }
                return convertible.ToInt16(null);
            case TypeCode.UInt16:
                if (Value is ushort)
                {
                    return (int)(ushort)Value;
                }
                return (int)convertible.ToUInt16(null);
            case TypeCode.Int32:
                if (Value is int)
                {
                    return (int)Value;
                }
                return convertible.ToInt32(null);
            case TypeCode.UInt32:
                if (!(Value is uint))
                {
                    return convertible.ToUInt32(null);
                }
                return (uint)Value;
            case TypeCode.Int64:
                if (Value is long)
                {
                    return (long)Value;
                }
                return convertible.ToInt64(null);
            case TypeCode.UInt64:
                if (!(Value is ulong))
                {
                    return convertible.ToUInt64(null);
                }
                return (ulong)Value;
            case TypeCode.Decimal:
                if (Value is decimal)
                {
                    return convertible.ToDouble(null);
                }
                return Convert.ToDouble(convertible.ToDecimal(null));
            case TypeCode.Single:
                if (Value is float)
                {
                    return (float)Value;
                }
                return convertible.ToSingle(null);
            case TypeCode.Double:
                if (Value is double)
                {
                    return (double)Value;
                }
                return convertible.ToDouble(null);
            case TypeCode.String:
                return ToDouble(convertible.ToString(null), NumberFormat);
            }
        }
        throw new InvalidCastException(Utils.GetResourceString("InvalidCast_FromTo", Utils.VBFriendlyName(Value), "Double"));
    }
    
    0 讨论(0)
提交回复
热议问题