Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Very basic OOP question

Very basic OOP question

Scheduled Pinned Locked Moved C#
question
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SFORavi
    wrote on last edited by
    #1

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

    }

    P M A 3 Replies Last reply
    0
    • S SFORavi

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

      }

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Employee e **= new Employee()**; (Though I don't know why you're cloning the Employee.)

      1 Reply Last reply
      0
      • S SFORavi

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

        }

        M Offline
        M Offline
        Michael Handschuh
        wrote on last edited by
        #3

        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.

        S 1 Reply Last reply
        0
        • M Michael Handschuh

          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.

          S Offline
          S Offline
          SFORavi
          wrote on last edited by
          #4

          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 
          
          
          
          
          
              }
          
          M 1 Reply Last reply
          0
          • S SFORavi

            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 
            
            
            
            
            
                }
            
            M Offline
            M Offline
            Michael Handschuh
            wrote on last edited by
            #5

            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.

            S 1 Reply Last reply
            0
            • M Michael Handschuh

              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.

              S Offline
              S Offline
              SFORavi
              wrote on last edited by
              #6

              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;
                  }
              
              N B 2 Replies Last reply
              0
              • S SFORavi

                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;
                    }
                
                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #7

                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

                S 1 Reply Last reply
                0
                • S SFORavi

                  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;
                      }
                  
                  B Offline
                  B Offline
                  BobJanova
                  wrote on last edited by
                  #8

                  I don't understand what the purpose of GetEmployee is. It is functionally doing a clone but that seems to be unnecessary.

                  1 Reply Last reply
                  0
                  • S SFORavi

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

                    }

                    A Offline
                    A Offline
                    arya1685
                    wrote on last edited by
                    #9

                    Hi, Just creating an Instance of Employee Class...... :laugh:

                    Thanks & Regards Arya1685 P Before printing think about your responsibility & commitment with the Environment! ü

                    1 Reply Last reply
                    0
                    • N Not Active

                      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

                      S Offline
                      S Offline
                      SFORavi
                      wrote on last edited by
                      #10

                      Thanks very much Mark and Bob for the response. I think I am slowly getting it. Thanks, Ravi

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups