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);
}
}
}
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);
}
}
}