Very basic OOP question
-
I have a simple Employee Class as follows: I want to create a new Employee object from a different Class having Main Can I return the Complete Employee object from here when Main class uses GetEmployee method. The code below errors out saying variable e in last method below is undefined
namespace TestEmployee
{
class Employee
{int eId; string eName; double age; public Employee(int i, string n, double a) { eId = i; eName = n; age = a; } public Employee() { eId = 1105; eName = "Peter Cava"; age = 32 ; } public Employee GetEmployee() { Employee e; e.eId = this.eId; e.eName = this.eName; e.eId = this.eId; return e; } }
}
-
I have a simple Employee Class as follows: I want to create a new Employee object from a different Class having Main Can I return the Complete Employee object from here when Main class uses GetEmployee method. The code below errors out saying variable e in last method below is undefined
namespace TestEmployee
{
class Employee
{int eId; string eName; double age; public Employee(int i, string n, double a) { eId = i; eName = n; age = a; } public Employee() { eId = 1105; eName = "Peter Cava"; age = 32 ; } public Employee GetEmployee() { Employee e; e.eId = this.eId; e.eName = this.eName; e.eId = this.eId; return e; } }
}
Employee e **= new Employee()**;
(Though I don't know why you're cloning the Employee.) -
I have a simple Employee Class as follows: I want to create a new Employee object from a different Class having Main Can I return the Complete Employee object from here when Main class uses GetEmployee method. The code below errors out saying variable e in last method below is undefined
namespace TestEmployee
{
class Employee
{int eId; string eName; double age; public Employee(int i, string n, double a) { eId = i; eName = n; age = a; } public Employee() { eId = 1105; eName = "Peter Cava"; age = 32 ; } public Employee GetEmployee() { Employee e; e.eId = this.eId; e.eName = this.eName; e.eId = this.eId; return e; } }
}
If you want to create a new employee from your main method you should just use:
Employee myEmployee = new Employee();
if you really want to have a cloned object you should have a look at the Interface ICloneable for implementing the right methods.
-
If you want to create a new employee from your main method you should just use:
Employee myEmployee = new Employee();
if you really want to have a cloned object you should have a look at the Interface ICloneable for implementing the right methods.
thanks for the reply I did create a new Employee Object
class Employee
{int eId; string eName; double age; public Employee(int i, string n, double a) { eId = i; eName = n; age = a; } public Employee() { } public Employee GetEmployee() { Employee e = new Employee(); e.eId = this.eId; e.eName = this.eName; e.age = this.age; return e; } }
and calling this from a form where I give the 3 inputs and want to see it in new set of fields
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { int i1 = Convert.ToInt32(textBox1.Text); string nm = textBox2.Text; double d2 = Convert.ToDouble(textBox3.Text); Employee em = new Employee(i1, nm, d2); em.GetEmployee(); // Iam stuck on how to get the individual fields and display in separate box below. textBox5.Text = em.GetEmployee?? for Employee id from em object textBox6.Text = em.GetEmployee?? for Employee name from em object textBox6.Text = em.GetEmployee?? for Employee age from em object }
-
thanks for the reply I did create a new Employee Object
class Employee
{int eId; string eName; double age; public Employee(int i, string n, double a) { eId = i; eName = n; age = a; } public Employee() { } public Employee GetEmployee() { Employee e = new Employee(); e.eId = this.eId; e.eName = this.eName; e.age = this.age; return e; } }
and calling this from a form where I give the 3 inputs and want to see it in new set of fields
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { int i1 = Convert.ToInt32(textBox1.Text); string nm = textBox2.Text; double d2 = Convert.ToDouble(textBox3.Text); Employee em = new Employee(i1, nm, d2); em.GetEmployee(); // Iam stuck on how to get the individual fields and display in separate box below. textBox5.Text = em.GetEmployee?? for Employee id from em object textBox6.Text = em.GetEmployee?? for Employee name from em object textBox6.Text = em.GetEmployee?? for Employee age from em object }
Well the problem is that all your variables in the employee class don't have public, or private set. In that case they will be private and only be seen within the class Employee. To manage that the correct way, C# offers you the posibility to create so called "Properties". I've created properties as you see in the code of the class.
public class Employee
{// This is another example of writing properties
/*
private int eId;
private string eName;
private double age;public int EId { get{return eId;} set{eId = value;} } public string EName { get{return eName;} set{eName= value;} } public int Age { get{return age;} set{age= value;} }
*/
// Properties
public int EId{get;set;}
public string EName{get;set;}
public double Age{get;set;public Employee(int i, string n, double a) { eId = i; eName = n; age = a; } public Employee() { }
}
In the Form1 class you can access the properties pretty simple by creating a new Employee with Employee em = new Employee(); and access each property like: em.Name
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { int i1 = Convert.ToInt32(textBox1.Text); string nm = textBox2.Text; double d2 = Convert.ToDouble(textBox3.Text); Employee em = new Employee(i1, nm, d2); // Iam stuck on how to get the individual fields and display in separate box below. textBox5.Text = em.EId.ToString(); textBox6.Text = em.EName; textBox6.Text = em.Age.ToString(); }
Hope this helps.
-
Well the problem is that all your variables in the employee class don't have public, or private set. In that case they will be private and only be seen within the class Employee. To manage that the correct way, C# offers you the posibility to create so called "Properties". I've created properties as you see in the code of the class.
public class Employee
{// This is another example of writing properties
/*
private int eId;
private string eName;
private double age;public int EId { get{return eId;} set{eId = value;} } public string EName { get{return eName;} set{eName= value;} } public int Age { get{return age;} set{age= value;} }
*/
// Properties
public int EId{get;set;}
public string EName{get;set;}
public double Age{get;set;public Employee(int i, string n, double a) { eId = i; eName = n; age = a; } public Employee() { }
}
In the Form1 class you can access the properties pretty simple by creating a new Employee with Employee em = new Employee(); and access each property like: em.Name
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { int i1 = Convert.ToInt32(textBox1.Text); string nm = textBox2.Text; double d2 = Convert.ToDouble(textBox3.Text); Employee em = new Employee(i1, nm, d2); // Iam stuck on how to get the individual fields and display in separate box below. textBox5.Text = em.EId.ToString(); textBox6.Text = em.EName; textBox6.Text = em.Age.ToString(); }
Hope this helps.
Oh My, cant believe I dropped the ball on private and public declaration. Thanks very much for the details and how to use the properties. One last question please in the Employee class, if I have to return as an object I need to have a variable of the class and do a new, right like in
Employee e = new Employee();
in code below.
class Employee
{public int eId; public string eName; public double age; public Employee(int i, string n, double a) { eId = i; eName = n; age = a; } public Employee() { } public Employee GetEmployee() { Employee e = new Employee(); e.eId = this.eId; e.eName = this.eName; e.eId = this.eId; return e; }
-
Oh My, cant believe I dropped the ball on private and public declaration. Thanks very much for the details and how to use the properties. One last question please in the Employee class, if I have to return as an object I need to have a variable of the class and do a new, right like in
Employee e = new Employee();
in code below.
class Employee
{public int eId; public string eName; public double age; public Employee(int i, string n, double a) { eId = i; eName = n; age = a; } public Employee() { } public Employee GetEmployee() { Employee e = new Employee(); e.eId = this.eId; e.eName = this.eName; e.eId = this.eId; return e; }
You should not expose the class variables as public. Rather good design uses property accessors. That way any logic that may need to be executed when a property is set or accessed will be executed. You don't always need an instance of a class to access it's properties or methods.
public class Employee
{
private static int m_Id;public static Foo()
{}
public static int Id{get; set;}
}int id = Employee.Id;
Employee.Foo();Static methods and properties are not bound to an instance of a class. The Factory pattern quite often uses this to create instances
public class Employee
{
public static Employee CreateEmployee()
{
return new Employee();
}
}Employee e = Employee.CreateEmployee();
No comment
-
Oh My, cant believe I dropped the ball on private and public declaration. Thanks very much for the details and how to use the properties. One last question please in the Employee class, if I have to return as an object I need to have a variable of the class and do a new, right like in
Employee e = new Employee();
in code below.
class Employee
{public int eId; public string eName; public double age; public Employee(int i, string n, double a) { eId = i; eName = n; age = a; } public Employee() { } public Employee GetEmployee() { Employee e = new Employee(); e.eId = this.eId; e.eName = this.eName; e.eId = this.eId; return e; }
-
I have a simple Employee Class as follows: I want to create a new Employee object from a different Class having Main Can I return the Complete Employee object from here when Main class uses GetEmployee method. The code below errors out saying variable e in last method below is undefined
namespace TestEmployee
{
class Employee
{int eId; string eName; double age; public Employee(int i, string n, double a) { eId = i; eName = n; age = a; } public Employee() { eId = 1105; eName = "Peter Cava"; age = 32 ; } public Employee GetEmployee() { Employee e; e.eId = this.eId; e.eName = this.eName; e.eId = this.eId; return e; } }
}
-
You should not expose the class variables as public. Rather good design uses property accessors. That way any logic that may need to be executed when a property is set or accessed will be executed. You don't always need an instance of a class to access it's properties or methods.
public class Employee
{
private static int m_Id;public static Foo()
{}
public static int Id{get; set;}
}int id = Employee.Id;
Employee.Foo();Static methods and properties are not bound to an instance of a class. The Factory pattern quite often uses this to create instances
public class Employee
{
public static Employee CreateEmployee()
{
return new Employee();
}
}Employee e = Employee.CreateEmployee();
No comment