Tom Gullen
Tom Gullen

Reputation: 61719

c# is this how I do override methods?

    // Loads a users info
    public void loadUserInfo()
    {
        CrystalTech.tblUsersDataTable dsCommon = new CrystalTech.tblUsersDataTable();
        using (tblUsersTableAdapter userAdapter = new tblUsersTableAdapter())
        {
            userAdapter.FillBy(dsCommon, this.ID);
        }
        this.username = dsCommon[0].userName;
        this.company.ID = dsCommon[0].clientID;
        this.company.name = dsCommon[0].ClientName;
        this.isBuyer = bool.Parse(dsCommon[0].isBuyer.ToString());
        this.isClient = bool.Parse(dsCommon[0].isClient.ToString());
        this.isClientPowerUser = bool.Parse(dsCommon[0].powerUser.ToString());
        this.isReportingUser = bool.Parse(dsCommon[0].reportingUser.ToString());
        this.isSupplier = bool.Parse(dsCommon[0].isSupplier.ToString());
        this.isPaperSupplier = bool.Parse(dsCommon[0].isPaperSupplier.ToString());
        this.hasKitView = bool.Parse(dsCommon[0].haskitview.ToString());
    }
    public void loadUserInfo(int usersID)
    {
        this.ID = usersID;
        loadUserInfo();
    }

Is this correct/standard? Or am I approaching this incorrectly? The aim is to have the usersID passed in an optional parameter.

Upvotes: 1

Views: 421

Answers (7)

Scott
Scott

Reputation: 13921

You can also use optional parameters as of C# 4.0:

// Loads a users info
    public void loadUserInfo(int usersID = 0)
    {
        if (usersID > 0) this.ID = usersID;

        CrystalTech.tblUsersDataTable dsCommon = new CrystalTech.tblUsersDataTable();
        using (tblUsersTableAdapter userAdapter = new tblUsersTableAdapter())
        {
            userAdapter.FillBy(dsCommon, this.ID);
        }
        this.username = dsCommon[0].userName;
        this.company.ID = dsCommon[0].clientID;
        this.company.name = dsCommon[0].ClientName;
        this.isBuyer = bool.Parse(dsCommon[0].isBuyer.ToString());
        this.isClient = bool.Parse(dsCommon[0].isClient.ToString());
        this.isClientPowerUser = bool.Parse(dsCommon[0].powerUser.ToString());
        this.isReportingUser = bool.Parse(dsCommon[0].reportingUser.ToString());
        this.isSupplier = bool.Parse(dsCommon[0].isSupplier.ToString());
        this.isPaperSupplier = bool.Parse(dsCommon[0].isPaperSupplier.ToString());
        this.hasKitView = bool.Parse(dsCommon[0].haskitview.ToString());
    }

Upvotes: 5

Guffa
Guffa

Reputation: 700152

No, it's not. Overriding is when you supply a different implementation for an inherited method, what you are doing is overloading.

When overloading, you should use the same name for the methods. Also, it's recommended to name methods using pascal case:

// Loads a users info
public void LoadUserInfo() {
  ...
}

public void LoadUserInfo(int usersID) {
  this.ID = usersID;
  loadUserInfo();
}

Upvotes: 1

Barlow Tucker
Barlow Tucker

Reputation: 6519

If you are wanting ID as an optional param, you should check out C#'s optional param that is in C# 4.0

http://geekswithblogs.net/michelotti/archive/2009/02/05/c-4.0-optional-parameters.aspx

Upvotes: 1

Bablo
Bablo

Reputation: 916

Firstly what you have done is method overloading, overriding is something completely different. Assuming it is method overloading you are after and the name of the parametered method is a simple typo, then yes that is how you do it.

Upvotes: 2

Chris Barlow
Chris Barlow

Reputation: 3314

Tom,

There's nothing wrong with doing it this way, but you don't have to have a different method name to accept the parameter (loadUserInfo vs. loadUserInf). You can have two different methods with the same name if they accept different parameters. So you could have:

public void loadUserInfo()

AND

public void loadUserInfo(int usersID)

Upvotes: 2

Kernow Steve
Kernow Steve

Reputation: 196

The second one should be loadUserInfo but otherwise, yes.

Upvotes: 1

m.edmondson
m.edmondson

Reputation: 30862

Any time you have methods with the same name but different parameters you are correctly overloading.

Assuming that second method is meant to be loadUserInfo then yes - this is correct.

Upvotes: 2

Related Questions