OOP question

G

Guest

Hi,



I have an LDAPUser Class.

It has 4 properties and One method as below.


public static LDAP FindLDAPUser(string emailaddress)
{
find user
if(user is found)
{
NewUser = new LDAP(sUID,sFirstName,sLastName,sEmailAddress);
return NewUser;
}
else
{
NewUser = new LDAP(empty values)
}


I am using the static method to contain my actual New contruct of the class.

In my code behind page of a webform(user enters an email address into a
textbox and presses enter).
I have the following code (drastically shortened):

private void cmdSearch_Click(object sender, System.EventArgs e)
{
LDAP NewUser;

NewUser = LDAP.FindLDAPUser(this.txtboxEmail.Text);

this.txtboxEmail.Text = NewUser.Email.ToString();
this.txtboxFName.Text = NewUser.FirstName.ToString();
this.txtboxLName.Text = NewUser.LastName.ToString();
this.txtboxUsername.Text = NewUser.EmployeeId.ToString();


}

Because my FindLDAPUser is a static method and actually creates the class
inside itself, is this bad OOD?

It works fine and everything, however because in the click event I do not
instantiate my class with new as below, my intellisence cannot pick up the
NewUser properties if I try to access these properties outside my if
statement. Please remember I left out a bunch of code.

LDAP NewUser = New LDAP(empty values);

I just don't like the idea of purposefully putting in bogus properties
values so that I can gain better access to my properties and methods. I would
then replace the empty values for the object with real values after I run the
FindLDAP user.


Should I just return a serialized peace of data from the FindLDAPUser method
and deserialize it into the new construct method?

I'd prefer not to instantiate an object at all if I can't find a user in the
first place after searching LDAP.

please advise.

thanks

chris
 
R

Rachel Suddeth

Chris said:
...
private void cmdSearch_Click(object sender, System.EventArgs e)
{
LDAP NewUser;

NewUser = LDAP.FindLDAPUser(this.txtboxEmail.Text);

this.txtboxEmail.Text = NewUser.Email.ToString();
this.txtboxFName.Text = NewUser.FirstName.ToString();
this.txtboxLName.Text = NewUser.LastName.ToString();
this.txtboxUsername.Text = NewUser.EmployeeId.ToString();


}

Because my FindLDAPUser is a static method and actually creates the class
inside itself, is this bad OOD?

Putting the allocation inside the FindLDAPUser is fine. You are initializing
the NewUser variable by calling the function that returns the correct type.
I have done things like this, and never had a problem with intellisense
because of it. However, if you want the thing to not exist when there's no
user, than you are going to have to test for its existance before using its
properties. That's just the way programming works. Your FindUser can return
null if no user is found. Then the statement that does the assignments
should go inside a block like
if ( NewUser != null ){}

If you want your object to automatically have empty values to assign when
there's no user so the rest of your code doesn't have to test for the user's
existance, then you have to instantiate it with empty values. The choice is
yours.

-Rachel
 
M

Michael S

Chris said:
Hi,



I have an LDAPUser Class.

It has 4 properties and One method as below.


public static LDAP FindLDAPUser(string emailaddress)
{
find user
if(user is found)
{
NewUser = new LDAP(sUID,sFirstName,sLastName,sEmailAddress);
return NewUser;
}
else
{
NewUser = new LDAP(empty values)
}


I am using the static method to contain my actual New contruct of the
class.

In my code behind page of a webform(user enters an email address into a
textbox and presses enter).
I have the following code (drastically shortened):

private void cmdSearch_Click(object sender, System.EventArgs e)
{
LDAP NewUser;

NewUser = LDAP.FindLDAPUser(this.txtboxEmail.Text);

this.txtboxEmail.Text = NewUser.Email.ToString();
this.txtboxFName.Text = NewUser.FirstName.ToString();
this.txtboxLName.Text = NewUser.LastName.ToString();
this.txtboxUsername.Text = NewUser.EmployeeId.ToString();


}

Because my FindLDAPUser is a static method and actually creates the class
inside itself, is this bad OOD?

- No. This is a implemented simple Factory-pattern. What you do above is
great OO. Having a static method build an instance is neat.
I just don't like the idea of purposefully putting in bogus properties
values so that I can gain better access to my properties and methods. I
would
then replace the empty values for the object with real values after I run
the
FindLDAP user.

Indeed. Hence the invention of null =)
please advise.

oki

Happy Coding
- Michael S
 

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