newbie question

O

OO_newbie

Hi, I'm very new to OO, and I don't understand some things... can someone
please explain: (this code is from msdn help and it works)

1. In the code below, in the Main method, how is it possible that this code
works well, I'm confused by this:

Employee.numberOfEmployees = 100;
Employee e1 = new Employee();

How is it possible to assign a value to a variable numberOfEmployees, before
I've created an instance of Employee class?
Cause, in this, I first assign a value to that variable and then I create an
instance of Employee class.
How can that be?
I assign a value to an object that doesn't exist yet?
Is it because it's a static variable?
Then, this means that I can assign a value to a static variable before the
class is instantiated?

2. Properties are also a little bit confusing stuff to me...when I read
theory,it's fine,I can understand what a property is, but in code, the
confusing is that the properties seem very similar to methods... when would
I want to use properties, instead of "plain" variables?

3.Also, how to decide which variable to declare as private and which as
public?

here's the confusing code:
___________________________________________________
using System;
namespace props
{
public class Employee
{
public static int numberOfEmployees;
private static int counter;
private string name;
//read-write instance property
public string Name
{
get{return name;}
set{name = value;}
}
//read only static property
public static int Counter
{get{return counter;}}
// Constructor:
public Employee()
{
// Calculate the employee's number:
counter = ++counter + numberOfEmployees;
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
Employee.numberOfEmployees = 100;
Employee e1 = new Employee();
e1.Name = "Claude Vige";
Console.WriteLine("Employee number: " + Employee.Counter);
Console.WriteLine("Employee name: {0}", e1.Name);
}
}
}
 
O

Olaf Baeyens

1. In the code below, in the Main method, how is it possible that this
code
works well, I'm confused by this:

Employee.numberOfEmployees = 100;
Employee e1 = new Employee();
It should not work, under normal conditions.;

You either you do this:
Employee e1 = new Employee();
e1 .numberOfEmployees = 100;

Or it could be contained in the constructor
Employee e1 = new Employee(100);

The reason why Employee.numberOfEmployees = 100; does not crash before
creating this on the heap is that the variable is created as 'static' on in
the class meaning that it is the same thing as a global defined variable.
Static variables should be avoided unless you specially design them to be
that.
2. Properties are also a little bit confusing stuff to me...when I read
theory,it's fine,I can understand what a property is, but in code, the
confusing is that the properties seem very similar to methods... when would
I want to use properties, instead of "plain" variables?
Properties are actual methods, and can be a get end/or set method.
The nice thng is that they appear to be normal variables, but in reality
they can do more.
Basically it makes the code that uses that clase far more readable.
3.Also, how to decide which variable to declare as private and which as
public?
Try as much to use private, unless you have to make it public.
 
P

Peter van der Goes

OO_newbie said:
Hi, I'm very new to OO, and I don't understand some things... can someone
please explain: (this code is from msdn help and it works)

1. In the code below, in the Main method, how is it possible that this
code works well, I'm confused by this:

Employee.numberOfEmployees = 100;
Employee e1 = new Employee();

How is it possible to assign a value to a variable numberOfEmployees,
before I've created an instance of Employee class?
Cause, in this, I first assign a value to that variable and then I create
an instance of Employee class.
How can that be?
I assign a value to an object that doesn't exist yet?
Is it because it's a static variable?
Then, this means that I can assign a value to a static variable before the
class is instantiated?

2. Properties are also a little bit confusing stuff to me...when I read
theory,it's fine,I can understand what a property is, but in code, the
confusing is that the properties seem very similar to methods... when
would I want to use properties, instead of "plain" variables?

3.Also, how to decide which variable to declare as private and which as
public?

