How do i change property value in vb.net

2019-08-18 07:38发布

i would like to assign my array vals to properties of my object.

like:

    For i = 1 To 32
        myClass.Prop_i = val[i]
    Next

3条回答
干净又极端
2楼-- · 2019-08-18 08:00

VB.NET isn't a dynamic language: you can't do such things.

Since VB.NET doesn't have a "dynamic" keyword like C#, your option is reflection:

myClass.GetType().GetProperty("Prop_" + i.ToString()).SetValue(myClass, val[i], null);

But if you're more explicit with your problem maybe there's a more elegant solution than reflection ;)

查看更多
来,给爷笑一个
3楼-- · 2019-08-18 08:04

If you are willing to write some code in C# and use it in VB.NET, and need to store primitive types like int, float or byte, and all your properties are of the same type. Then you can create a union structure with an array covering the fields.

Then you can use code like this:

Sub Main()  ' vb.net

    Dim bag As New PropertyBag()
    bag.AllProperties = New Single() {1, 2, 3, 4, 5, 6, 7, 8}
    Dim three As Single = bag.Prop_3    'returns 3
    Dim five As Single = bag(4)         'returns 5 (0-based index)

End Sub

When declared like

[StructLayout(LayoutKind.Explicit, Size=Size)]
public unsafe struct PropertyBag
{
    const int Count = 8;    //8 fields
    const int Size = 8 * 4; //4 bytes per field

    [FieldOffset(0)]
    fixed float list[Count];

    [FieldOffset(0)]        float x1;
    [FieldOffset(4)]        float x2;
    [FieldOffset(8)]        float x3;
    [FieldOffset(12)]        float x4;
    [FieldOffset(16)]        float x5;
    [FieldOffset(20)]        float x6;
    [FieldOffset(24)]        float x7;
    [FieldOffset(28)]        float x8;        

    public float Prop_1 { get { return x1; } set { x1 = value; } }
    public float Prop_2 { get { return x2; } set { x2 = value; } }
    public float Prop_3 { get { return x3; } set { x3 = value; } }
    public float Prop_4 { get { return x4; } set { x4 = value; } }
    public float Prop_5 { get { return x5; } set { x5 = value; } }
    public float Prop_6 { get { return x6; } set { x6 = value; } }
    public float Prop_7 { get { return x7; } set { x7 = value; } }
    public float Prop_8 { get { return x8; } set { x8 = value; } }

    public float this[int index]
    {
        get
        {
            fixed (float* ptr = list)
            {
                return ptr[index];
            }
        }
        set
        {
            fixed (float* ptr = list)
            {
                ptr[index] = value;
            }
        }
    }

    public float[] AllProperties
    {
        get
        {
            float[] res = new float[Count];
            fixed (float* ptr = list)
            {
                for (int i = 0; i < Count; i++)
                {
                    res[i] = ptr[i];
                }
            }
            return res;
        }
        set
        {
            fixed (float* ptr = list)
            {
                for (int i = 0; i < Count; i++)
                {
                    ptr[i] = value[i];
                }
            }
        }
    }
}

Note that reflection should work in your case (like others have answered), but this is just a different approach to the problem (and a very fast one too). The main limitation is what types can be made into pointers in C# (sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool)

查看更多
走好不送
4楼-- · 2019-08-18 08:20

Your property needs to define Set. This will allow you to modify the property.

查看更多
登录 后发表回答