The online racing simulator
VB6 UDT padding and copying hell
This is making no sense and I'm drunk and tired too (which isn't helping).

If I have the following code:

Public Type Something
Lots of variables....
End Type

Public SomethingArray() As Something
Public SomethingOnce As Something

Public Sub SomethingElse

'populate SomethingArray here...

SomethingOnce = SomethingArray(x)

End Sub

The problem I'm having is that SomethingOnce does not match with SomethingArray(x)... I'm getting loads of data type misalignments. Which is silly, as whatever auto-padding VB6 adds should be identical, so there should be no problems copying the data. Only it doesn't work (note that in my real code, Something is a fairly complex set of some data types, other UDTs, and arrays of UDTs). What is really curious is that changing the structure of the UDTs that Something is made from (by manually adding padding bytes) affects the data misalignments. I was hoping to find the right padding which would cause the problem to go away but I've had no luck.

This problem seemed to come out of the blue, the copy worked fine until I added an extra UDT to Something, and now everything has gone to pot. I just don't understand why.

Any ideas?
#2 - Stuff
You might try the Let or LSet statements VB6 has. Probably Let because they are both of Something udt. If those still don't work, you could try the CopyMemory API to poke the memory into shape.
#3 - ciph
Quote from Bob Smith :This is making no sense and I'm drunk and tired too (which isn't helping).

If I have the following code:

Public Type Something
Lots of variables....
End Type

Public SomethingArray() As Something
Public SomethingOnce As Something

Public Sub SomethingElse

'populate SomethingArray here...

SomethingOnce = SomethingArray(x)

End Sub

The problem I'm having is that SomethingOnce does not match with SomethingArray(x)... I'm getting loads of data type misalignments. Which is silly, as whatever auto-padding VB6 adds should be identical, so there should be no problems copying the data. Only it doesn't work (note that in my real code, Something is a fairly complex set of some data types, other UDTs, and arrays of UDTs). What is really curious is that changing the structure of the UDTs that Something is made from (by manually adding padding bytes) affects the data misalignments. I was hoping to find the right padding which would cause the problem to go away but I've had no luck.

This problem seemed to come out of the blue, the copy worked fine until I added an extra UDT to Something, and now everything has gone to pot. I just don't understand why.

Any ideas?

maybe u tell us what variable type "Something" is in real since vb6 dont have Something? Is it String, Integer, Boolean, Long ?

If you tell me the format i can maybe help you out on the memory problem
#4 - Woz
I would advise use a class over UDTs in VB. The class wire up takes more work but you gain later down the line.

UDTs in VB suffer all forms of problems, passing and boundary conditions. They are also value based so passing them around results in large memory copies. Also if used in a with block can cause all maner of locking issues.

Finally they are Impossible to pass through a COM boundary as they are not a valid COM type. This means you are forced into a single executable or have all the logic that uses them in one object.

What are you trying to do because your that follows will take a copy so if you change SomethingOnce it will not change SomethingArray(x) unless you copy back.

SomethingOnce = SomethingArray(x)

I think I have that right, not touched VB in a year now so some of the subtle bits might be fuzzy.
Thanks for the tips, I'll have a play. TBH I'm only copying the array element for ease of access, I'm not intending to change anything within it after the copy, I should probably do things properly and just read from the damn array directly but I'm not in the mood to change that much code if there's an easier solution.
#6 - ciph
Anyways you can also try CopyMemory. To get the Pointer use VarPtr()

Should look like this:

CopyMemory SomethingOnce, ByVal VarPtr(SomethingArray(X)), Len(SomethingArray(X))
Aha, I was being retarded, a little more digging shows that I was still loading one of the old data structures into memory from a file, which was obviously not working right. :doh: All fixed now though, and those tips could come in handy later.
#8 - ciph
No problem, if you need help in VB6 feel free to add me on MSN or PM me. Im mastering VB since 1995

FGED GREDG RDFGDR GSFDG