By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,915 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,915 IT Pros & Developers. It's quick & easy.

Object Naming

P: n/a
I've been teaching myself C# for a few months now and I have a concept
problem. I know how to instantiate an object from a class I’ve created,
but now I want to retrieve and store data in a database and I can’t seem
to understand how I would name my objects.

For example, I have an employee’s database with all the info I want an
employee to have. I also have an employee class, that with methods can
open and retrieve an employee from the database and manipulate that data
and save it back to the database. Now this is where I have a conceptual
problem. When I was goofing around and learning this OOP language I
could type the following:

Employee fred = new Employee();

With this new object "fred" I could do all those things that an object
of Employee could do. Now I named this object "fred" in my code and
used it as needed. What I’m having a problem with is when the data is
in a database and a user wants to open the database and change something
how do you name the objects? Another words, how do I get data from the
database and use it as the name of my object? If the user opens more
than one employee at a time how do I name these Employee objects using
the database? Do I have to have different names for each object of
employee opened? Do I have to even use the database to name my objects.

This is a very confusing concept to me so pretend you’re talking to a
child and maybe I can understand this concept.

Thanks in advance.

Lance Alwood
la******@sbcglobal.net
Nov 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
You're trying to apply object oriented techniques to database accesses,
which is good, but not supported at a low level by the language. This will
all change with C# 3.0 and Linq but for now you're stuck dealing with DB
access in the old-world.

You are also confusing the instance of an object with the name of the class.
You may have thousands of objects of type Employee. They will always be of
type Employee but they may have an individual identifier, perhaps the value
associated with their primary key in the database, with which they may also
be associated.

To be honest it's very difficult to advise you. You need to understand the
idea of a class, the instance of a class and how to diffeentiate between two
instances of the same class which is an application specific matter.

I'll try to make things better by saying:

#1 A class is the definition of a set of behaviours that can be applied to
an individual chunk of data
#2 An instance is an individual chunk of data that obeys rules set out by
the class definition
#3 Any instance may provide some unique identifier such as a name,
stock-number or guid or any other unique thing that individually identifies
the object within your own scheme. This identifier is entierely up to you
and doesn't have a set of rules associated with naming conventions or
indeed, built into the programming language.

I don't know if this'll help but FWIW....

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Lance" <la******@sbcglobal.net> wrote in message
news:Se*******************@newssvr14.news.prodigy. com...
I've been teaching myself C# for a few months now and I have a concept
problem. I know how to instantiate an object from a class I’ve created,
but now I want to retrieve and store data in a database and I can’t seem
to understand how I would name my objects.

For example, I have an employee’s database with all the info I want an
employee to have. I also have an employee class, that with methods can
open and retrieve an employee from the database and manipulate that data
and save it back to the database. Now this is where I have a conceptual
problem. When I was goofing around and learning this OOP language I could
type the following:

Employee fred = new Employee();

With this new object "fred" I could do all those things that an object of
Employee could do. Now I named this object "fred" in my code and used it
as needed. What I’m having a problem with is when the data is in a
database and a user wants to open the database and change something how do
you name the objects? Another words, how do I get data from the database
and use it as the name of my object? If the user opens more than one
employee at a time how do I name these Employee objects using the
database? Do I have to have different names for each object of employee
opened? Do I have to even use the database to name my objects.

This is a very confusing concept to me so pretend you’re talking to a
child and maybe I can understand this concept.

Thanks in advance.

Lance Alwood
la******@sbcglobal.net

Nov 17 '05 #2

P: n/a
In addition to Bob's post, here are some more clarifications where I
think I see confusion in your post:

1. Not every instance of Employee in your program has to "have a name,"
as you put it. Don't forget that you can create collections of
employees, like this:

ArrayList employeeList = new ArrayList();
employeeList.Add(new Employee());
employeeList.Add(new.Employee());

Now you have an ArrayList called employeeList that contains two
employees... but those employees don't really have "names" in the sense
that you meant, do they?

2. You might do better to talk in terms of "references" rather than
"names" when talking about things like fred:

Employee fred = new Employee();

In truth, the employee isn't "named fred" unless inside the Employee
class there is a field or property called Name, and it's set to "fred"
for this particular instance. Rather, you have a variable called "fred"
that can contain a reference to an Employee instance. You created a new
instance of Employee, and put a reference to that instance into the
variable "fred". This would be nothing more than a complicated
rewording of what you said, except for this:

Employee fred = new Employee();
Employee jane = fred;

In this case, there is _only one_ instance of Employee. Both the
variable "fred" and the variable "jane" contain references to that same
instance of Employee. The Employee instance itself is just sitting in a
chunk of memory somewhere. It has no "name," unless you give it a
quality (field or property) called "name" or "Name" or something, but
then that has _nothing to do_ with the names of the variables that may
contain references to it. Just to drive the point home, in the above
case if you were to say:

