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
In this video, you'll learn how to create the same methods but with different parameters.
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.
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!
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.