Pattern to increment an instance variable 
Author Message
 Pattern to increment an instance variable

I have an Integer instance variable accessed thru accessors. I was
wondering if there is a pattern to increment/decrement these
variables.

I got into trouble doing the following:

nextCount:= (self totalCount: self totalCount + 1.)

MyClass>>totalCount
        ^totalCount.

MyClass>>totalCount: anObject
        totalCount := anObject.

My nextCount was being initialized to self instead of the totalCount.
I fixed #totalCount: but I thought there must be a better pattern for
this?

Costas



Sun, 08 Jun 2003 01:18:35 GMT  
 Pattern to increment an instance variable
Costas,

Not sure if this is what you're looking for, but the value of an assignment
is the value of its right-hand side, so you could write:

self totalCount: (nextCount := self totalCount + 1)

I suggest you leave the setter to return self.

Regards,

Peter van Rooijen


Quote:
> I have an Integer instance variable accessed thru accessors. I was
> wondering if there is a pattern to increment/decrement these
> variables.

> I got into trouble doing the following:

> nextCount:= (self totalCount: self totalCount + 1.)

> MyClass>>totalCount
> ^totalCount.

> MyClass>>totalCount: anObject
> totalCount := anObject.

> My nextCount was being initialized to self instead of the totalCount.
> I fixed #totalCount: but I thought there must be a better pattern for
> this?

> Costas



Sun, 08 Jun 2003 02:35:55 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Instances incorrectly sharing instance variables?

2. Spin Box increments less than 1- from .0153 to .98438 w/.0153 increment changes

3. Increment numeric variables with "mouse"????

4. Variable increment counter

5. Incrementing a variable in Prolog

6. Pls Help: Incrementing Variables in Prolog

7. instances of classes and instances of instances

8. using variable as a pattern for matching

9. problems pattern matching a variable

10. Variable as regular expression in pattern?

11. awk pattern as a variable with regular expression

12. variable as search-pattern: if ($0 ~ myVar)

 

 
Powered by phpBB® Forum Software