tags:

views:

24

answers:

2

I have built a custom Data Class that stores IP Address Details.

        Public Class IPAddressDataItem

        Private _ID As Integer
        Private _IP As String
        Private _Name As String

        Public Property ID() As Integer
            Get
                Return _ID
            End Get
            Set(ByVal value As Integer)
                _ID = value
            End Set
        End Property
        Public Property IP() As String
            Get
                Return _IP
            End Get
            Set(ByVal value As String)
                _IP = value
            End Set
        End Property
        Public Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property\

        Public Sub New(ByVal id As Integer, ByVal ip As String, ByVal name As String)
            _ID = id
            _IP = ip
            _Name = name
        End Sub

    End Class

What I'm trying to figure out how to do is search it for specific data.

Example.. I send it an IP address and it will return the name to me.

Does anyone know how I would do this?

+2  A: 

First of all, you need to put the object in a collection. To do this, you need to choose a data structure (i.e. List, ArrayList, etc)

Dim Items as List(Of IPAddressDataItem)

Then, you can iterate through the collection, find the item based on the search criteria and return the data required.

Function GetName(ByVal IP As String) As String
    For Each Item As IPAddressDataItem In Items
        If Item.IP.CompareTo(IP) = 0 Then
             Return Item.Name
        End If
    Next
End Function

Right now, if you have an instance of the object, you can just access the property directly.

ShaunLMason
Yup, I'm putting the object into a collection. I will try your iteration.. .thanks.
rockinthesixstring
A: 

I have tried this, but it's not seemingly working.

    Dim isfound As Boolean = False
    Dim items As List(Of SimpleSysLog.DataItems.IPAddressDataItem)

    'loop through the IPAddressDataItem to try and fine the ID of the IP Address
    For Each Item As SimpleSysLog.DataItems.IPAddressDataItem In items
        If Item.IP.CompareTo(IP) = 0 Then
            Return Item.ID
            isfound = True
            Exit For
        End If
    Next
rockinthesixstring
1) You need to declare a New list.2) Assuming you actually filled the List, your not going to hit isfound because it is AFTER the return statement.
ShaunLMason
Yup you're right. I actually had a problem with the Dim items as List... but after I stored the list into an instance of the object, I was just fine.Thanks for the help.
rockinthesixstring
No problem, those things get us all sometimes.
ShaunLMason