Do not use on any
shared computer
August 29, 2008 02:58pm pdt
 
[x]
Attachment Details

VB.NET ArrayList.contains(...) Do this not use the "Equals(ByVal obj as Object)"  Method????

Zone: .NET
Tags: arraylist
Ok, I am extremely confused here :)  I come from a strong Java background (by strong, I mean I have a good understanding on how java works as a OOP language), and recently I have been learning how to use the .NET framework.  I am currently having problems with ArrayList Class, specifically the "Public Function Contains(ByVal value As Object) As Boolean".  

Here are parts of my current code.

in my Name Class:

 ' I am assuming this overrides the System.Object.Equals method.... if
'  it doesnt, than this is probably my problem :)

 '  Below is the code for the Name Class.  Which is in the same code 'file' as another class.. I dont
 '  think this makes a difference, but am mentioning it encase.

Public Class MyOtherClass

.... ' Other class data and methods...etc

End Class

Public Class Name
    Private First As String
    Private Last As String

    Public Sub New(ByVal First As String, ByVal Last As String)
        Firstname = First
        Lastname = Last
    End Sub

    Public Overloads Function ToString() As String
        Return Me.Last & ", " & Me.Last
    End Function

    Public Overloads Function Equals(ByVal obj As Object) As Boolean

        Dim name As Name

'  Ignoring types until I can get this part working....
'  In my test, I have ONLY sent Name objects to this method.

        name = CType(obj, Name)
        If Me.First.ToLower.Equals(name.First.ToLower) And _
           Me.Last.ToLower.Equals(name.Last.ToLower) Then
            Return True
            '  End If
        End If
        Return False
    End Function


    Public Property Firstname() As String
        Get
            Firstname = First
        End Get
        Set(ByVal Value As String)
            If Value.Equals("") Then
                Throw New MySql.MySqlException("Please Enter a valid firstname!")
            End If
            First = Value
        End Set
    End Property

    Public Property Lastname() As String
        Get
            Lastname = Last
        End Get
        Set(ByVal Value As String)
            If Value.Equals("") Then
                Throw New MySql.MySqlException("Please Enter a valid lastname!")
            End If
            Last = Value
        End Set
    End Property

End Class


'  Now in another file I have a form that has an variable:

Public myNames as Arraylist

....and here is part of that form class

'  I click on a button and it calls this function,  however, it NEVER returns true for the expression
    Private Sub addName(ByVal obj As Name)
        ' Adds the name to the mynames arraylist

        If myNames.Contains(obj) Then
            Throw New MySql.MySqlException("Name already exists in the list!")
        End If
        myNames.Add(obj)   ' Adds object to the list
        Call refreshList()       ' refreshins certain feilds.
    End Sub



I can add the same name over and over again, and whenever I use the .contains function, it never returns true....  I am either missing something very basic with how .net uses the equals method or I am missing something very simple in my own code, either way I am fed up.....so here I am.  I've also had a few drinks since I have started tonight, that could be the reason too :)
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Programming
Question Asked By: FaithNoMore
Solution Provided By: _TAD_
Participating Experts: 1
Solution Grade: B
Views: 120
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by _TAD_

Rank: Guru

Expert Comment by _TAD_:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by FaithNoMore
Author Comment by FaithNoMore:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by _TAD_

Rank: Guru

Expert Comment by _TAD_:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by _TAD_

Rank: Guru

Expert Comment by _TAD_:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by _TAD_

Rank: Guru

Expert Comment by _TAD_:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by FaithNoMore
Author Comment by FaithNoMore:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by _TAD_

Rank: Guru

Accepted Solution by _TAD_:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by FaithNoMore
Author Comment by FaithNoMore:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34