Inheritance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tshad

    #1

    Inheritance

    I am playing with abstract classes and interfaces and in my sample program I
    can't understand why I can't access a variable that is in the inherited
    class. It is public and I thought that you could directly access
    (apparently not).

    *************** *************** *******
    Option Strict On

    Module Module1

    Public Interface Plane
    Property TailNumber() As String
    Property NumberOfEngines () As Integer
    End Interface

    Public Class SingleEngineLan d
    Implements Plane

    Private FTailNumber As String
    Public Engines As Integer = 1

    Public Function NumberOfWheels( ) As Integer
    Return 3
    End Function

    Public Property TailNumber() As String Implements Plane.TailNumbe r
    Get
    Return FTailNumber
    End Get
    Set(ByVal Value As String)
    FTailNumber = Value
    End Set
    End Property

    Public Property NumberOfEngines () As Integer Implements
    Plane.NumberOfE ngines
    Get
    Return Engines
    End Get
    Set(ByVal Value As Integer)
    Engines = Value
    End Set
    End Property

    End Class

    Public Class DoubleEngineLan d
    Inherits SingleEngineLan d
    Engines = 2 <--- The error
    End Class

    Sub Main()

    Dim myPlane As New SingleEngineLan d
    Dim myPlane2 As New SingleEngineLan d
    Dim myPlane3 As New DoubleEngineLan d

    myPlane.TailNum ber = "150"
    myPlane2.TailNu mber = "2000"

    Console.WriteLi ne(String.Forma t("My 1st Planes Tail Number is: {0}",
    myPlane.TailNum ber))
    Console.WriteLi ne(String.Forma t("My 2nd Planes Tail Number is: {0}",
    myPlane2.TailNu mber))
    Console.WriteLi ne(String.Forma t("My 1st Planes Number Wheels is
    {0}", myPlane2.Number OfWheels))
    Console.WriteLi ne(String.Forma t("My 3rd Planes TailNumber is {0}",
    myPlane3.TailNu mber))
    Console.WriteLi ne(String.Forma t("Number of engines in doubleEngine
    Plane is {0}", myPlane3.Number OfEngines))
    Console.Read()
    End Sub

    End Module
    *************** *************** *******

    What Do I have to do to access it.

    I have Set up my SingleEnginePla ne Class to be the default class and set
    "Engine=1". I then create another class "DoubleEnginePl ane" where I want to
    change the variable "Engine=2", but I get the error:

    Declaration expected

    Shouldn't it be able to access the variable in the Base Class directly?

    Thanks,

    Tom


  • Tom Shelton

    #2
    Re: Inheritance

    On Oct 15, 9:54 pm, "tshad" <t...@dslextrem e.comwrote:
    I am playing with abstract classes and interfaces and in my sample program I
    can't understand why I can't access a variable that is in the inherited
    class. It is public and I thought that you could directly access
    (apparently not).
    >
    *************** *************** *******
    Option Strict On
    >
    Module Module1
    >
    Public Interface Plane
    Property TailNumber() As String
    Property NumberOfEngines () As Integer
    End Interface
    >
    Public Class SingleEngineLan d
    Implements Plane
    >
    Private FTailNumber As String
    Public Engines As Integer = 1
    >
    Public Function NumberOfWheels( ) As Integer
    Return 3
    End Function
    >
    Public Property TailNumber() As String Implements Plane.TailNumbe r
    Get
    Return FTailNumber
    End Get
    Set(ByVal Value As String)
    FTailNumber = Value
    End Set
    End Property
    >
    Public Property NumberOfEngines () As Integer Implements
    Plane.NumberOfE ngines
    Get
    Return Engines
    End Get
    Set(ByVal Value As Integer)
    Engines = Value
    End Set
    End Property
    >
    End Class
    >
    Public Class DoubleEngineLan d
    Inherits SingleEngineLan d
    Engines = 2 <--- The error
    End Class
    >
    You can't access engines like that outside of a method/property. You
    need to add a sub new:

    Public Sub New ()
    Engines = 2
    End Sub

    The compiler thinks your trying to declare a new variable...

    --
    Tom Shelton

    Comment

    • Cor Ligthert[MVP]

      #3
      Re: Inheritance

      Tshad,

      If you send a sample then please make it as short as possible.

      If you would do the same and remove by instance all the not relevant
      Interface stuff, then you would see that you either are in your end class
      are creating a new engine or setting the value of it outside a method.

      Cor

      Comment

      • tshad

        #4
        Re: Inheritance

        "Tom Shelton" <tom_shelton@co mcast.netwrote in message
        news:1192507913 .720714.246970@ e9g2000prf.goog legroups.com...
        On Oct 15, 9:54 pm, "tshad" <t...@dslextrem e.comwrote:
        >I am playing with abstract classes and interfaces and in my sample
        >program I
        >can't understand why I can't access a variable that is in the inherited
        >class. It is public and I thought that you could directly access
        >(apparently not).
        >>
        >************** *************** ********
        >Option Strict On
        >>
        >Module Module1
        >>
        > Public Interface Plane
        > Property TailNumber() As String
        > Property NumberOfEngines () As Integer
        > End Interface
        >>
        > Public Class SingleEngineLan d
        > Implements Plane
        >>
        > Private FTailNumber As String
        > Public Engines As Integer = 1
        >>
        > Public Function NumberOfWheels( ) As Integer
        > Return 3
        > End Function
        >>
        > Public Property TailNumber() As String Implements
        >Plane.TailNumb er
        > Get
        > Return FTailNumber
        > End Get
        > Set(ByVal Value As String)
        > FTailNumber = Value
        > End Set
        > End Property
        >>
        > Public Property NumberOfEngines () As Integer Implements
        >Plane.NumberOf Engines
        > Get
        > Return Engines
        > End Get
        > Set(ByVal Value As Integer)
        > Engines = Value
        > End Set
        > End Property
        >>
        > End Class
        >>
        > Public Class DoubleEngineLan d
        > Inherits SingleEngineLan d
        > Engines = 2 <--- The error
        > End Class
        >>
        >
        You can't access engines like that outside of a method/property. You
        need to add a sub new:
        >
        Public Sub New ()
        Engines = 2
        End Sub
        >
        The compiler thinks your trying to declare a new variable...
        Of course. Which is exactly what the error said.

        Little brain fade there. :(

        Thanks,

        Tom
        >
        --
        Tom Shelton
        >

        Comment

        • Cor Ligthert[MVP]

          #5
          Re: Inheritance

          Tshad,

          Of course. Which is exactly what the error said.
          Why did you than not write it in your message instead of copying all the
          code?

          Now both Tom and me had to look for it inside this code and when I saw Tom
          answer,which was obvious sent in the same time as mine, I was lucky to see
          his answer. But he needed of course almost the same time as I to see it. It
          was so obvious that it took not much time, however it had been easier with
          the error message.

          Cor

          Comment

          • tshad

            #6
            Re: Inheritance


            "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
            news:8C01FC46-C59D-493C-83A8-135A71A2E218@mi crosoft.com...
            Tshad,
            >
            >
            >Of course. Which is exactly what the error said.
            >
            Why did you than not write it in your message instead of copying all the
            code?
            >
            Not sure what you are talking about:

            Here is the what I said:

            *************** *************** *************
            I have Set up my SingleEnginePla ne Class to be the default class and set
            "Engine=1". I then create another class "DoubleEnginePl ane" where I want to
            change the variable "Engine=2", but I get the error:

            Declaration expected
            *************** *************** *************** ****

            The "Declaratio n expected" is what the error was and that was what Tom said:

            *************** *************** ***********
            The compiler thinks you're trying to declare a new variable...
            *************** *************** ***********

            Tom

            Now both Tom and me had to look for it inside this code and when I saw Tom
            answer,which was obvious sent in the same time as mine, I was lucky to see
            his answer. But he needed of course almost the same time as I to see it.
            It was so obvious that it took not much time, however it had been easier
            with the error message.
            >
            Cor

            Comment

            • Cor Ligthert[MVP]

              #7
              Re: Inheritance

              Tshad,

              You are right,

              Sorry

              Cor

              Comment

              • tshad

                #8
                Re: Inheritance

                "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
                news:3B6F63FD-C3C9-46CD-8689-6F7ED621C5FE@mi crosoft.com...
                Tshad,
                >
                You are right,
                Sorry
                That's OK.

                It was kind of stuffed in all the other stuff, so easy to miss.

                Thanks,

                Tom
                >
                Cor

                Comment

                Working...