Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

argument suspected not referring to proper variables

I am trying to understand what is going on here. I have two variables activeToInactive and inactiveToActive and I increment them whenever I update a status. Here is my code first of all

Counter Service:

export class CounterService {
    // Initial States
    activeToInactive = 0
    inactiveToActive = 0
    
    incrementStatusCount(statusCount: number) {
        // Increment either of the initial states whenever the function is called.
        statusCount++
        if (statusCount == this.activeToInactive) {
            console.log(`${statusCount} - active to inactive`)
        }
        if (statusCount == this.inactiveToActive) {
            console.log(`${statusCount} - inactive to aactive`)
        }
        // I notice that statusCount does equal the initial states so if statements evaluate to false
        
    }
}

Transfer Service:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import { Injectable } from "@angular/core";
import { CounterService } from "./counter.service";

@Injectable()
export class transferService {

    constructor(private Counter: CounterService) {}
    activeUsers = ['Max', 'Anna'];
    inactiveUsers = ['Chris', 'Manu']  
    
    setToInactive(id: number) {
        this.inactiveUsers.push(this.activeUsers[id]);
        this.activeUsers.splice(id, 1);


        // Calling the function in a different service
        this.Counter.incrementStatusCount(this.Counter.activeToInactive)
    }
      
    setToActive(id: number) {
        this.activeUsers.push(this.inactiveUsers[id]);
        this.inactiveUsers.splice(id, 1);

        // Calling the function in a different service
        this.Counter.incrementStatusCount(this.Counter.inactiveToActive)
    }  
}

When either function in Transfer Service is called, the initial states do not increment. All I know is that statusCount does NOT equal to any of the initial states. Does this have to do with references? I haven’t mastered them but if you have any tips on how I can approach this then let me know.

Thanks in advance !!!

>Solution :

You are very close.

In the incrementStatusCountfunction, you are receiving a parameter statusCount, which you intend to use for incrementing either activeToInactive or inactiveToActive based on the value passed to the function.

However, in the TransferService, when you call this.Counter.incrementStatusCount(this.Counter.activeToInactive) and this.Counter.incrementStatusCount(this.Counter.inactiveToActive), you are passing the initial values of activeToInactive and inactiveToActive instead of the actual counts that you want to increment. So let’s just use inactiveToActive and activeToInactive directly.

Update your CounterService like this:

incrementStatusCount(incrementActiveToInactive: boolean) {
    // Increment either of the initial states based on the provided flag.
    if (incrementActiveToInactive) {
        this.activeToInactive++;
        console.log(`${this.activeToInactive} - active to inactive`);
    } else {
        this.inactiveToActive++;
        console.log(`${this.inactiveToActive} - inactive to active`);
    }
}

Your TranserService will only slightly change, like so:

setToInactive(id: number) {
        this.inactiveUsers.push(this.activeUsers[id]);
        this.activeUsers.splice(id, 1);

        // Call the function with the correct flag
        this.Counter.incrementStatusCount(true);
    }

    setToActive(id: number) {
        this.activeUsers.push(this.inactiveUsers[id]);
        this.inactiveUsers.splice(id, 1);

        // Call the function with the correct flag
        this.Counter.incrementStatusCount(false);
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading