none
Set value of Child Property that Inherits from Part/Base RRS feed

  • Question

  • I know my syntax is wrong but I have no idea how to do this correctly or what the best way would be. I have a base class or parent class. My child class inherits from it and I'd like to define the parent properties in the child class. The idea is that the child class a "copy" of the parent class but you define the values in the child class and basically create a new object based off the properties of the Parent. Not sure that makes sense or can be done/created. 


    Parent:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Test
    {
        public class Base_Parent
        {
            public string Name { get; set; }
            public string Value_1 { get; set; }
            public string Value_2 { get; set; }
            public int Display_Value_1 { get; set; }
            public int Display_Value_2 { get; set; }
        }
    }

    Child:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Test
    {
        public class Child_1 : Base_Parent
        {
            Base_Parent.Name = "a Name";
            Base_Parent.Value_1 = "Value 1";
            Base_Parent.Value_2= "Value 2";
            Base_Parent.Display_Value_1 = 1;
            Base_Parent.Display_Value_2 = 2;
        }
    }
    

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Test
    {
        class Child_2 :Base_Parent
        {
            Base_Parent.Name = "a Name";
            Base_Parent.Value_1 = "Value 3";
            Base_Parent.Value_2= "Value 4";
            Base_Parent.Display_Value_1 = 3;
            Base_Parent.Display_Value_2 = 4;
        }
    }

    Monday, March 18, 2019 7:03 PM

Answers

  • Did you already try constructors?

    public class Child_1 : Base_Parent

    {

        public Child_1()

        {

            Name = "a Name";

            Value_1 = "Value 1";

            Value_2 = "Value 2";

            Display_Value_1 = 1;

            Display_Value_2 = 2;

        }

    }

    • Marked as answer by old_School Monday, March 18, 2019 8:15 PM
    Monday, March 18, 2019 7:57 PM

All replies

  • Trying this as well but gives error:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Test
    {
        public class Child_1 : Base_Parent
        {
            public override new string Name = "a Name";
            public override new string Value_1 = "Value 1";
            public override new string Value_2 = "Value 2";
            public override new int Display_Value_1 = 1;
            public override new int Display_Value_2 = 2;
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Test
    {
        class Child_2 : Base_Parent
        {
            public override new string Name = "a Name";
            public override new string Value_1 = "Value 3";
            public override new string Value_2 = "Value 4";
            public override new int Display_Value_1 = 3;
            public override new int Display_Value_2 = 4;
        }
    }
    

    Error:

    The modifier 'override' is not valid for this item

    Monday, March 18, 2019 7:09 PM
  • Well I found a solution but I hope someone knows a better way:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Test
    {
        public class Child_1 : Base_Parent
        {
            public Base_Parent obj = new Base_Parent();
            public object Get_obj()
            {
                obj.Name = "a Name";
                obj.Value_1 = "Value 1";
                obj.Value_2 = "Value 2";
                obj.Display_Value_1 = 1;
                obj.Display_Value_2 = 2;
    
                return obj;
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Test
    {
        class Child_2 : Base_Parent
        {
            public Base_Parent obj = new Base_Parent();
            public object Get_obj()
            {
                obj.Name = "a Name";
                obj.Value_1 = "Value 3";
                obj.Value_2 = "Value 4";
                obj.Display_Value_1 = 3;
                obj.Display_Value_2 = 4;
    
                return obj;
            }
        }
    }

    Is there a way to build this class without needing to call a function though? Where the values would be preset for me based on the parent class properties? 

    Basically what I want to do is call a object and the properties already be predefined statically but I want the objects properties to be based on a base class. That make sense?


    • Edited by old_School Monday, March 18, 2019 7:32 PM
    Monday, March 18, 2019 7:21 PM
  • use :base

    Something like this?

    class Dog
        {
            string name, value_1, value_2;

            public Dog(string name, int age)
            {

            }
        }
        class Havanese : Dog
        {
            public Havanese(string name, int age , string fancy_haircut): base(name, age)
            {
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                Havanese havanese = new Havanese("Balder",6,"mohawk");
            }
        }
    Monday, March 18, 2019 7:55 PM
  • Did you already try constructors?

    public class Child_1 : Base_Parent

    {

        public Child_1()

        {

            Name = "a Name";

            Value_1 = "Value 1";

            Value_2 = "Value 2";

            Display_Value_1 = 1;

            Display_Value_2 = 2;

        }

    }

    • Marked as answer by old_School Monday, March 18, 2019 8:15 PM
    Monday, March 18, 2019 7:57 PM
  • Thanks thats what I needed. Never thought of a constructor 
    Monday, March 18, 2019 8:16 PM