So what happened to += in VB?

Started by
16 comments, last by Unwise owl 20 years ago


http://www.woodmann.net/crackz/Tutorials/Vbpcode.htm
Advertisement
http://vbnet.mvps.org/index.html?http://vbnet.mvps.org/dev/vb6/vb6faq.htm
afterburn, you can log in if you want. Also, you should check the dates on those pages.

"Sneftel is correct, if rather vulgar." --Flarelocke
Sorry just noticed the .TargetSystem which means you''re using Withs already and you cant use nested Withs, so the second option of using object reference variables (pointers) might be better, but only if you''re using objects rather than udts.

Another option is to stop using VB Integers/Longs for properties like Missiles and use your own home-grown integer/long class which supports functions like Increment, e.g.

''class cLong
''Value can be declared as User Interface Default for the object
Public Value As Long

Public Sub Inc(i As Long)
Value = Value + i
End Sub

Public Sub Dec(i As Long)
Value = Value - i
End Sub


Then use:
udtPlayers(intOldOwner).VisibleStars(.TargetSystem).Missiles.Inc intMissiles

This should run faster than your original code:

udtPlayers(intOldOwner).VisibleStars(.TargetSystem).Missiles = udtPlayers(intOldOwner).VisibleStars(.TargetSystem).Missiles + intMissiles

it wouldn''t run faster than using Withs or object references because of the extra overhead of a class rather than an integer, and it would have extra memory overhead. These sacrifices might be worth it for better readability/maintainability.
I''ve never looked (no point really), but I have a feeling these 2 statements compile into the exact same machine code:

x = x + 1
x++;

Using home grown integer/long classes with Inc or Dec functions is similar to Extrarius''s idea of passing parameters by reference to generic AddInt functions. However, from experience, I wouldn''t recommend passing standard types (integers, longs) by reference - I had a lot of problems porting such code to Java recently, and I''m not sure whether VB.net continues to support this (since in some ways VB.net is more similar to Java/C# than VB5/6).
quote:Original post by afterburn
VB is an Opcode language.


Everything is an opcode language, they''re just different for different systems. VB has both P-Code & native compilation, AFAIK MS made native compilation default for VB6 ... leading me to wonder why they even bothered to stick with the P-Code option.
quote:Original post by Anonymous Poster
I''ve never looked (no point really), but I have a feeling these 2 statements compile into the exact same machine code:

x = x + 1
x++;



Eh, technically they should if the compiler is at least halfway decent, but the actual meaning of these two lines is different. The first is saying "take ''x'', add ''1'' to it, and store the result in ''x''." The second is saying "take ''x'', increment it, store the result back in there."

Any compiler worth its salt would optimize this out, but the first case would imply loading ''x'' twice, I believe.

Not that it matters anyways.

This topic is closed to new replies.

Advertisement