here's the confusing code:
___________________________________________________
using System;
namespace props
{
public class Employee
{
public static int numberOfEmployees;
private static int counter;
private string name;
//read-write instance property
public string Name
{
get{return name;}
set{name = value;}
}
//read only static property
public static int Counter
{get{return counter;}}
// Constructor:
public Employee()
{
// Calculate the employee's number:
counter = ++counter + numberOfEmployees;
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
Employee.numberOfEmployees = 100;
Employee e1 = new Employee();
e1.Name = "Claude Vige";
Console.WriteLine("Employee number: " + Employee.Counter);
Console.WriteLine("Employee name: {0}", e1.Name);
}
}
}
1. Yes, it is because numberOfEmployees is a static class member, not an
instance member. As it is declared as static, only one instance exists,
regardless of how many (or few) Employee objects exist. It exists before the
first Employee object is created and persists even if all Employee objects
are destroyed.
2. Properties provide controlled access to private datamembers, therefore
they are methods of a sort. The private datamember is exposed through the
property, allowing controlled exposure. Examples: a property with no "set"
creates a read-only datamember (from outside the class). Adding code to
"set" to restrict the allowable values, controls values that can be stored
in a datamember. For example, if you had a datamember hour and a property
Hour as part of time on a clock, you could write code in Hour.set to
disallow values < 0 and > 23.
3. The access specifier for a given class member has to be decided on a
case-by-case basis. In general, datamembers should be private so that
uncontrolled access is disallowed, unless there is an overwhelming reason
for less restriction. Typically, class designers try to preserve
encapsulation by not allowing code outside the class to access datamembers
directly. Public methods to allow *controlled* access to private data.
 
J

Jeff Connelly

OO_newbie said:
Hi, I'm very new to OO, and I don't understand some things... can someone
please explain: (this code is from msdn help and it works)

1. In the code below, in the Main method, how is it possible that this
code works well, I'm confused by this:

Employee.numberOfEmployees = 100;
Employee e1 = new Employee();

How is it possible to assign a value to a variable numberOfEmployees,
before I've created an instance of Employee class?

Well you did not do this
e1.numberOfEmployees = 100;

"e1" is an instance of Employee class. But "Employee" is not an instance of
Employee class. If you look at the Employee class, I think you will
probably find that "numberOfEmployees" is static, which means you don't need
an instance of Employee for it to exist.
Is it because it's a static variable?
Then, this means that I can assign a value to a static variable before the
class is instantiated?
Yes.

2. Properties are also a little bit confusing stuff to me...when I read
theory,it's fine,I can understand what a property is, but in code, the
confusing is that the properties seem very similar to methods... when
would I want to use properties, instead of "plain" variables?

You never have to use Properties, and under the covers they are implemented
the same way as methods. However, you might want to consider what an
"inline" function is. There is more of a performance hit when you call a
function, so sometimes the compiler just sticks in the variable name without
you knowing it, without actually calling the function. So if you had a
simply function that was
void f()
{
i = 3;
}
Then the compiler will usually just stick "i=3" in the code where you have
f(); And this is basically what Properties do for you - allow you to "call"
it, but without using the parentheses. Use it whenever you want really -
it's personal preference.
3.Also, how to decide which variable to declare as private and which as
public?

The general rule of thumb is that all variables are private or protected.
Only properties and methods are public. There can be exceptions, but the OO
principle is called "information hiding". Keep the implementation separate
from the public interface.
 
O

OO_newbie

Hi guys, I just want to thank you all, things are much clearer now! :)
but it is obvious that I need to dig deeper in OO concepts... if there's any
recommendations, I'd be thankful...
now I'm combining several books... Visual C# step by step by John Sharp,
Thinking in C++ by B.Eckel and some more... but I'd like to gain the clear
picture of basic concepts without the 1000+ pages book...you know, just how
to start thinking in OO world...
anyway, thanks to all of you!!!
 
O

Olaf Baeyens

Hi guys, I just want to thank you all, things are much clearer now! :)
but it is obvious that I need to dig deeper in OO concepts... if there's any
recommendations, I'd be thankful...
I don't now about books, but I do not that you should do reverse thinking
compared to none-OOP languags and this is problematic for people used to
think the none-OOP way. It took me 2 years to finally play with the OOP
thing.

The nice thing of OOP is that you can create far bigger programs than you
could do in none-OOP. But you lose a little speed, and you must create the
classes in a good way, or else you end into troubles.

One good way of looking at OOP way of doing is assuming that every class is
a mini-program. It does one thing.But working together with other
mini-programs to do one complex thing.
 

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