Classes

Introduction to Classes

Classes are an unfinished part of MBSC 2.0. Unlike UDTs, classes use pointers all the time. They also keep track of their own lifetime, are dynamically allocated, and can be returned from functions. All of this together makes classes a much more powerful version of UDTs.

Features of Classes

And here's the section where I explain what the first paragraph means. Ugh.

Using Classes

Here's an example that uses classes:

Class ListNode
   Dim Data as String
   Dim Next as ListNode
End Class

Class LinkedList
   Dim Root as ListNode

   Sub Insert(Data as String)
      Dim CurNode as ListNode

      CurNode = Root
      While CurNode.Next <> NULL
         CurNode = CurNode.Next
      Wend
      Set CurNode.Next = New CurNode
      CurNode.Next.Data = Data
   End Sub

   Sub Delete(Index as Long)
      Dim CurNode as ListNode
      Dim CurIndex as Long

      CurNode = Root
      While CurNode.Next <> NULL & CurIndex < Index-1
         CurNode = CurNode.Next
         CurIndex = CurIndex + 1
      Wend
      If CurNode.Next <> NULL Then CurNode.Next = CurNode.Next.Next
   End Sub
End Class

Sub Main(Player as Long)
   Dim List as LinkedList

   Dim i as Long
   For i = 1 To 50
      If IsPlaying(i) Then List.Insert(GetPlayerName(i))
   Next i
End Sub

Remember, classes are unfinished, so none of this code actually compiles. It only describes how they should work.

There's so much involved here that there's no way I'm explaining all of it now. I haven't even explained pointers and references anywhere in the tutorial. >_<

Table of Contents
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.