Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the Property from Another Class?

Status
Not open for further replies.

eb24

Programmer
Dec 17, 2003
240
US
Let's say I have 2 classes, Employee and State (reference the code below for further details), and I set the Employee.stateID, upon the setter being set, how can I get the StateName, i.e. State.stateName? Thank you in advance for any assistance!

----- Employee Class -----
public class Employee
{
private string _firstName;
private string _lastName;
private int _stateId;

... removed other code for brevity
public int StateId
{
get { return _stateId;}
set { _stateId= value; }
}
}
----- State Class -----
public class State
{
private int _stateId;
private string _stateName;

... removed other code for brevity
public int StateId
{
get { return _stateId;}
set { _stateId= value; }
}
public int StateName
{
get { return _stateName;}
set { _stateName= value; }
}
}
 
Employee shouldn't have a reference to StateId. Instead it should reference State
Code:
class Employee
{
   public State State {get; set;}
}
public class State {}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks for your reply!

Maybe I should elaborate on my business logic layer - these classes are genereated via some ORM tool and thus resemble our database schema. So, according to my code from above, the Employee table has a field called StateId which in turn is linked to the State table. So, let's say the StateId is set, within that setter, how can I retrieve the StateName? Do I have to create a function in the Collection that loops through each State?

Thank you in advance for any assistance!
 
You are missing Jason's point...

Your database has an id that links to a State but in your class - oo design suggests that you should not pass back the ID but instead pass back the object that it is related to.

True OO design allows the business layer to not care where the information comes from or how it's linked, but what classes contain what other classes.

You should be able to produce statements like

organization.Address.State.StateName

rather than producing statements like

int id = organization.AddressID;
Address address = //do something to go find the address
int stateid = address.StateID;
State state = //do something to find state
string statename = state.StateName

ORM tools are great for helping you to produce classes that follow the schema of a DB but it is then up to you to take it that extra step to make the system truly OO.

 
Thanks for advice.

As I continue to learn... that's all I can say at the moment.

Still doesn't draw me any closer at the moment to a solution. Any and all tips will be greatly appreciated!
 
what orm tool are you using?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Well, CodeSmith to generate our DAL and BLL. I can access other classes b/c of parent / child relationships, but this is like a type and its only connection that is see, is that its a property.

Really appreciate the assistance!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top