Click here to Skip to main content
15,905,875 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
VB
Module Module1
    Sub Main()
        Dim oMyTest As New MyTest

        oMyTest.CreateMyName("Call From OutSide, Why Can't I assign directly to MyName")
       'oMyTest.MyName = ("Why Can’t I DO THIS")
    End Sub

End Module

Public Class MyTest

    Private MyName As String

    Public Sub CreateMyName(ByVal sString As String)
        MyName = sString
        Console.WriteLine(MyName)

        Dim oMyTest As New MyTest

       ' oMyTest.MyName = "Why does this work"
' why does this work 
' if I initiate a the same class object inside the same class i can access all private fields, function, and methods.
        Console.WriteLine(MyName)
        Console.WriteLine("Inside Obect: " & oMyTest.MyName)
    End Sub
End Class
Posted
Comments
Sandeep Mewara 31-May-11 10:41am    
What do you mean "Why can I access any private within same class"? -> They are scoped like that.

This here is an excellent source that explains the differences of all available access modifiers in VB terminology: http://msdn.microsoft.com/en-us/library/76453kax(v=vs.80).aspx[^].

That will explain all you asked and then some.

Cheers!
 
Share this answer
 
Comments
Marc A. Brown 31-May-11 11:03am    
Good link.
Espen Harlinn 31-May-11 11:13am    
My five
Sergey Alexandrovich Kryukov 31-May-11 16:52pm    
Good answer, my 5.
I would even say http://msdn.microsoft.com/en-us/library/76453kax(v=VS.100).aspx.
--SA
It works because Private members of a class are visible to all other members of the same class.

Really, you need to read up on this[^]. This is a very basic concept you're missing and cannot afford to misunderstand.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 31-May-11 12:24pm    
Yep! Another good link. 5+
Sergey Alexandrovich Kryukov 31-May-11 16:53pm    
Good, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900