Results 1 to 9 of 9

Thread: [RESOLVED] Simple List Question *Solved*

  1. #1

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Resolved [RESOLVED] Simple List Question *Solved*

    Hello!

    This is the first time I am using a list in code. (Still a beginner...) But anyway, something's not right. I created a list of points, and added one to the list, but how can you tell if a list contains that point? I thought List.Contains() could work, but I guess not. Nothing happens when I load up the form. What am I doing wrong? How can I tell if a list contains a point or not?

    Thanks!

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim PointsList As List(Of Point)
    4.  
    5.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    6.  
    7.         Dim TestPoint1 As Point = New Point(0, 0)
    8.         Dim TestPoint2 As Point = New Point(0, 0)
    9.  
    10.         PointsList.Add(TestPoint1)
    11.         If PointsList.Contains(TestPoint2) Then
    12.             Label1.Text = "It worked!"
    13.         End If
    14.  
    15.     End Sub
    16.  
    17. End Class
    Last edited by NinjaNic; Oct 5th, 2014 at 12:10 PM.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Simple List Question

    This is because the load event is throwing an error (NullRef) that you aren't seeing. If you search for errors in load event you will find out why you don't see it. To find YOUR error create a button and move the code from the load event to the button click event.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: Simple List Question

    Hm, I still don't understand, it wants me to use the "new" keyword, but I already have it there.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Simple List Question

    At first, I didn't see the exception, but now I do.

    The Load event is a bit unlike anything else due to an internal disagreement between the OS and VS groups within MS. As a result, if you are running on 64-bit Windows, and an exception is thrown in the Load event without also being caught in the Load event, then the event handler will appear to simply exit. No exception will be shown, so you won't even know it's happening unless you step through the code and see execution suddenly go elsewhere.

    You can put the code into a Try...Catch handler, but I think DB is correct about the solution. This looks like test code, so putting it in a button click will make it easier to work with anyways.

    However, there may be a deeper issue. I don't think there is, in this case, since you are using Points, but there could be in general. The .Contains method doesn't always do what people expect it to do. It all depends on whether or not the type in the list has the proper interfaces defined. If you use a List (of SomeClass), and SomeClass doesn't have equality defined for it, then the .Contains will be comparing whether the list contains the specific instance of the class. You appear to be looking for a point that has the same X and Y as the one you are searching for, and I think that will work for the Point type, but for some types, you'd be asking whether the actual class was in the list not whether there was a class in the list with comparable values. I've always thought that the .Contains method could trip people up because of that.
    My usual boring signature: Nothing

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Simple List Question

    Quote Originally Posted by NinjaNic View Post
    Hm, I still don't understand, it wants me to use the "new" keyword, but I already have it there.
    No, you don't. You have it in some places, but not in one critical place: You are declaring the List, but you never create it, so you are getting a NullReference Exception on this line:

    PointsList.Add(TestPoint1)
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: Simple List Question

    Oh! You're right, thanks you very much. It seems to work when I type this:

    Dim PointsList As List(Of Point) = New List(Of Point)

    I was thinking it needed the "new" for the point.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Simple List Question *Solved*

    Or else, you can just type:

    Dim PointsList As New List(of Point)

    Either way works fine. The List is an object, so until you create an instance explicitly with New, it just isn't there.

    By the way, on the Tools menu for the forum is the option to set a thread to resolved, if you don't want to edit the title.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: Simple List Question *Solved*

    It seems like I can still edit variables ant things without using new.
    And thanks for the forum tip. I didn't know that!

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: [RESOLVED] Simple List Question *Solved*

    Some you can, some you can't. It depends on whether or not they are value types or reference types. Value types are your basic types like integer, double, etc., along with Structures. Point is a Structure, so you don't HAVE to use New with that, though you can. The truly odd one is string, which is kind of a hybrid. It's a reference type that acts like a value type.

    For all refernce types, which includes ALL classes, including forms, then you have to create a new instance to use the object. Shared members do not require an instance, so you can get to them without creating an instance, but shared members are also kind of rare.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width