Thursday, January 31, 2008

VB6 -> VB.NET Upgrade Wizard, Fixed Width Structures

I used the upgrade wizard included in Visual Studio.NET and found it overall pretty good. My biggest issue with it was the way it handled structures with fixed width fields. In VB you'd define a structure like this:

Type example
field1 As String * 10
field2 As String * 10
End Type


In VB.NET it would get converted as:

Structure example
<VBFixedString(10), _
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=10)> _
Public field1 As String
<VBFixedString(10), _
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=10)> _
Public field2 As String
End Structure


So it looks like the wizard did the trick by adding all those attributes. I thought that this was adequate until we realized during testing that we weren't getting properly padded fields out of the structure like we had been in VB6. So if we set field1 to "hello" in VB6, then retrieved the value, we'd get "hello     " (thats 5 trailing spaces). In VB.NET, the same value in field1 would be retrieved as "hello" with no spaces. I'd bet that that fancy pants attributes actually does its job when used as a library from another COM app. But in just a normal capacity it did not behave expected.

No problem, we'll just have to try and fix it! So my first thought is that maybe there is some sort of .NET library to redirect all member access to a function. Sort of like __getattr__ in python. We could conceivably just change the Structure to a Class, and inherit it from a class that implements __getattr__ and we wouldn't have to change any code.

No dice, as far as I can tell there is nothing in .NET to bounce member access to a function. It would have been nice to have some sort of class that would provide the bounce for us, then we just override the member resolution function or something.

So what I did instead was turn those Structures to Classes anyways, and had them all inherit from a class implementing this property and method:

Default Public ReadOnly Property GetField(ByVal retrievethisfield As String) As String
Get
Return _GetField(retrievethisfield)
End Get
End Property

Public Function _GetField(ByVal fieldname As String) As String
Dim field As Reflection.FieldInfo = Me.GetType().GetField(fieldname)
Dim val As Object = field.GetValue(Me)
Dim fixedlength As Integer
For Each attr As Attribute In field.GetCustomAttributes(True)
If TypeOf attr Is VBFixedStringAttribute Then
fixedlength = CType(attr, VBFixedStringAttribute).Length
Return Left(CStr(val).PadRight(fixedlength, " "), fixedlength)
End If
Next
Return val
End Function


So the dirty part is that to set you use normal member lookup via obj.field1 etc, but to do a get you do obj("field1") and it will do the padding for you (Note the use of the default property...). You can get rid of some of those wizard attributes, as long as you use VBFixed. Ugly huh! What would be nice is to be able to then tag the member variables in the structure/class as writeonly so I could find (as errors in the task list) all the gets and replace them with my lookup. This is possible for properties, but not for member variables because they must be second class citizens.

Sometimes I feel like VB.NET has some python like things I like (specifically invoking unknown methods on an 'object' object). Other times I realize its a totally different animal and I can find comparable things between any language if I wanted to.

Tuesday, January 29, 2008

Superbowl Box Pool Odds on Point Change

my dad sent me this link today:

http://www.j-e-f-f.com/sb-o-m.htm


it details the percentage payout per quarter that each superbowl pool box has earned in all superbowls. since we play a pool on point change, this link doesn't actually tell us much.

so i went to superbowl.com for stats and made a quick program to come up with this chart that gives us the percentage of hits on each box. here it is:


0 1 2 3 4 5 6 7 8 9
0 10.69 2.24 1.21 8.79 5 1.72 7.59 8.45 1.21 2.76
1
0.17 0.34 0.86 0.86 0.34 1.03 1.38 0.17 0.52
2

0 1.21 0.34 0 1.03 1.38 0.17 0.52
3


3.62 2.76 0.17 4.83 5.52 0.17 1.55
4



0.69 0.52 1.55 3.1 0 1.9
5




0 0.17 0.52 0 0.17
6





1.21 3.45 0.34 0.86
7






2.93 0.86 1.9
8







0.17 0.69
9








0.34


When taking AFC & NFC into consideration, this is the chart. NFC is the columns going across, AFC is the rows going down.



0 1 2 3 4 5 6 7 8 9
0 10.69 1.55 0.69 4.66 2.93 1.21 3.97 4.48 1.03 1.55
1 0.69 0.17 0.34 0.69 0.69 0.17 0.69 0.86 0.17 0.34
2 0.52 0 0 0.52 0.34 0 0.52 0.69 0 0.34
3 4.14 0.17 0.69 3.62 1.03 0 2.59 2.76 0.17 1.38
4 2.07 0.17 0 1.72 0.69 0 1.03 2.07 0 1.03
5 0.52 0.17 0 0.17 0.52 0 0.17 0.34 0 0.17
6 3.62 0.34 0.52 2.24 0.52 0 1.21 1.72 0.34 0.34
7 3.97 0.52 0.69 2.76 1.03 0.17 1.72 2.93 0.34 0.86
8 0.17 0 0.17 0 0 0 0 0.52 0.17 0.52
9 1.21 0.17 0.17 0.17 0.86 0 0.52 1.03 0.17 0.34


and likewise, here are the expected final scores when afc/nfc is not taken into account:


0 1 2 3 4 5 6 7 8 9
0 0 2.44 2.44 2.44 0 4.88 4.88 7.32 0 4.88
1
0 0 0 4.88 4.88 2.44 2.44 2.44 2.44
2

0 0 2.44 0 0 2.44 0 2.44
3


0 4.88 0 4.88 2.44 0 0
4



0 0 0 9.76 0 2.44
5




0 0 0 0 0
6





2.44 2.44 2.44 2.44
7






4.88 0 2.44
8







0 2.44
9








0

and when afc & nfc are taken into account (afc is rows again)


0 1 2 3 4 5 6 7 8 9
0 0 0 2.44 0 0 4.88 2.44 4.88 0 2.44
1 2.44 0 0 0 2.44 2.44 2.44 2.44 2.44 2.44
2 0 0 0 0 2.44 0 0 0 0 2.44
3 2.44 0 0 0 2.44 0 0 2.44 0 0
4 0 2.44 0 2.44 0 0 0 9.76 0 2.44
5 0 2.44 0 0 0 0 0 0 0 0
6 2.44 0 0 4.88 0 0 2.44 2.44 2.44 2.44
7 2.44 0 2.44 0 0 0 0 4.88 0 0
8 0 0 0 0 0 0 0 0 0 2.44
9 2.44 0 0 0 0 0 0 2.44 0 0

First Post!

yes, i've created a blog using the power of the internet.