By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,915 Members | 1,345 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,915 IT Pros & Developers. It's quick & easy.

friend class

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
"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

P: n/a
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

P: n/a

"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

P: n/a

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