SetValue Not Working as Advertised


New member
Sep 26, 2004
Programming Experience
It would seem the SetValue function is not working as advertised.
Create a project and drop a Command button on it
Then past the following into the code and run it. If it works for you, I would like to know what I'm doing wrong???

Thanks in advance for any help!

If I can get this to work, I will be posting an ultracool upgrade to the BinaryReader class that lets you read structures from a stream using fully managed code.
[font=Courier New]Public Structure MyStruct[/font]
[font=Courier New]Public aByte As Byte[/font]
[font=Courier New]Public aShort As Short[/font]
[font=Courier New]Public anInt As Integer[/font]
[font=Courier New]End Structure[/font]

[font=Courier New]Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _[/font]
[font=Courier New]Handles Button1.Click[/font]
[font=Courier New]Dim obj As New MyStruct[/font]
[font=Courier New]Dim fld As System.Reflection.FieldInfo[/font]
[font=Courier New]For Each fld In obj.GetType.GetFields[/font]
[font=Courier New]	 Select Case fld.FieldType.Name[/font]
[font=Courier New]		 Case Is = "Byte"[/font]
[font=Courier New]			Console.WriteLine("Found a Byte Field. Assigning the value 100 to it.")[/font]
[font=Courier New]			fld.SetValue(obj, System.Convert.ToByte(100))[/font]
[font=Courier New]	 End Select[/font]
[font=Courier New]Next fld[/font]
[font=Courier New]Console.WriteLine("obj.aByte should be equal to 100, but...")[/font]
[font=Courier New]Console.WriteLine("obj.aByte=" & obj.aByte.ToString)[/font]
[font=Courier New]End Sub[/font]

My output says that aByte is still 0....
Talk about things that make you go ARRRRRRGH!