469,342 Members | 5,581 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,342 developers. It's quick & easy.

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
Nov 21 '05 #1
4 1852
"Boni" <oilia@nospam> schrieb:
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.

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

Nov 21 '05 #2
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]" <hi***************@gmx.at> schrieb im Newsbeitrag
news:OD**************@TK2MSFTNGP12.phx.gbl...
"Boni" <oilia@nospam> schrieb:
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.

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

Nov 21 '05 #3

"Boni" <oilia@nospam> wrote in message
news:Oy**************@TK2MSFTNGP15.phx.gbl...
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

Nov 21 '05 #4

"Boni" <oilia@nospam> wrote in message
news:er**************@TK2MSFTNGP15.phx.gbl...
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.
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Christophe Barbe | last post: by
1 post views Thread by Dmitry D | last post: by
5 posts views Thread by Trevor Lango | last post: by
15 posts views Thread by Samee Zahur | last post: by
9 posts views Thread by Adam Badura | last post: by
6 posts views Thread by ralphmerridew | last post: by
3 posts views Thread by Filimon Roukoutakis | last post: by
5 posts views Thread by Amit Gupta | last post: by
6 posts views Thread by WaterWalk | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.