[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:
Public Class Form1
Dim PointsList As List(Of Point)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim TestPoint1 As Point = New Point(0, 0)
Dim TestPoint2 As Point = New Point(0, 0)
PointsList.Add(TestPoint1)
If PointsList.Contains(TestPoint2) Then
Label1.Text = "It worked!"
End If
End Sub
End Class
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.
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!
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.