(Really) Basic OO Design Guidance Needed.

P

pbd22

Hi.

I need some help on object oriented design concepts. This question may
seem silly but I need a better understand of what is going on when a
type of class N is used within a method declaration. For example, I
have designed a "User" class and am using it such that:

public User GetUserByName(string first, string last)
{
// code...
}

I suppose I need a better understanding of how this method would be
called within the context
of a program. The User class is below.

Thanks.

public class User
{

#region Member Variables

protected int _ID
protected string _firstName;
protected string _lastName;
protected string _email;
protected string _password;
protected string _gender;
protected string _securityQuestion;
protected string _securityAnswer;
protected string _city;
protected string _state;
protected string _zipCode;
protected string _securityCode;

#endregion

#region Constructors

public User()
{
}

public User(int ID, string email, string password, string first,
string last,
string gender, string question, string answer, string
city,
string state, string zip)
{

this._email = email;
this._password = password;
this._firstName = first;
this._lastName = last;
this._gender = gender;
this._securityQuestion = question;
this._securityAnswer = answer;
this._city = city;
this._state = state;
this._zipCode = zip;
}

#endregion

#region Public Properties

public Guid _ID { get; set; }
public string _Email { get; set; }
public string _Password { get; set; }
public string _Gender { get; set; }
public string _City { get; set; }
public string _State { get; set; }
public string _ZipCode { get; set; }
public string _SecirtyCode { get; set; }

#endregion

#region Methods and Functions

#endregion

}
 
S

Sebastian Hungerecker

pbd22 said:
public User GetUserByName(string first, string last)
{
// code...
}

I suppose I need a better understanding of how this method would be
called within the context of a program.

I don't think I understood your question.
The above method could be called like this:
User john = GetUserByName("john", "doe");
// now do something with john
If that's not the answer to your question, please rephrase it.

HTH,
Sebastian
 
P

proxyuser

Sebastian Hungerecker said:
I don't think I understood your question.
The above method could be called like this:
User john = GetUserByName("john", "doe");
// now do something with john
If that's not the answer to your question, please rephrase it.

It depends what he means by "Get". That's typical of a database
application, where the user exists in the database, and you want to retrieve
that information and construct a new User object in the program to represent
it. Of course there could be dozens of other explanations.

But, it really is as simple as Sebastian says. There is nothing much "OO"
going on here, unless you provide more of the overall design.
 
S

sloan

You can find a basic Customer / Order / OrderDetail example here:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!140.entry
based on Northwind database.

Your User class needs to be created by something.
Since you're providing information (presumably about an already existing
User like "John" "Smith") then that information is probably persisted
somewhere. A database, a text file, etc, etc.


Here is another flavor you can look at:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!176.entry

........

You're not really doing anything "OO" here. You have a basic object.

You need (something) to populate the class. My preference is to put that
code into a Controller or Manager class.
UserController or UserManager

Check my code samples. Both are DOWNLOADABLE and should work within a few
minutes of downloading them.
 
R

rhaazy

I think what the OP is looking for is what he would do inside his
GetUser function.
The name of your function implies that the user already exists, and
you are infact searching for it.
So you would have to have some code in this function that searches for
the user.

public User GetUserByName(string first, string last)
{
User objectToReturn = new User();
objectToReturn = SearchForUser(first, last);
return objectToReturn;

}

If what you are doing is creating a new user based on the inputed
first and last name, then this is what you would do here...

public User GetUserByName(string first, string last)
{
User objectToReturn = new User();
objectToReturn.FirstName = first;
objectToReturn.LastName = last;
....etc
return objectToReturn;

}
 
S

Sebastian Hungerecker

rhaazy said:
public User GetUserByName(string first, string last)
{
User objectToReturn = new User();
objectToReturn = SearchForUser(first, last);
return objectToReturn;

}

You create a new User object by calling new User() and then just throwing it
away on the next line. If you remove that your code becomes
User objectToReturn = SearchForUser(first, last);
return objectToReturn;

or simply:
return SearchForUser(first, last);

which seems pretty pointless, as you could just call the SearchForUser
method directly.

HTH,
Sebastian
 
E

eschneider

He may also be wondering how to use the user objects he created, here is a
simple example:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace WindowsFormsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

User john = GetUserByName("john", "doe");

System.Console.WriteLine(john.ToString());

}

/// <summary>

/// Find a user by name.

/// </summary>

/// <param name="first"></param>

/// <param name="last"></param>

/// <returns></returns>

public User GetUserByName(string first, string last)

{

List<User> users = ObtainUsers();

for (int t = 1; t <= 5; t++)

{

User item = users[t];

if (string.Compare(item.FirstName, first, true) == 0 &
string.Compare(item.LastName, last, true) == 0)

{

return item;

}

}

return null;

}

/// <summary>

/// Create a list of users. Could be pulled from a database or web service.

/// </summary>

/// <returns></returns>

