friend class

  • Thread starter Thread starter Boni
  • Start date Start date
B

Boni

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
 
Boni said:
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
}
}

That's not supported.
 
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?
 
Boni said:
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?

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(ByVal Address As String)
mMyClass.Address = 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
 
Boni said:
I would like to have an access from one class to the private
variables of other.

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.MinValue

Friend Sub Prime( ByVal Value As Integer )
' Prevent Prime being called more than once
If K <> Integer.MinValue Then
Throw New ApplicationException()
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top