bowlingbrad
bowlingbrad

Reputation: 35

Best way to associate two strings of text in VB.NET

I've got two strings that I want to associate together so that I can search by username and get the computername. Strings: username computername

I'm not that fluent in VB code so I don't know how to build this object. This is what I think I need:

(
  ("user1" "computer1")
  ("user2" "computer2")
  ("user3" "computer3")
  ("user4" "computer4")
  ("user5" "computer5")
)

Then I want to be able to query the list by asking: What computername is associated with "user3"? And get "computer3" as the result.

Upvotes: 3

Views: 209

Answers (5)

Andrew Morton
Andrew Morton

Reputation: 25066

I suppose there is the possibility that one computer is used by more than one user, and more than one user could use a particular computer. In that case, it is probably going to be simpler to have a class which holds the information relating a user to a computer. You can then make a List of instances of that class to hold the entire set of information, and being a List, you can easily query it with LINQ, like this:

Option Infer On
Module Module1

    Dim computerUsers As New List(Of ComputerUser)

    Class ComputerUser
        Property ComputerName As String
        Property UserName As String
    End Class

    Sub CreateTestData()
        computerUsers = New List(Of ComputerUser)
        computerUsers.Add(New ComputerUser With {.ComputerName = "PC1", .UserName = "Fred"})
        computerUsers.Add(New ComputerUser With {.ComputerName = "PC2", .UserName = "Jim"})
        computerUsers.Add(New ComputerUser With {.ComputerName = "PC3", .UserName = "Jane"})
        computerUsers.Add(New ComputerUser With {.ComputerName = "PC1", .UserName = "Jim"})

    End Sub

    Sub Main()
        CreateTestData()

        Dim pcName = "PC1"
        Dim thisPcUsers = computerUsers.Where(Function(x) x.ComputerName = pcName).Select(Function(y) y.UserName).ToList()
        Console.WriteLine(pcName & " is used by: " & String.Join(", ", thisPcUsers))

        Dim user = "Jim"
        Dim thisUserPCs = computerUsers.Where(Function(x) x.UserName = user).Select(Function(y) y.ComputerName).ToList()
        Console.WriteLine(user & " uses computers: " & String.Join(", ", thisUserPCs))

        Console.ReadLine()

    End Sub

End Module

Which outputs

PC1 is used by: Fred, Jim
Jim uses computers: PC2, PC1

Edit +1 to Seth, whose answer I did not see while I was writing this.

Edit 2 You can use LINQ in various ways, for example to get the users whose names start with "J*":

Dim partialName = "J*"
Dim usersWithWildcard = computerUsers.Where(Function(x) x.UserName Like partialName).ToList()

Console.WriteLine("Users starting with " & partialName)
For Each u In usersWithWildcard
    Console.WriteLine(u.UserName)
Next

Upvotes: 1

Seth
Seth

Reputation: 199

While the answers using dictionary will surely work, I personally prefer to use a class and and list.

You'd do something like this:

Public Class UserComputer
    Private _user As String
    Public Property User() As String
        Get
            Return _user
        End Get
        Set(ByVal value As String)
            _user = value
        End Set
    End Property

    Private _comp As String
    Public Property Computer() As String
        Get
            Return _comp
        End Get
        Set(ByVal value As String)
            _comp = value
        End Set
    End Property

    Public Sub New(User As String, Computer As String)
      Me.User = User
      Me.Computer = Computer
    End Sub

End Class

Public Sub Main()
  Dim myList As New List(Of UserComputer)
  myList.Add(New UserComputer("user1", "computer1")
  '... keep adding objects
End Sub

The advantage here is that it becomes very easy to extend your class UserComputer should you need to; just add more properties to the class. It also let's you easily consume the information in the objects in other parts of your program.

Upvotes: 1

lc.
lc.

Reputation: 116538

You're looking for a System.Collections.Generic.Dictionary. Specifically a Dictionary(Of String, String).

Add items one at a time:

Dim computers As New Dictionary(Of String, String)
computers.Add("user1", "computer1")
computers.Add("user2", "computer2")
'...

Or, you can use an initializer and do it in one fell swoop:

Dim computers As New Dictionary(Of String, String) From
    {
      {"user1" "computer1"},
      {"user2" "computer2"},
      {"user3" "computer3"},
      {"user4" "computer4"},
      {"user5" "computer5"}
    }

And access individual elements using an indexer:

Dim result As String = computers("user3")

Note this will throw an exception if "user3" does not exist in the collection. You may wish to look into TryGetValue if there is a possibility to search for nonexistent keys.

Upvotes: 4

Joel Coehoorn
Joel Coehoorn

Reputation: 416179

Dim userComputers As New Dictionary(Of String, String) From
{   
   {"user1" "computer1"},
   {"user2" "computer2"},
   {"user3" "computer3"},
   {"user4" "computer4"},
   {"user5" "computer5"}
}

Dim computer As String = userComputers("user3")

Upvotes: 0

Dan Puzey
Dan Puzey

Reputation: 34218

I suspect that want you want is a Dictionary(Of String, String):

Dim x as New Dictionary(Of String, String)
x.Add("user1", "computer1") ' etc...

Dim computer as String = x("user1")

Upvotes: 0

Related Questions