public List<User> ObtainUsers()

{

List<User> users = new List<User>();

User jane = new User("Jane", "Doe");

users.Add(jane);

User john = new User("John", "Doe");

users.Add(john);

return users;

}

}

/// <summary>

/// User object.

/// </summary>

public class User

{

private string m_FirstName;

private string m_LastName;

public string FirstName

{

get { return m_FirstName; }

set { m_FirstName = value; }

}

public string LastName

{

get { return m_LastName; }

set { m_LastName = value; }

}

public User(string first, string last)

{

this.FirstName = first;

this.LastName = last;

}

public override string ToString()

{

return this.LastName + ", " + this.FirstName;

}

}

}
 
P

Peter Morris

Firstly you'd have to make it a static method. Then I'd suggest not
implementing DB actions on your user class anyway. Take a look at an object
persistence framework!
 
H

Hillbilly

I recommend a trip to Amazon to find a used copy of the now out of print "C#
Class Design Handbook" as it will help you establish all of the fundamentals
of OOP design, its a no fluff handbook published by Wrox and can be
purchased as low as $7-10 US.
 
P

pbd22

Thanks All.

Thanks eschneider - that "was" what I was looking for. I was somewhat
confused on how to
use my User class from within my program. Your code has helped a lot,
thanks. As an aside,
wouldn't it be easier to simply pass the First and Last names to the
where clause of the database
query vs. returning all the records from the database and then doing
your sort logic within the
ObtainUsers() method?

I have one more question. How do I access all the data for a given
user based on a specific condition -
such as the UserID. So, for "User uObject = new User()" I want to pass
uObject.UserID = ID and get
back all of its assigned properties such as uObject.First,
uObject.Last, uObject.Gender and so on.

Thanks again.
 
E

eschneider

To obtain one user yes, but probably not in the long run, because eventually
someone will want all the users, that also requires intermitten DB usage. If
you do request by user name make sure you use parameter objects and not just
a sql string.

I don't understand you question, the uObject contains all the properties,
what do you mean by get back (from the Database)?

I would pick-up a book on OO, it will help speed you up.


Schneider
 
B

Bill Richards

Is this really a discussion about OO?

It seems to me that this is a "How do I construct an object and subsequently
use it?" question.

I think the OO answers should be something along the lines of ...

1. Create an interface and/or an abstract class

public abstract class User
{
protected User(string givenName, string familyName)
{
GivenName = givenName;
FamilyName = familyName;
}

// define some user specific members
public string GivenName { get; private set; }
public string FamilyName { get; private set; }

public abstract string GetFullName();
}

2. Create some concrete classes

public class AdministrativeUser : User
{
public AdminstrativeUser(string given, string family) : base(given,
family) { /* AdministrativeUser ctor code */ }

// implement the abstract members
override public string GetFullName() { return string.Format("Admin user
{0}, {1}", FamilyName, GivenName); }
}

public class RegularUser : User
{
public RegularUser (string given, string family) : base(given, family)
{ /* RegularUser ctor code */ }

// implement the abstract members
override public string GetFullName() { return string.Format("Regular
user {1} {0}", FamilyName, GivenName); }
}

3. Once you have this, you can implement a factory pattern to instantiate
your user objects returning you a generic (usual use of the word here, not
as in C# Generics) User object, for example

public class UserFactory
{
public static List<User> CreateUsersList()
{
return new List<User> { new AdministrativeUser("Queen",
"Elizabeth"), new RegularUser("Joe", "Public"),new
AdministrativeUser("Prince", "Phillip"), }
}
}

4. Now we can use our objects in an OO manner (because that is how we have
defined them).

foreach(var user in UserFactory.CreateUsersList())
Debug.WriteLine(user.GetFullName());

This is OO because we have a lits of items of type User, and when we operate
on each of these items using members declared in the User class the
behaviour is actually that defined in the specific AdministrativeUser or
RegularUser instance.

There are other aspects to OOP, this example has just demonstrated
inheritance and polymorphism.

Read books and practice, in my humble experience one can only excell through
patience, study and much practice.
 
P

pbd22

Hillbily (et al) -

I have been looking for that book (its lowest used price is high 30s
on amazon).
This book is out of print and came out in 2003. Is there a more recent
book that
does an equally good job of explaining the concepts? Or, do you really
feel that
this book is the best at explaining Class Design / OO despite its
age?

Thanks again.
 
S

sloan

Sorry, I hit SEND too fast.

The way I usually "fish" for old books is to:

Go to google.com and click shopping.
Put the title in.
Find the ISBN
Fish around there to see if the books are available. Older books are
usually listed, but will show "Out of Stock" at the websites.

Then use the ISBN, try ebay. Try amazon for their USED books (through other
vendors).

Good luck.
 
P

pbd22

thanks sloan. I bit the bullet and bought the lowest used price on
amazon (which wasn't that low).

thanks for the suggestion.
cheers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top