Zabahey
Zabahey

Reputation: 328

What appropriate way to create and use class ? (example inside)

Now. I so confuse when use any classes. What is best practice approach to use them?

Example I have a UserClass name User that have 2 attributes UserName and Password

when I use I will create new object like this

UserClass userObject = new UserClass();

this is my question that I so confuse to use

If i want to call Method name "Login" what is appropriate way to use them?

Between 1 Set Value to Attribute of Object Like this

      userObject.UserName = "user";
      userObject.Password = "password";

      if (userObject.Login())
      {
           //Do Something After Login
      }
      else
      {
           //Show Something when Error
      }

   2 Send Value as parameter
      if (userObject.Login("user","password"))
      {
           //Do Something After Login
      }
      else
      {
           //Show Something when Error
      }

From above What is appropriate approach to use and apply to any classes?

Thank you very much for your guidance _/\_

P.S I practice to be better Programmer

Upvotes: 1

Views: 85

Answers (1)

SLaks
SLaks

Reputation: 887433

Use a property to store information that belongs to the object. (eg, a Username)

Use a parameter to pass information to a single method which doesn't have to do with the rest of the object.

Upvotes: 3

Related Questions