fred.Salary = 100000;

then jane.Salary would also change to be 100000, because fred and jane
both refer to the same Employee instance, and it's that instance that
holds the Salary.

As you can see, thinking of object instances as having the "name" of a
variable that refers to them can lead to misunderstandings! Variables
are usually named based on what you're _doing with_ the object instance
at the moment, not based on the identity of the object (although there
are exceptions).

3. There are no Employee objects "in the database" (unless you have an
ObjectBase, which is doubtful, as they sort of came and went in the
1990's). Most likely, you have a relational database, in which case
there are no Employee objects there.

An instance of an Employee (or an Employee object) is a chunk of memory
controlled by your running program, with certain information in it that
you have defined in your class definition. Don't confuse this, which
exists only inside your program, with _other representations_ of
employees. Certainly the database contains employee information, but
it's not in the same form as your Employee instances in memory. It's
employee information, all right, but it is in a different form.

So, you need to write software to mediate between the representation of
an employee in the database and Employee objects in the memory used by
your program. You need to write code to read employee information from
the database (most likely coming back in the form of a DataSet
containing DataTables... see the System.Data namespace or a reference
on ADO.NET for more information) and then unpack that information and
use it to construct Employee instances in memory.

Similarly, when you want to save changes to a particular Employee
instance, you will have to write code to get the relevant information
out of your in-memory Employee instance and save that back to the
database, thus changing that employee's representation in the database.

So, the process is: make a request to the database for information; use
the information to build Employee instances; work with the Employee
instances; build database command(s) to update the database with any
changes; execute the command(s) against the database to change the
employee information there.

This doesn't just apply to databases: XML, comma-separated files, and
SOAP messages are all examples of possible external representations for
an employee. In each case you would have to write software to translate
between the external representation and instances of the Employee class
within your program.

Nov 17 '05 #3

P: n/a
Yes Bob that helps me know that I will sign up with a grammer news group
and try to help me express myself better. I reread what I wrote and I
can see that I didn't correctly explain the problem.

But you hit the problem right on the nose...you mentioned: "Any instance
may provide some unique identifier such as a name, stock-number or guid
or any other unique thing that individually identifies the object within
your own scheme."

Okay lets say I have a unique id in the database or from anywhere how do
I code it?

So instead of this:

Employee fred = new Employee();

Employee "" = new Employee();

Now what goes between those quotes above? I know the quotes don't
belong there but how do I get the id from the database into the code
where "fred" would be? I can't use a variable like this can I?

string emp_name = "fred";

Employee emp_name = new Employee();

I don't know how to take the data from a database or someother source
and make it the name of my objects that get instatiated in code?

It's hard for me to express this problem because the concept of naming
my objects by hard coding their name is all I know how to do. How do I
name many objects from the same class using different names from some
other source than my hard coding the name? Does that make any sense?




Bob Powell [MVP] wrote:
You're trying to apply object oriented techniques to database accesses,
which is good, but not supported at a low level by the language. This will
all change with C# 3.0 and Linq but for now you're stuck dealing with DB
access in the old-world.

You are also confusing the instance of an object with the name of the class.
You may have thousands of objects of type Employee. They will always be of
type Employee but they may have an individual identifier, perhaps the value
associated with their primary key in the database, with which they may also
be associated.

To be honest it's very difficult to advise you. You need to understand the
idea of a class, the instance of a class and how to diffeentiate between two
instances of the same class which is an application specific matter.

I'll try to make things better by saying:

#1 A class is the definition of a set of behaviours that can be applied to
an individual chunk of data
#2 An instance is an individual chunk of data that obeys rules set out by
the class definition
#3 Any instance may provide some unique identifier such as a name,
stock-number or guid or any other unique thing that individually identifies
the object within your own scheme. This identifier is entierely up to you
and doesn't have a set of rules associated with naming conventions or
indeed, built into the programming language.

I don't know if this'll help but FWIW....

Nov 17 '05 #4

P: n/a
Let me see if I can rewrite this and make less confusing:

Lets say I have a program which allows a user to edit employees. She
opens up one employee record and starts to edit it. Then she decides to
open up another record and edit it at the same time. So two records are
open and she is going back and forth between them.

Now in code I have to create an object from Employee so she can edit the
first record. When she opens the second record I have to create another
object of class Employee for her to edit.

What I'm having a tough time with is the concept or the way you open
these objects and reference them each as separate objects in code. I
can't name each object the same and I don't know which employee she will
choose so I can't name use Employee fred = new Employee();

So the real problem is how do I give each object a separate reference in
code not knowing which employee they will choose? Or how do you give
different references to objects of the same class on the fly?


Lance wrote:
I've been teaching myself C# for a few months now and I have a concept
problem. I know how to instantiate an object from a class I’ve created,
but now I want to retrieve and store data in a database and I can’t seem
to understand how I would name my objects.

For example, I have an employee’s database with all the info I want an
employee to have. I also have an employee class, that with methods can
open and retrieve an employee from the database and manipulate that data
and save it back to the database. Now this is where I have a conceptual
problem. When I was goofing around and learning this OOP language I
could type the following:

Employee fred = new Employee();

With this new object "fred" I could do all those things that an object
of Employee could do. Now I named this object "fred" in my code and
used it as needed. What I’m having a problem with is when the data is
in a database and a user wants to open the database and change something
how do you name the objects? Another words, how do I get data from the
database and use it as the name of my object? If the user opens more
than one employee at a time how do I name these Employee objects using
the database? Do I have to have different names for each object of
employee opened? Do I have to even use the database to name my objects.

This is a very confusing concept to me so pretend you’re talking to a
child and maybe I can understand this concept.

Thanks in advance.

Lance Alwood
la******@sbcglobal.net

Nov 17 '05 #5

P: n/a
Don't confuse the name of the variable you're using to hold a reference
to an Employee with the identity of the Employee object itself. In
order to illustrate what I mean by that, here's a simple mock-up of an
Employee class:

public class Employee
{
private int _employeeId;
private string _firstName;
private string _lastName;

public Employee(int id, string first, string last)
{
this._employeeId = id;
this._firstName = first;
this._lastName = last;
}

public int EmployeeId
{
get { return this._employeeId; }
}

public string FirstName
{
get { return this._firstName; }
set { this._firstName = value; }
}

public string LastName
{
get { return this._lastName; }
set { this._lastName = value; }
}
}

Now, given this, you can make an employee #1 called "Fred Everyman"
like this:

Employee employeeToEdit = new Employee(1, "Fred", "Everyman");

Notice that the the variable "employeeToEdit" that holds a reference to
the employee isn't named based on _who_ the employee is, but rather on
_what_ you want to do with it. The C# compiler doesn't care about this,
of course... you could just as easily have said

Employee xhfdsljfa = new Employee(1, "Fred", "Everyman");

and the employee would still be #1, and his name would still be "Fred
Everyman". It would just be a lot harder to read the code. :-)

And, as I mentioned in my previous post, you can make lists of these
employees:

ArrayList employees = new ArrayList();
employees.Add(new Employee(1, "Fred", "Everyman"));
employees.Add(new Employee(2, "Jane", "Johnson"));
employees.Add(new Employee(15, "Harry", "Hurricane"));

Now you have a list of three employees. Their identities: that is,
their employee numbers and their names, are part of the Employee object
instances themselves, and have nothing to do with the name of the
variable referring to them. Again, as I said in my last post, try this:

Employee a = new Employee(1, "Fred", "Everyman");
b = a;
Console.WriteLine(b.LastName);
a.LastName = "Johnson";
Console.WriteLine(b.LastName);

You will find that b.LastName changes from "Everyman" to "Johnson",
because a and b are two variables that refer to the same instance of
Employee: employee #1, Fred Everyman, so when you say "Change the last
name of the employee to which 'a' refers," you change the last name of
the only employee instance around: employee #1. It just happens that
both variables a and b "point" to it, if you will.

I suggest that you get these concepts clear in your mind before
worrying about databases... they're a whole other kettle of fish.

Nov 17 '05 #6

P: n/a
Thanks Bruce I love the example, it amazes me how much there is to learn.

Lets say that I don't know how many objects the user is going to have
open at the same time and I don't know what order they are going to
close them. So your example:

Employee employeeToEdit = new Employee(1, "Fred", "Everyman");

I have an object called employeeToEdit which I hard coded and I can edit
it but now like I said the user wants more than one record open at the
same time. I have to give a reference to the second or third or more
objects and they cannot reference the same object because they are in
fact different objects.

How do I give different references to each object on the fly?

Bruce Wood wrote:
Don't confuse the name of the variable you're using to hold a reference
to an Employee with the identity of the Employee object itself. In
order to illustrate what I mean by that, here's a simple mock-up of an
Employee class:

public class Employee
{
private int _employeeId;
private string _firstName;
private string _lastName;

public Employee(int id, string first, string last)
{
this._employeeId = id;
this._firstName = first;
this._lastName = last;
}

public int EmployeeId
{
get { return this._employeeId; }
}

public string FirstName
{
get { return this._firstName; }
set { this._firstName = value; }
}

public string LastName
{
get { return this._lastName; }
set { this._lastName = value; }
}
}

Now, given this, you can make an employee #1 called "Fred Everyman"
like this:

Employee employeeToEdit = new Employee(1, "Fred", "Everyman");

Notice that the the variable "employeeToEdit" that holds a reference to
the employee isn't named based on _who_ the employee is, but rather on
_what_ you want to do with it. The C# compiler doesn't care about this,
of course... you could just as easily have said

Employee xhfdsljfa = new Employee(1, "Fred", "Everyman");

and the employee would still be #1, and his name would still be "Fred
Everyman". It would just be a lot harder to read the code. :-)

And, as I mentioned in my previous post, you can make lists of these
employees:

ArrayList employees = new ArrayList();
employees.Add(new Employee(1, "Fred", "Everyman"));
employees.Add(new Employee(2, "Jane", "Johnson"));
employees.Add(new Employee(15, "Harry", "Hurricane"));

Now you have a list of three employees. Their identities: that is,
their employee numbers and their names, are part of the Employee object
instances themselves, and have nothing to do with the name of the
variable referring to them. Again, as I said in my last post, try this:

Employee a = new Employee(1, "Fred", "Everyman");
b = a;
Console.WriteLine(b.LastName);
a.LastName = "Johnson";
Console.WriteLine(b.LastName);

You will find that b.LastName changes from "Everyman" to "Johnson",
because a and b are two variables that refer to the same instance of
Employee: employee #1, Fred Everyman, so when you say "Change the last
name of the employee to which 'a' refers," you change the last name of
the only employee instance around: employee #1. It just happens that
both variables a and b "point" to it, if you will.

I suggest that you get these concepts clear in your mind before
worrying about databases... they're a whole other kettle of fish.

Nov 17 '05 #7

P: n/a
Well, you can use collection structures (see System.Collections
namespace) such as ArrayList.

Or, in the case you're citing, most likely the person would have
several instances of the same form open on the screen at the same time,
and each instance of the form class would hold a reference to the
instance of Employee that that form was editing.

If you're talking multiple users across multiple systems then the
problem moves into the database, because class instances aren't shared
across different copies of your program running in different places (or
even on the same machine). Again, don't confuse the Employee object
that your program is holding in memory with the employee information
stored in a database. They're different representations, even though
they represent the same real-world thing.

Nov 17 '05 #8

P: n/a
On Tue, 25 Oct 2005 23:44:08 GMT, Lance <la******@sbcglobal.net>
wrote:
Let me see if I can rewrite this and make less confusing:

Lets say I have a program which allows a user to edit employees. She
opens up one employee record and starts to edit it. Then she decides to
open up another record and edit it at the same time. So two records are
open and she is going back and forth between them.

Now in code I have to create an object from Employee so she can edit the
first record. When she opens the second record I have to create another
object of class Employee for her to edit.

What I'm having a tough time with is the concept or the way you open
these objects and reference them each as separate objects in code. I
can't name each object the same and I don't know which employee she will
choose so I can't name use Employee fred = new Employee();

So the real problem is how do I give each object a separate reference in
code not knowing which employee they will choose? Or how do you give
different references to objects of the same class on the fly?


The basic rule is that code shouldn't care about the specific content
of an object. If your code contains something like Employee fred; or
Employee david; it is most likely badly structured.

You don't say how the user is supposed editing several employees at
once. Here is an easy example (code below) that does it by creating a
new form for each employee that is being edited. Notice how the
employee data is an instance variable inside the EditEmployeeForm. For
each form that is created the employee variable will contain data for
that specific form.

When the OpenTwoEditEmployeeForms() is called two EditEmployeeForms
will be displayed. One will contain information about Fred, while the
other will contain info about Donald.

class MainForm : Form
{
public void OpenTwoEditEmployeeForms()
{
EditEmployee("Fred");
EditEmployee("Donald");
}

public void EditEmployee(string employeeID)
{
Form form = new EditEmployeeForm(employeeID);
form.Show();
}
}

class EditEmployeeForm : Form
{
private Employee employee;

public MyUserEditControl(string employeeID)
{
employee = EmployeeFactory.Load(employeeID); //Load from database

//Fill textboxes with employee data here
}

// Call this method when the user of the system clicks the
// save/apply/ok button.
public void Save()
{
//Fill employee with textbox data here

EmployeeFactory.Save(employee); //Save to database
}
}

Instead of creating an EditEmployeeForm by inheriting from Form, other
choices could be to inherit from TabPage or the more generic
UserControl. In the case of editing one employee on each tabpage, the
main form code would look something like the following instead:

TabControl myTabControl = new TabControl();

public void EditEmployee(string employeeID)
{
//This creates the tabpage that is used to edit the employee
//and adds it to the tabcontrol in the mainform.
myTabControl.Pages.Add(
new EditEmployeeTabPage(employeeID));
}

--
Marcus Andrén
Nov 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.