Results 1 to 1 of 1

Thread: Pascal-like Set class for VB.net (OK for VB2010 and VB2015)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2013
    Posts
    21

    Pascal-like Set class for VB.net (OK for VB2010 and VB2015)

    Historically, Pascal has been a language that has supported Sets, not available in C and its successors nor in Basic. Sets can be very useful in counting and placing values (of bytes or alphanumeric items) into specific memory. These characteristics are useful in simulating card games, puzzle-solving (as in Sudoko), random number generation without repetition (as in Lotto cards).

    The code presented here is in two parts; the SetClass, which contains the methods for Sets and an Application that demonstrates the frequency of letters, numerals and punctuation of a text string.

    Sets have a given size capacity; in the SetClass items which have a numerical value in the range of 0 to 127 are allowed. This covers the ASCII family of all common used symbols.

    To use the code, open your VB Studio and open a new "Console Application". Give it a name so that you can easily find it later. Select and Copy the top section (the class) and paste immediately below "Module1". The copy and paste the app code immediately below "Sub Main". Save All to compete the installation.

    If you find this useful, you may wish to place the SetClass into a Library or into a module of its own.

    Code:
      ' Author: Gordon Coverley email gcoverley@hotmail.com
        Public Class setClass
            ' version June 30 2018
            'provides methods to use Sets in VB.net
            Private xset() As Byte
            Private Const UPPER = 127
    
            Public Sub New() 'CONSTRUCTOR
                ReDim Me.xset(UPPER)
                For n As Integer = 0 To UPPER 'clears the set. ex: dim setx as New setclass()
                    Me.xset(n) = False
                Next
            End Sub
            Public Shared Operator +(ByVal set1 As setClass, ByVal set2 As setClass) As setClass
                Dim b As Boolean 'union of set1 and set2
                Dim set3 As New setClass()
                For L = 0 To UPPER
                    b = set1.xset(L) Or set2.xset(L)
                    set3.xset(L) = b
                Next
                Return set3
            End Operator
            Public Shared Operator <>(ByVal set1 As setClass, ByVal set2 As setClass) As Boolean
                'returns TRUE if set1 is different from set2
                Dim result As Boolean = False
                For n As Integer = 0 To UPPER
                    If (set1.xset(n) Xor set2.xset(n)) Then
                        result = True
                    End If
                Next
                Return result
            End Operator
            Public Shared Operator =(ByVal set1 As setClass, ByVal set2 As setClass) As Boolean
                'returns TRUE if set1 is identical to set2
                Dim result As Boolean = True
                For n As Integer = 0 To UPPER
                    If set1.xset(n) <> set2.xset(n) Then
                        result = False
                    End If
                Next
                Return result
            End Operator
    
            Public Shared Operator *(ByVal set1 As setClass, ByVal set2 As setClass) As setClass
                Dim b As Boolean 'intersection of set1 and set2
                Dim set3 As New setClass()
                For L = 0 To UPPER
                    b = set1.xset(L) And set2.xset(L)
                    set3.xset(L) = b
                Next
                Return set3
            End Operator
            Public Shared Operator -(ByVal set1 As setClass, ByVal set2 As setClass) As setClass
                Dim b As Boolean 'set1 -set2 (items of set2 that are in set1 are removed)
                Dim set3 As New setClass()
                For L = 0 To UPPER
                    b = set1.xset(L) And Not (set2.xset(L))
                    set3.xset(L) = b
                Next
                Return set3
            End Operator
    
            Public Sub addItem(ByRef obj As Object) ' adds a char, integer, string to calling set. Does not clear set
                ' ex. myset.additem("abc")
                Dim j As Integer
                If TypeOf obj Is Integer Then
                    Me.xset(obj) = True
                    Exit Sub
                End If
                If TypeOf obj Is Char Then
                    Me.xset(Asc(obj)) = True
                    Exit Sub
                End If
                If TypeOf obj Is String Then
                    For j = 0 To obj.length - 1
                        Me.xset(Asc(obj(j))) = True
                    Next j
    
                    Exit Sub
                End If
            End Sub
            Public Sub removeItem(ByRef obj As Object)  ' removes a char, integer, string in calling set. 
                ' ex. myset.removeitem("abc")
                Dim j As Integer
                If TypeOf obj Is Integer Then
                    Me.xset(obj) = False
                    Exit Sub
                End If
                If TypeOf obj Is Char Then
                    Me.xset(Asc(obj)) = False
                    Exit Sub
                End If
                If TypeOf obj Is String Then
                    For j = 0 To obj.length - 1
                        Me.xset(Asc(obj(j))) = False
                    Next j
    
                    Exit Sub
                End If
    
            End Sub
            Public Function itemIsIn(ByRef obj As Object) As Boolean
                ' determines if obj is in calling set. When called,parameter MUST be dimensioned as obj.
                Dim j As Integer
                If TypeOf obj Is Integer Then
                    If Me.xset(obj) = False Then
                        Return False    ' obj not present
                    Else
                        Return True     ' obj is in calling set
                    End If
                End If
                If TypeOf obj Is Char Then
                    If Me.xset(Asc(obj)) = False Then
                        Return False    ' obj not present
                    Else
                        Return True     ' obj is in calling set
                    End If
                End If
                If TypeOf obj Is String Then
                    Dim q As Boolean = True
                    For j = 0 To obj.length - 1
                        If Me.xset(Asc(obj(j))) = False Then
                            q = False    ' obj not present
                            Exit For
                        End If
                    Next j
                    Return q
                End If
                Return False    'if this is reached, there is an error in item
            End Function
            Public Sub empty() 'clears (empty) the calling set
                Array.Clear(Me.xset, 0, UPPER)
            End Sub
            Public Function contains(ByVal set1 As setClass) As Boolean
                ' returns TRUE if set1 is in calling set, otherwise returns FALSE
                Dim result As Boolean = True
                For L = 0 To UPPER
                    If (set1.xset(L)) Then
                        If Not Me.xset(L) Then
                            result = False
                            Exit For
                        End If
                    End If
                Next
                Return result
            End Function
            Public Function getCnt() As Byte
                Dim count As Byte 'returns no. of items in set. ex: setx.set_cnt()
                count = 0
                For L = 0 To UPPER
                    If Me.xset(L) Then
                        count = count + 1
                    End If
                Next
                Return count
            End Function
            Public Function valInSet(ByVal value As Byte) As Boolean 'use ItemIsIn, if you like.
                If Me.xset(value) Then ' tests for value in set. ex: setx.val_in_set(asc("A")
                    Return True
                Else
                    Return False
                End If
            End Function
            Public Sub addRange(ByVal j As Byte, ByVal k As Byte)
                Dim n As Integer ' fills set with contigous values from j to k inclusive.ex: myset.add_range(48,57)
                For n = j To k ' does not clear the set first.
                    Me.xset(n) = True
                Next
            End Sub
            Function toAlpha() As String  'calling set items put into a string, use for display/printing
                Dim s, cs As String
                s = ""
                For k As Integer = 0 To 127  'to convert set to a string
                    If Me.xset(k) Then
                        cs = Chr(k)
                        s = s & cs
                    End If
                Next
                Return s
            End Function
        End Class
    'Application code follows:
     Console.WriteLine("Demo of Set class-Counting vowels,consonants,punctuation, numerals in a string")
            Dim letterset, consonantset, vowelset, punctuationset, numeralset As New setClass
            Dim cntvowels, cntconsonants, cntnumerals, cntpunctuation, other As Integer
            'fill these sets with letters,etc
            letterset.addRange(Asc("A"), Asc("Z"))      'all upper case letters. addRange needs byte parameters
            letterset.addRange(Asc("a"), Asc("z"))      'add lower case letters
            vowelset.addItem("aeiouAEIOU")               ' vowels
            punctuationset.addItem(".,!:;?")             'some punctuation
            numeralset.addItem("0123456789")             'numerals
            consonantset = letterset - vowelset         'subtract vowels from letterset
            'User test message
            Console.Write("Enter a message: ")
            Dim str As String = Console.ReadLine()
            For j As Integer = 0 To str.Length - 1
                Dim b As Object = str(j)
                If consonantset.itemIsIn(b) Then        'note b is an object
                    cntconsonants += 1
                ElseIf vowelset.itemIsIn(b) Then
                    cntvowels += 1
                ElseIf numeralset.itemIsIn(b) Then
                    cntnumerals += 1
                ElseIf punctuationset.itemIsIn(b) Then
                    cntpunctuation += 1
                Else
                    other += 1
                End If
            Next
            'output counts
            Console.WriteLine("Test string is:- " & str)
            Console.WriteLine("consonants= " & cntconsonants)
            Console.WriteLine("vowels= " & cntvowels)
            Console.WriteLine("numerals= " & cntnumerals)
            Console.WriteLine("punctuation= " & cntpunctuation)
            Console.WriteLine("others= " & other)
    
            Console.ReadLine()
    Last edited by gordc1; Jul 15th, 2018 at 07:58 AM. Reason: looking for word wrap seems OK

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width