Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Unwise owl

So what happened to += in VB?

This topic is 5203 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In C++ there are practical combined assignment and arithmetic operators like +=, -=, *=, and /=. It means that instead of writing x = x + y, one can instead write x += y and have y added to x without major trouble. This might seem like a minor issue, but in my latest game which is a clone of a strategy game from 1992, there are lots of cases where I have to add or subtract values from their variables because of the huge amount of numbercrunching. Those assignments lines in VB6 tend to become pretty when indices have to be made in several arrays, like: (in the code I was currently working on when I thought of this about eight of these statements for different units appear right fter each other) udtPlayers(intOldOwner).VisibleStars(.TargetSystem).Missiles = udtPlayers(intOldOwner).VisibleStars(.TargetSystem).Missiles + intMissiles In Visual Basic I have to cut statements like these in two lines at best, several lines in the absolute worst cases. A combined assignment and arithmetic operator would have helped me immensely to cut down on code length and improve readability. I can't see any downfall with such operators, and I would like to know if Microsoft had anything specific in mind when they left them out. If anyone has the slightest idea about this please enlight me. EDIT: Example was faulty, and I corrected it. [edited by - Unwise owl on April 16, 2004 1:33:14 PM]

Share this post


Link to post
Share on other sites
Advertisement
Simple: VB != C/C++

They''ve been left out for the same reason that the conditional "if" instruction has to contain a mandatory "then" after the condition (despite such a thing being completely unecessary) or that "for" loops are so limited (compare them to C-style loops: for(initial condition, exit condition, action taken every iteration), versus for variable = start value to end value [step increment]). Because VB just isn''t C.

Share this post


Link to post
Share on other sites
VB = Visual BASIC. Adding additional operators would confuse things making it less than BASIC. For singly incrementing / decrementing values you could use the inc() and dec() functions if I remember correctly, other than that you have to deal with the longer winded value = value + increment_value or go use C++ instead.

Share this post


Link to post
Share on other sites
VB is an Opcode language. Meaning everything is ran against a DLL that really does all the work, Opcodes can not handle this type of structure as it would require independant assignments.

Although you would be glad to know that VB.net has all that and bit shift oprands.

--
What are you nutz?

I have nothing to say to your unevolved little brain. The more I say gives you more weapons to ask stupid questions.

Share this post


Link to post
Share on other sites
I won't afford Microsoft's .NET stuff within the nearest two years, and as additional strengthening of this argument I can say I switched to a friend's outdated VB6 disk from VB5 merely a few weeks ago. I am generally one version behind in the technology race because of the money issue (neither am I too keen on free compilers or "Learning Editions"; I've had hell trying to convert a VC++5 project in DirectX to DevCPP and I don't like the restrictions of those cheap versions). Still, what you speak of brings hope...

[edited by - Unwise owl on April 16, 2004 1:35:35 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by afterburn
VB is an Opcode language. Meaning everything is ran against a DLL that really does all the work, Opcodes can not handle this type of structure as it would require independant assignments.




Wow, wrong in so many different ways. Assuming any version of VB made in recent memory still used bytecode instead of compiling to binary (which they don''t), you still have the issue that your statements about "opcodes" and "independant assignments" are total rubbish.

Share this post


Link to post
Share on other sites
VB has a way to pass parameters by reference, right? Then you could make a function like AddInt(byref a as integer, byval b as integer) a = a + b {with whatever func/endfunc etc you''d need around it} and then just use that instead of +=. The only downside is that it would be a prefix operator instead of infix and you''ll have to make one for all the variable types you nead.

Share this post


Link to post
Share on other sites
Just one more thing, for better runtime performance, and sometimes less typing and better readability, in VB5/6 you can do something like:

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

'or if VisibleStars is a collection of objects of say class cStar
'(this wont work with user defined types since variable
' assignment is a copy rather than a reference)

Dim oStar As cStar
oStar = udtPlayers(intOldOwner).VisibleStars(.TargetSystem)
oStar.Missiles = oStar.Missiles + intMissiles

Edit:
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.

[edited by - abstractworlds on April 16, 2004 1:58:51 PM]

Share this post


Link to post
Share on other sites
Actually I do use With already in my example (.TargetSystem), the problem is when you got about five simultaneous arrays in which you carry out operations in a non-sequential order. Typing new Withs and End Withs would cause more pain than it's worth. And that single With can't get rid of most of the excessive typing (or more often copying with minor modifications that is needed). Sometimes I feel I would need to make separate function for a lot of stuff, but then there is always some detail that is different and prevents logicial organization of the code into a function.


[edited by - Unwise owl on April 16, 2004 2:00:33 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!