Home Using an object in a single class with multiple constructors
Reply: 1

Using an object in a single class with multiple constructors

Yoav Linder
1#
Yoav Linder Published in 2018-01-12 11:05:59Z

This is the class that I build the constructors in, I want to have multiple "UserKeys" constructors, 1 for each character in my project but the main class doesn't recognize the second constructor only the first one (if I have a constructor that takes 0 arguments, it recognizes that and the first one, but not the second one)

abstract class BaseKeys
{
    public abstract bool LeftPressed();
    public abstract bool RightPressed();
    public abstract bool UpPressed();
    public abstract bool DownPressed();
    public abstract bool high_hitPressed();
    public abstract bool rope_jumpPressed();
    public abstract bool runRightPressed();
    public override bool leftRightPressed();
}

class UserKeys : BaseKeys
{
    #region data
    Keys left, right, up, down, walk;
    Keys combo, high_hit;
    #endregion

    #region ctor
    public UserKeys(Keys left, Keys right,
                    Keys up, Keys down, Keys high_hit)
    {
        this.left = left;
        this.right = right;
        this.up = up;
        this.down = down;
        this.high_hit = high_hit;
    }

    public UserKeys(Keys right, Keys left, 
                    Keys down, Keys walk, Keys high_hit)
    {
        this.left = left;
        this.right = right;
        this.down = down;
        this.high_hit = high_hit;
        this.walk = walk;
    }
}
Tim Schmelter
2#
Tim Schmelter Reply to 2018-01-12 11:41:29Z

Both constructors have the same parameters, that doesn't work. Only their names are different. You need to make them different:

public UserKeys(Keys left, Keys right,
                Keys up, Keys down, 
                Keys high_hit)
{
    this.left = left;
    this.right = right;
    this.up = up;
    this.down = down;
    this.high_hit = high_hit;
}

public UserKeys(Keys left, Keys right, 
                Keys up, Keys down, 
                Keys high_hit, Keys walk)
{
    this.left = left;
    this.right = right;
    this.up = up;
    this.down = down;
    this.high_hit = high_hit;
    this.walk = walk;
}

I've added Keys up to the second and used the same order as in the first(otherwise very confusing and error-prone). If you don't know Keys up pass Keys.None.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.483614 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO