How to access an object's private variables (VB .NET)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sick0Fant
    New Member
    • Feb 2008
    • 121

    #1

    How to access an object's private variables (VB .NET)

    I have a class (call it "A") that has an object (call it "B") whose code I wrote. I want A to be able to access B's private variables.

    In C++ (and probably C#), I would just type "friend class A;" in the definition of B. However, this doesn't seem to work, and from what I can tell from MSDN, the Friend keyword in VB doesn't work that way.

    There has to be a way to do this... anyone know how?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I think if you label the fields as "protected" and not as "private" then the "owning" object can manipulate the fields but nobody else can?

    Comment

    • Sick0Fant
      New Member
      • Feb 2008
      • 121

      #3
      Originally posted by Plater
      I think if you label the fields as "protected" and not as "private" then the "owning" object can manipulate the fields but nobody else can?
      Declaring the field Protected didn't seem to make any difference. I tried "Protected Friend" which gave the class access to the object's field regardless of whether or not it owned the instance, which isn't quite what I want.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Might be a namsspace allowance or something then. I don't remember exactly what protected did.
        Hit f1 with protected highlighted, maybe there's something else there that could help you?

        Comment

        • Sick0Fant
          New Member
          • Feb 2008
          • 121

          #5
          Protected lines up pretty well with what it means in C, in that protected fields are accessible by derived objects. Friend means that the field is accessible to all classes in the assembly.

          I'd like to restrict access to all classes except for a certain one.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Could you put the two classes in their own assembly and seal it up? (I think you can do that right? You can seal classes so they cannot be derrived from, I think you can do that to a namespace?)

            Comment

            Working...