Objects and instance variables in loops Is there anything wrong with doing this:

Public Class Form1
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For value As Integer = 0 To 1000000
            Dim p As New Person
            p.ID = value
            p.DoSomething()
            p = Nothing
        Next
    End Sub
    Public Class Person
        Dim _ID As Integer
        Public WriteOnly Property ID() As Integer
            Set(ByVal value As Integer)
                _ID = value
            End Set
        End Property

        Public Sub DoSomething()
            _ID = _ID + 1
            'Do more with ID
        End Sub
    End Class
End Class


As appose to this:

Public Class Form1
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For value As Integer = 0 To 1000000
            Person.DoSomething(value)
        Next
    End Sub
    Public Class Person
        Public Shared Sub DoSomething(ByVal id As Integer)
            id = id + 1
            'Do more with ID
        End Sub
    End Class

End Class


I have written some code similar to that in sample 1. I have read online that code 1 can cause problems when threading is involved, but I do not understand why? Are there any other problems with sample 1? Sample 1 meets my requirements better.
In sample1, as you call it, you have an instance field which you change. Therefore, the Person class you are using is mutable. Mutable classes are prominent to concurrency issues if a single instance is manipulated by multiple threads, unless explicit synchronization mechanism is properly involved.

In sample2, if you pass the value in the method, and you are not modifying the state of the Person class as you do your business logic, then the code is thread safe. No other thread can access the local variable id and modify it concurrently, since it is visible to the current thread only. So there is no need to apply a synchronization mechanism.

However, sample2 may not be appropriate enough for your concrete requirements. If you give more code clues on what happens in the method in question, then a more-accurate advice can be given.