Inheritance

T

tshad

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 SingleEngineLand
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.TailNumber
Get
Return FTailNumber
End Get
Set(ByVal Value As String)
FTailNumber = Value
End Set
End Property

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

End Class

Public Class DoubleEngineLand
Inherits SingleEngineLand
Engines = 2 <--- The error
End Class

Sub Main()

Dim myPlane As New SingleEngineLand
Dim myPlane2 As New SingleEngineLand
Dim myPlane3 As New DoubleEngineLand

myPlane.TailNumber = "150"
myPlane2.TailNumber = "2000"

Console.WriteLine(String.Format("My 1st Planes Tail Number is: {0}",
myPlane.TailNumber))
Console.WriteLine(String.Format("My 2nd Planes Tail Number is: {0}",
myPlane2.TailNumber))
Console.WriteLine(String.Format("My 1st Planes Number Wheels is
{0}", myPlane2.NumberOfWheels))
Console.WriteLine(String.Format("My 3rd Planes TailNumber is {0}",
myPlane3.TailNumber))
Console.WriteLine(String.Format("Number of engines in doubleEngine
Plane is {0}", myPlane3.NumberOfEngines))
Console.Read()
End Sub

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

What Do I have to do to access it.

I have Set up my SingleEnginePlane Class to be the default class and set
"Engine=1". I then create another class "DoubleEnginePlane" 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
 
C

Cor Ligthert[MVP]

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
 
T

Tom Shelton

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 SingleEngineLand
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.TailNumber
Get
Return FTailNumber
End Get
Set(ByVal Value As String)
FTailNumber = Value
End Set
End Property

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

End Class

Public Class DoubleEngineLand
Inherits SingleEngineLand
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...
 
T

tshad

Tom Shelton said:
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
 
C

Cor Ligthert[MVP]

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
 
T

tshad

Cor Ligthert said:
Tshad,



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 SingleEnginePlane Class to be the default class and set
"Engine=1". I then create another class "DoubleEnginePlane" where I want to
change the variable "Engine=2", but I get the error:

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

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

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

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top