problem creating an array of instances where instance could be base or sub class

keithstruwig

New member
Joined
Oct 31, 2011
Messages
1
Programming Experience
10+
I have a class structure and am attempting to create an array of object instances from the class some instances are parents and some are children
Code snippets,

Parent Class object
Option Strict On
Option Explicit On
Public Class cTransportItem

Friend mName As String
Friend mWeight As Double
Friend mTransportStarDate As Double
....


Sub-class object (one of them)
Option Strict On
Option Explicit On
Public Class cCrew

Inherits cTransportItem

Public mCrewLevel As Integer

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


Option Strict On
Option Explicit On
Public Class semtestb

Private mNoItems As Integer
Private mItemType As Integer
Private TransportArray() As cTransportItem
.
.
.
Private Sub cmdInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInput.Click
For i = 1 To mNoItems
mItemType = CInt(InputBox("Input Item Type : 1 = Crew, 2 = Supply, 3 = Engineer, 4 = Officers"))
Select Case mItemType
Case 1
TransportArray(i) = New cCrew
TransportArray(i).Name = InputBox("Input Name for Item " & CStr(i))
TransportArray(i).Weight = CDbl(InputBox("Input Weight for Item " & CStr(i)))
TransportArray(i).TransportStarDate = CDbl(InputBox("Input Transport Star Date for Item " & CStr(i)))
TransportArray(i).crewlevel = CInt(InputBox("Input Crew level for Item " & CStr(i)))
TransportArray(i) = cCrew.IsHuman

(ishuman is a function method on each class and subclass)
These last two lines give errors respectively

'crewlevel' is not a member of 'WindowsApplication.cTransportItem'
Reference to a non shared member requires an object reference.

Where have I gone wrong
 
Last edited:
TransportArray(i) = New cCrew
TransportArray(i).Name = InputBox("Input Name for Item " & CStr(i))
TransportArray(i).Weight = CDbl(InputBox("Input Weight for Item " & CStr(i)))
TransportArray(i).TransportStarDate = CDbl(InputBox("Input Transport Star Date for Item " & CStr(i)))
TransportArray(i).crewlevel = CInt(InputBox("Input Crew level for Item " & CStr(i)))
you can rewrite that like this:
        Dim c As New cCrew
        With c
            .Name = InputBox("Input Name for Item " & CStr(i))
            .Weight = CDbl(InputBox("Input Weight for Item " & CStr(i)))
            .TransportStarDate = CDbl(InputBox("Input Transport Star Date for Item " & CStr(i)))
            .crewlevel = CInt(InputBox("Input Crew level for Item " & CStr(i)))
        End With
        TransportArray(i) = c

If you later need to access an element in the array as a more specific type you can cast it using CType/DirectCast, but the code here don't need to.

I realize this is beginner code, but think about what happens if user enters something other than a number, CDbl will throw an exception! Check out the TryParse methods instead, for example Double.TryParse.
TransportArray(i) = cCrew.IsHuman
I don't understand what that is supposed to mean.
 
Back
Top