Can I sum class values from diferent classes?Dear

I have a Class like this

PublicClass Item

Public Quantity1AsInteger

Public Quantity2AsInteger

.....

PublicQuantity20AsInteger

End Class

And two object like:

DimItem1AsItem

DimItem2AsItem

Now, I need to sum all the Quantity's, from 1 to 20. Can I do something like

DimItemSumAsItem

ItemSum = Item1 + Item2

I know I can make a function to sum all "Item" variables, BUT, how can I sum any class variables?

Is there something like this code?

For EachItemSum.Item"n"In {Item1, Item2, ... Item"n"}

ItemSum = Item1 + Item2 + ... + Item"n"

[2518 byte] By [LucasPasquali] at [2007-12-28]
# 1

You could try the following

Dim sum = Item1

For Each cur As Item In {Item2, Item3,...ItemN}

sum += cur

Next

JaredParsonsMSFT at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
I think the orginal question was about operator overloading, which is not available in .net 2003, I don't know about 2005.
TaDa at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

For this to work you will also need to overload "+" operation for Item. To do this you need code that looks like this:

Public Class Item

Public Quantity1 As Integer

Public Quantity2 As Integer

'.....

Public Quantity20 As Integer

Public Shared Operator +(ByVal arg1 As Item, ByVal arg2 As Item) As Item

Dim new_item As New Item

new_item.Quantity1 = arg1.Quantity1 + arg2.Quantity1

new_item.Quantity2 = arg1.Quantity2 + arg2.Quantity2

'...

new_item.Quantity20 = arg1.Quantity20 + arg2.Quantity20

Return new_item

End Operator

End Class

Thanks,
Vladimir

Vladimir_MS at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4
Vladimir,

Thanks for your answer. It works perfectly for what I need.

Lucas

LucasPasquali at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...