ʃʈɑɲ
ʃʈɑɲ

Reputation: 2694

VB.Net Type conversion

I'm trying to get my head back into VB.Net (been a while) and can't seem to get around this simple problem.

Here is my abstract base class with its interface:

Imports VBRefresh.Animal

Public Interface IAnimal
    ReadOnly Property AnimalType() As TypeOfAnimal
    Property IsAlive As Boolean
End Interface

Public MustInherit Class Animal
    Implements IAnimal

    Public Enum TypeOfAnimal
        Insect
        Mammal
        Fish
        Bird
        Reptile
    End Enum

    Private _animalType As TypeOfAnimal
    Public ReadOnly Property AnimalType As TypeOfAnimal Implements IAnimal.AnimalType
        Get
            Return _animalType
        End Get
    End Property

    Private _isAlive As Boolean
    Public Property IsAlive() As Boolean Implements IAnimal.IsAlive
        Get
            Return _isAlive
        End Get
        Set(ByVal value As Boolean)
            _isAlive = value
        End Set
    End Property

    Public Sub New(animalType As TypeOfAnimal)
        _animalType = animalType
    End Sub

End Class

And here are two derived classes (of which Cow is extending IAnimal):

Public Interface ICow
    Inherits IAnimal

    Property FriendlyName As String
End Interface

Public Class Cow
    Inherits Animal
    Implements ICow

    Public Sub New()
        MyBase.New(TypeOfAnimal.Mammal)
    End Sub

    Private _friendlyName As String
    Public Property FriendlyName As String Implements ICow.FriendlyName
        Get
            Return _friendlyName
        End Get
        Set(value As String)
            _friendlyName = value
        End Set
    End Property

End Class

Public Class Eagle
    Inherits Animal

    Public Sub New()
        MyBase.New(TypeOfAnimal.Bird)
    End Sub

End Class

I'm trying to change type of myAnimal to a Cow so I can access it's property FriendlyName but I can't get it to work without

  1. Creating a seperate object and setting that.
  2. Declaring FriendlyName in the IAnimal interface.. I tried casting myAnimal to ICow but that doesn't give me access to the FriendlyName property?

    Private Sub Main_Load(sender As Object, e As EventArgs) Handles Me.Load
    
      Dim myAnimal As IAnimal = New Eagle
      myAnimal.IsAlive = True
    
      myAnimal = New Cow
      myAnimal.FriendlyName = "Bella" 'this line doesn't work
    
      Dim myCow As ICow = New Cow
      myCow.FriendlyName = "Bella"
    
      MessageBox.Show(myAnimal.AnimalType.ToString)
    End Sub
    

Probably something stupid but I'm running out of options here.

Upvotes: 2

Views: 88

Answers (2)

ʃʈɑɲ
ʃʈɑɲ

Reputation: 2694

If I would like to use the newly cast object as an object itself,
I need to create an object of type ICow to which I assign the casted object:

Public Class Main
    Private Sub Main_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim myAnimal As IAnimal = New Eagle
        myAnimal.IsAlive = True

        myAnimal = New Cow
        myAnimal.FriendlyName = "Bella" 'this line doesn't work

        myAnimal = DirectCast(myAnimal, ICow)
        myAnimal.FirendlyName = "Bella" 'this is still unavailble?

        'guess I have to solve it this way then:
        Dim myCow As ICow = DirectCast(myAnimal, ICow)
        myCow.FriendlyName = "Bella"

        MessageBox.Show(myAnimal.AnimalType.ToString)
    End Sub
End Class

Upvotes: 0

user11909
user11909

Reputation: 1265

You declared myAnimal as IAnimal. IAnimal doesn't have the Property FriendlyName.

Therefore you have to cast IAnimal to Cow:

DirectCast(myAnimal, Cow).FriendlyName = "Bella"

OR

DirectCast(myAnimal, ICow).FriendlyName = "Bella"

Upvotes: 2

Related Questions