We help IT Professionals succeed at work.

VB.net class Saving a list of Table names

Murray Brown
Murray Brown used Ask the Experts™
on
Hi. I am trying to save a list of Table names in a class (created below). What code do I use to
first check if a table name exists in JoinTables and then if it doesn't add it

Public Class Class1

    Private pJoinTables As String

    Property JointTables() As String
        Get
            Return pJoinTables
        End Get
        Set(ByVal Value As String)
            pJoinTables = Value
        End Set
    End Property
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Here is the class:-

Public Class MyTables
	Private ReadOnly _myTables As IList(Of String)

	Public Sub New()
		_myTables = New List(Of String)()
	End Sub

	Public Sub AddTable(tableName As String)
		If Not _myTables.Contains(tableName) Then
			_myTables.Add(tableName)
		End If
	End Sub

	Public ReadOnly Property Tables() As IList(Of String)
		Get
			Return _myTables
		End Get
	End Property
End Class

Open in new window

Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Hi
I am using the following code in a standard module
I tried, but oClass1.Add does not work

           oClass1 = New Class1

            oClass1.JointTables = "hello"


            If Not oClass1.JointTables.Contains("hello") Then

            End If
did you try the code given by you, this is how you need to use it:-

Dim tables As New MyTables()

tables.AddTable("Table1")
tables.AddTable("Table2")
tables.AddTable("Table3")
tables.AddTable("Table2")


tables.Tables.ToList().ForEach(Console.WriteLine)

//If you see on console or during debugging, the tables type will only have table1, table2 and table3.
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Sorry but I am not quite following. I am using the class to save a list of items
OK, then do this:-
Public Class Class1

    Private pJoinTables As String

    Property JointTables() As String
        Get
            Return pJoinTables
        End Get
        Set(ByVal Value As String)
            If(Not pJoinTables.Contains(value))
            pJoinTables = pJoinTables & ',' & Value
            End If
        End Set
    End Property
End Class

Open in new window

Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
thanks very much
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented: