Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 944
  • Last Modified:

adding subclass to the superclass array in vb.net?

Hi,

I am trying to add subclasses object to the superclass array in vb.net.
Can't find a good example.


I want to do this in vb.net
public class Animal {
     public int height;
     public int width;
   
}
public class Dog extends Animal {
    public int weight;
}
public class Cat extends Animal {
    public String color;
}

Public class MyMain {

Cat myCat = new Cat();
        myCat.height = 10;
        myCat.width = 20;
        myCat.weight = 300;
       
        Dog myDog = new Dog();
        myDog.height=100;
        myDog.width = 200;
        myDog.color = "yellow";
     
        Cat cat1= new Cat();
        Dog dog1= new Dog();

Animal[] animals;
        animals = new Animal[3];
        animals[0]=myCat;
        animals[1]=myDog;
       
       
        System.out.println("cat " + animals[0].height);
        System.out.println("cat " + animals[0].width);
        if (animals[0] instanceof Cat) {
            cat1 = (Cat) animals[0];
            System.out.println("cat " + cat1.weight);
        }
       
       
        System.out.println("dog " + animals[1].height);
        System.out.println("dog " + animals[1].width);
        if (animals[1] instanceof Dog) {
            dog1 = (Dog) animals[1];
            System.out.println("dog " + dog1.color);
        }
         
}
0
dkim18
Asked:
dkim18
2 Solutions
 
DhaestCommented:
Just tested with the following c# console-app

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Cat myCat = new Cat();
            myCat.Height = 10;
            myCat.Width = 20;
            myCat.Color = "yellow";
            

            Dog myDog = new Dog();
            myDog.Height = 100;
            myDog.Width = 200;
            myDog.Weight = 300;

            Animal[] animals;
            animals = new Animal[3];
            animals[0] = myCat;
            animals[1] = myDog;

            Console.WriteLine();

        Console.WriteLine("cat " + animals[0].Height);
        Console.WriteLine("cat " + animals[0].Width);

        if (animals[0].GetType() == typeof(Cat))
        {
            Console.WriteLine("cat " + ((Cat) animals[0]).Color);
        }

        if (animals[0].GetType() == typeof(Dog))
        {
            Console.WriteLine("cat " + ((Dog)animals[0]).Weight);
        }


        }
    }

    public class Animal {
     public int Height;
     public int Width;
    
}

    public class Dog : Animal 
    {
        public int Weight { get; set; }
    }

    public class Cat :Animal 
    {
        public String Color;
    }


}

Open in new window

0
 
käµfm³d 👽Commented:
Dhaest's example in VB:

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

Namespace ConsoleApplication5
	Class Program
		Private Shared Sub Main(args As String())
			Dim myCat As New Cat()
			myCat.Height = 10
			myCat.Width = 20
			myCat.Color = "yellow"


			Dim myDog As New Dog()
			myDog.Height = 100
			myDog.Width = 200
			myDog.Weight = 300

			Dim animals As Animal()
			animals = New Animal(2) {}
			animals(0) = myCat
			animals(1) = myDog

			Console.WriteLine()

			Console.WriteLine("cat " + animals(0).Height)
			Console.WriteLine("cat " + animals(0).Width)

			If animals(0).[GetType]() = GetType(Cat) Then
				Console.WriteLine("cat " + DirectCast(animals(0), Cat).Color)
			End If

			If animals(0).[GetType]() = GetType(Dog) Then
				Console.WriteLine("cat " + DirectCast(animals(0), Dog).Weight)
			End If


		End Sub
	End Class

	Public Class Animal
		Public Height As Integer
		Public Width As Integer

	End Class

	Public Class Dog
		Inherits Animal
		Public Property Weight() As Integer
			Get
				Return m_Weight
			End Get
			Set
				m_Weight = Value
			End Set
		End Property
		Private m_Weight As Integer
	End Class

	Public Class Cat
		Inherits Animal
		Public Color As [String]
	End Class


End Namespace

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik, @toddanglin
'Facebook: facebook.com/telerik
'=======================================================

Open in new window

0
 
dkim18Author Commented:
THanks. This is what I am looking for.

  if (animals[0].GetType() == typeof(Cat))
If animals(0).[GetType]() = GetType(Cat) Then
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Just personal preference...but I'd write it as:

        If TypeOf animals(0) Is Cat Then
            Console.WriteLine("cat " + DirectCast(animals(0), Cat).Color)
        End If
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now