I’ve never taken the initiative to learn the correct class modifier since it has always been a “nice to have” but not a “need to have”.
It annoys me that I can do Dim F as New Person.FavoriteFoodsList.
Which class modifier do I use so that my Person class can utilize the FavoriteFoodsList but nothing outside of Person can instantiate the FavoriteFoodsList?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim P As New Person
P.FavoriteFoods.Add("Pizza")
Dim F As New Person.FavoriteFoodsList 'How do I prevent this
End Sub
Public Class Person
Public FavoriteFoods As New FavoriteFoodsList
Public Class FavoriteFoodsList
Inherits Collections.Generic.List(Of String)
End Class
End Class
I recommend you define a public interface for your class then mark the implementation as private.