none
Internal Classes RRS feed

  • Question

  • Hello all, I have a question about classes...

    I want to create a class that have sub class, but the function / method in main class, can be accessed by sub class, this what my code, and currently its not working:

    public class Class1
        {
            int ValueData;
            public Class1(int Value = 0)
            { ValueData = Value; }
    
            public int Value
            {
                get
                {
                    return ValueData;
                }
                set
                {
                    ValueData = value;
                }
            }
    
            public class Class2
            {
                int ValueData2;
                public Class2(int Value1 = 0, int Value2 = 0) : Class1(Value1) // : Class1(Value1) << this one is not working
                {
                    ValueData2 = Value2;
                }
    
                public int Value2
                {
                    get
                    {
                        return ValueData2;
                    }
                    set
                    {
                        ValueData2 = value;
                    }
                }
            }
        }

    I though this one is working on C++:

    public Class2(int Value1 = 0, int Value2 = 0) : Class1(Value1) // : Class1(Value1) << this one is not working in C#

    What I want to do is something like this:

    // What I want to do is something like this:
    Class1 test = new Class1.Class2(0, 0);
    test.Value = 10; // Set Value on Class1
    test.Value2 = 15; // Set Value on Class2

    Is it possible to do that? or There are other way to do this?

    Thanks




    • Edited by ChronO2Jam Tuesday, August 28, 2012 12:00 PM Renamed Title Thread, and the thread was solved, Hope useful if someone have the same problem
    Tuesday, August 28, 2012 11:37 AM

Answers

  • It's possible, your syntax is not quite correct:

        internal class SuperClass
        {
            public SuperClass(int value = 0)
            {
                this.Value = value;
            }
    
            public int Value { get; set; }
        }
    
        internal class SubClass : SuperClass
        {
            public SubClass(int value1 = 0, int value2 = 0)
                : base(value1)
            {
                this.Value2 = value2;
            }
    
            public int Value2 { get; set; }
        }

    • Marked as answer by ChronO2Jam Tuesday, August 28, 2012 11:58 AM
    Tuesday, August 28, 2012 11:45 AM

All replies

  • It's possible, your syntax is not quite correct:

        internal class SuperClass
        {
            public SuperClass(int value = 0)
            {
                this.Value = value;
            }
    
            public int Value { get; set; }
        }
    
        internal class SubClass : SuperClass
        {
            public SubClass(int value1 = 0, int value2 = 0)
                : base(value1)
            {
                this.Value2 = value2;
            }
    
            public int Value2 { get; set; }
        }

    • Marked as answer by ChronO2Jam Tuesday, August 28, 2012 11:58 AM
    Tuesday, August 28, 2012 11:45 AM
  • It's possible, your syntax is not quite correct:

        internal class SuperClass
        {
            public SuperClass(int value = 0)
            {
                this.Value = value;
            }
    
            public int Value { get; set; }
        }
    
        internal class SubClass : SuperClass
        {
            public SubClass(int value1 = 0, int value2 = 0)
                : base(value1)
            {
                this.Value2 = value2;
            }
    
            public int Value2 { get; set; }
        }

    WOW Thanks for very fast reply! and its working!

    Once again, thanks

    Tuesday, August 28, 2012 11:59 AM