friend class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Boni

    friend class

    Dear all,
    I would like to have an access from one class to the private variables of
    other. But I also can't find a right sintax. The word "friend" seems to do
    something else, VB compiler still complains.
    -----C++--------

    class A{
    private:
    int K;
    }
    class B{
    friend class A;

    void test(){
    A oA;
    A.K=10; //acccess private member
    }
    }
    ------VB----
    Please help


  • Herfried K. Wagner [MVP]

    #2
    Re: friend class

    "Boni" <oilia@nospam > schrieb:[color=blue]
    > I would like to have an access from one class to the private variables of
    > other. But I also can't find a right sintax. The word "friend" seems to do
    > something else, VB compiler still complains.
    > -----C++--------
    >
    > class A{
    > private:
    > int K;
    > }
    > class B{
    > friend class A;
    >
    > void test(){
    > A oA;
    > A.K=10; //acccess private member
    > }
    > }[/color]

    That's not supported.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Boni

      #3
      Re: friend class

      Is there a good solution?
      I would like to have most class members private for all classes except for
      one special, which has special handlers. What is a good design style to do
      so?
      "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schrieb im Newsbeitrag
      news:OD73A9kYFH A.1412@TK2MSFTN GP12.phx.gbl...[color=blue]
      > "Boni" <oilia@nospam > schrieb:[color=green]
      >> I would like to have an access from one class to the private variables of
      >> other. But I also can't find a right sintax. The word "friend" seems to
      >> do something else, VB compiler still complains.
      >> -----C++--------
      >>
      >> class A{
      >> private:
      >> int K;
      >> }
      >> class B{
      >> friend class A;
      >>
      >> void test(){
      >> A oA;
      >> A.K=10; //acccess private member
      >> }
      >> }[/color]
      >
      > That's not supported.
      >
      > --
      > M S Herfried K. Wagner
      > M V P <URL:http://dotnet.mvps.org/>
      > V B <URL:http://classicvb.org/petition/>[/color]


      Comment

      • Mythran

        #4
        Re: friend class


        "Boni" <oilia@nospam > wrote in message
        news:OyMFcQlYFH A.1344@TK2MSFTN GP15.phx.gbl...[color=blue]
        > Is there a good solution?
        > I would like to have most class members private for all classes except for
        > one special, which has special handlers. What is a good design style to do
        > so?[/color]

        A similar way?

        Friend Class MyClass1
        Friend Name As String
        Friend Address As String
        End Class

        Public Class MyClass2
        Private mMyClass As MyClass1

        Public Sub New()
        mMyClass = New MyClass1()
        End Sub

        Public Property Name() As String
        Get
        Return mMyClass.Name
        End Get
        Set
        mMyClass.Name = Value
        End Set
        End Property

        Public Sub SetAddress(ByVa l Address As String)
        mMyClass.Addres s = Address
        End Sub
        End Class


        The difference is, any class in the assembly can access MyClass1 and it's
        properties/methods. But, everything outside the assembly can't access
        MyClass1 directly.

        Mythran

        Comment

        • Phill.  W

          #5
          Re: friend class


          "Boni" <oilia@nospam > wrote in message
          news:erEIdDkYFH A.2128@TK2MSFTN GP15.phx.gbl...[color=blue]
          > I would like to have an access from one class to the private
          > variables of other.[/color]

          You can't. If you need to be access a variable outside of a given
          class, it has to be exposed, probably via a property.

          Looking at your code, I would tend towards adding a Friend
          method called something like "Prime", which class *in the same
          assembly* can use to pass in a value, as in

          Class A
          Private K as Integer = Integer.MinValu e

          Friend Sub Prime( ByVal Value As Integer )
          ' Prevent Prime being called more than once
          If K <> Integer.MinValu e Then
          Throw New ApplicationExce ption()
          End If

          K = Value
          End Sub

          End Class

          Class B
          Private oA As New A

          Sub New()
          oA.Prime( 10 )
          End Sub

          End Class

          HTH,
          Phill W.


          Comment

          Working...