views:

498

answers:

2

I have this bit of code that is being converted from vb6 to vb.net. I need to know what LenB is doing in this bit of code.

Dim singleValue As Single 'var for conversion use(4byte -> 1single)'
Dim bytes() As Byte
Dim valueB() As Byte 'this gets set elsewhere and is redim-d to its size'

For n = 0 To CDbl(ItemNumberCombo.Text) - 1
 'bytes() -> single'
 'UPGRADE_ISSUE: LenB function is not supported.'
 ReDim bytes(LenB(singleValue) - 1)
 bytes(3) = valueB(n * 4)
 bytes(2) = valueB(n * 4 + 1)
 bytes(1) = valueB(n * 4 + 2)
 bytes(0) = valueB(n * 4 + 3)
 'UPGRADE_ISSUE: LenB function is not supported.'
 'UPGRADE_ISSUE: VarPtr function is not supported. '
 Call memcpy(VarPtr(singleValue), VarPtr(bytes(0)), LenB(singleValue))
 'display the result'
 DText(n).Text = VB6.Format(singleValue, "0.000000E+00") 'CStr(singleValue)'
 If DataSaveCheckBox.CheckState = 1 And FileNameText.Text <> "" Then
  csvOutput = csvOutput & DText(n).Text & ","
 End If
Next n

Am I right in thinking that bytes is always ReDim'ed to the same size? By the looks of it 4 elements.

Why then use LenB to ReDim if you could just use a number? And why ReDim in the loop at all?

+3  A: 

Trying to explain bad code... is just an effort in futility. Interesting way to fill a Single by loading a byte array.

The LenB function gives you the length in bytes of a variable. Yes it will always return 4 when passed a Single variable type.

My guess for the redim is so the array gets initialized instead of preserved. But since it then assigns all 4 bytes, it isn't technically needed and is probably just defensive programming. Defensive programming might also explain the LenB. In case Single changes size in the future.

Will Rickards
I get the funny feeling a C programmer was forced to use VB6. ;-)
Mike Spross
Actually, looking at it again, looks like they even aliased RtlMoveMemory to "memcpy". Oh, they were really missing C that day :-P
Mike Spross
LOL! All VB programmers know Bruce McKinney christened it CopyMemory and no other name can ever be used, or it won't work
MarkJ
+3  A: 
Joel Coehoorn
For scalar types, Len and LenB both return the size of the data. In this case, if you pass a Single to LenB, it will return 4 (a Single occupies 4 bytes of memory). It looks like the code is trying to reconstitute Singles from an array of bytes.
Mike Spross
"length in bytes of a string, regardless of things like character encoding" - that does not compute. A string only has a length of actual *characters* if you don't regard encoding. Also, wasn't the question what LenB() does on *other* types?
bzlm
@bzlm: the individual characters that make up that string may not all occupy the same number of bytes. The string "dog" may be more than 3 bytes long for many encodings.
Joel Coehoorn
Updated wording to please the critics
Joel Coehoorn
+1. Mmmm...sample code :-)
Mike Spross
+1 will check it out at work tomorrow and make sure it works before I accept it :) Not that I don't trust the correctness just want to make sure it fits with the rest of the code.
Tanj
Don't forget to adjust for the stringbuilder
Joel Coehoorn