VB.net get list of items from class

Murray Brown
Murray Brown used Ask the Experts™
on
Hi

I was given the following code to load a list of names to a class object. How do I get these items so that I can loop through each one of these


Module Module1

    Sub Main()
        Dim objCls As New Class1

        objCls.JointTables = "Tab1"
        objCls.JointTables = "Tab2"
        objCls.JointTables = "Tab3"
        objCls.JointTables = "Tab2"


    End Sub

End Module

Public Class Class1


    Private _pJoinTables As String

    Public Sub New()
        _pJoinTables = ""
    End Sub

    Property JointTables() As String
        Get
            Return _pJoinTables
        End Get
        Set(ByVal sValue As String)
            If (Not _pJoinTables.Contains(sValue)) Then
                _pJoinTables = _pJoinTables & "," & sValue 'Error on this line first &
            End If
        End Set
    End Property

End Class
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
How about this instead?
Module Module1

    Sub Main()
        Dim objCls As New Class1
        objCls.AddTable("Tab1")
        objCls.AddTable("Tab2")
        objCls.AddTable("Tab3")
        objCls.AddTable("Tab2")
        For Each table As String In objCls.JointTables
            Console.WriteLine(table)
        Next
        Console.ReadKey()
    End Sub

End Module

Public Class Class1

    Private _pJoinTables As New List(Of String)

    Public Sub AddTable(ByVal table As String)
        If Not _pJoinTables.Contains(table) Then
            _pJoinTables.Add(table)
        End If
    End Sub

    ReadOnly Property JointTables() As String()
        Get
            Return _pJoinTables.ToArray
        End Get
    End Property

End Class

Open in new window

try this:-
Module Module1

    Sub Main()
        Dim objCls As New Class1

        objCls.JointTables = "Tab1"
        objCls.JointTables = "Tab2"
        objCls.JointTables = "Tab3"
        objCls.JointTables = "Tab2"

        For Each o As String In objCls.JointTables.Split(",")
            Console.WriteLine(o)
        Next

        Console.Read()
    End Sub

End Module

Public Class Class1


    Private _pJoinTables As String

    Public Sub New()
        _pJoinTables = ""
    End Sub

    Property JointTables() As String
        Get
            Return _pJoinTables
        End Get
        Set(ByVal sValue As String)
            If (Not _pJoinTables.Contains(sValue)) Then
                If (_pJoinTables.Length = 0) Then
                    _pJoinTables = sValue
                Else
                    _pJoinTables = _pJoinTables & "," & sValue 'Error on this line first &
                End If
            End If
        End Set
    End Property

End Class

Open in new window

Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial