Beginning C# - Part 27: Overloading | Ray Wenderlich

In this video, you'll learn how to create the same methods but with different parameters.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3247-beginning-c/lessons/27

Hello! Can I download your lectures?

hello why are the videos not available any more?

Iโ€™m not sure what you mean - the entire series is available to view on the site? Are you not seeing it?

Hello, my code for this challenge is as follow:

class GoCart
{
    public void SetSpeed(int speed)
    {
        Debug.Log("The GoCart speed is: " + speed);
    }

    public void SetSpeed(int speed, bool turbo)
    {
        if (turbo)
        {
            Debug.Log("The GoCart is: " + speed * 2);
        }
        else
        {
            Debug.Log("The GoCart is: " + speed);
        }
    }
}

public class OverloadingChallenge : MonoBehaviour
{
    void OnDisable()
    {
        GoCart goCart = new GoCart();
        goCart.SetSpeed(10,true);
    }
}

Do same thing, but its simpler, wanna know what is wrong with it because when i do challenge like this and watch your solution it was different yet giving same result.
Dont understand/KNOW what problems can cause my approach (it can i suppose if you do this different way)?
Thx for reply in advance, cheers.

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

In SetSpeed(), you are actually making your modifications to the speed parameter. That needs to be an instance variable. In the demo, notice I use โ€˜this.speed += 2โ€™ instead of โ€˜speed += 2โ€™. Currently, you are changing the speed but the state isnโ€™t be recorded. I hope that helps!

@bdmoakley Remind me again why should i not repeat myself when coding. Ty.

@humdy Please check out this article when you get a chance:

I hope it helps!

Hello, I know this is kinda old but still, my code from the challenge was:

class GoKart
{

private int speed;
public bool Turbo;

public void SetKartInfo(int speed)
{
    this.speed = (Turbo = true) ? speed * 2 : speed;
    Debug.Log("Speed with turbo on " + speed);
}


public void SetKartInfo(int speed, bool Turbo)
{
    this.Turbo = Turbo;
    Debug.Log("Speed with turbo off " + speed);
    SetKartInfo(speed);

}

}

public class overloadHomework : MonoBehaviour {

void OnDisable()
{
    GoKart gokart = new GoKart();
    gokart.SetKartInfo(5, true);
   
}

}

I saw another similar comment but still wanted to check if this is wrong in any way bcoz it looks like it gives the same results. thanks.