0

I am trying to make this an OOP program. I wanted to use constructors so I could use the array in other classes through inheritance. I would make the actual array in the MakeArray class. The test class is an example of a class I would use the array in.

Class superClass
        Protected array1D() As String
        Protected array2D(,) As String

        Public Sub New(ByVal array() As String)
            array1D = array
        End Sub

        Public Sub New(ByVal array(,) As String)
            array2D = array
        End Sub

        Public Sub New()
            'intialise without a course
        End Sub
    End Class

    Class MakeArray
        Inherits superClass

        Function Import1DArray() As Array
            Dim fileReader As New StreamReader("1Darray.txt")
            Dim lineFromFile As String = fileReader.ReadLine()
            Dim counter As Integer = 0
            Do
                ReDim Preserve array1D(counter)
                array1D(counter) = lineFromFile
                lineFromFile = fileReader.ReadLine()
                counter = counter + 1
            Loop Until lineFromFile = ""

            Return array1D
        End Function
    End Class

    Class test
        Inherits superClass

        Sub test()
            For i = 0 To 2 Step 1
                Console.WriteLine(array1D(i))
            Next
        End Sub
    End Class

I am quite new to OOP, so I'm not 100% sure about how it works.

Thanks!

L. John
  • 39
  • 6
  • 1
    Class inheritance is not the way here, `Interface` implementation is more suitable here. for example, if you have`Animal` super class (base class in .net) that you can write a derived class `Tiger` or `Dog` or `Cat` etc... because thay are all animal and share properties/fields that all animals has. MakeArray is not SuperClass lige Tiger is Animal – Jonathan Applebaum Oct 17 '19 at 20:16
  • I learnt a bit about Interfaces, but never got a solid understanding. I learnt about abstract classes and I heard they are similar? How would it work? Thanks! – L. John Oct 17 '19 at 20:20
  • When I first started programming, using interfaces was very confusing to me to. here is a nice starting point to explore (its C# but the principles are the same in VB.NET): https://stackoverflow.com/questions/1686174/when-should-one-use-interfaces. their is vast of information for this topic. – Jonathan Applebaum Oct 17 '19 at 20:27
  • I could make the program using both inheritance and interface possibly? It seems a lot easier with the use of interfaces, but how could I use inheritance for it? I wanted to get an idea of all the basics of OOP. Thanks! – L. John Oct 17 '19 at 20:37
  • Technically you can do this with Inheritance, but it will harm the understanding of this topic. and yes VB.NET allows you to inherit class (only one) and implement multiple interfaces. – Jonathan Applebaum Oct 17 '19 at 20:41
  • Ah ok. The thing is, I just can't see how I could use inheritance here, but if its possible then is there a simple change to the program I have to make to make it work? Thanks! – L. John Oct 17 '19 at 20:42

0 Answers0