473,385 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Accessing Friend Variables

Ok, this is probably a no-brainer for most of you but its escaping me and I
can't even seem to find the answer in any documentation. How do you access a
friend variable of one class, from another class in the same project?

Thanks
Blake
Nov 20 '05 #1
9 4789
* "Blake Weaver" <rb******@bellsouth.net> scripsit:
Ok, this is probably a no-brainer for most of you but its escaping me and I
can't even seem to find the answer in any documentation. How do you access a
friend variable of one class, from another class in the same project?


The same way as you do that with a public variable.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
"Blake Weaver" <rb******@bellsouth.net> schrieb
Ok, this is probably a no-brainer for most of you but its escaping me
and I can't even seem to find the answer in any documentation. How do
you access a friend variable of one class, from another class in the
same project?


There's no difference to Public variables in the same project.

class C1
friend i as integer
end class

'...

dim o as new c1
o.i = 17 'Access Friend field
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3
The same way you access any variable, by its name. But remember, that
Friend scope is namespace scope, so if the 2 classes are not in the same
namespace, Friend won't cut it for you.
"Blake Weaver" <rb******@bellsouth.net> wrote in message
news:zE*****************@bignews3.bellsouth.net...
Ok, this is probably a no-brainer for most of you but its escaping me and I can't even seem to find the answer in any documentation. How do you access a friend variable of one class, from another class in the same project?

Thanks
Blake

Nov 20 '05 #4
Ok, perhaps I've got the wrong idea in my head or perhaps I didn't explain
myself right. I've got a class with a variable in, a control on the form
actually. I've got another class (another form) that I call from the main
form/class. Now, once I display the new form I want to be able to get at
that particular control on the main form. So:

' In Main Form
Private Sub Button1_Click(ByVal sender As system.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim NewForm As New frmChildForm()
NewForm.Show()
End Sub

' In Child Form
....
' I want to access a control (just say a textbox) on the Main Form
' In VB6 you could do something like:
MsgBox frmMainForm.txtTextBox.Text
....

How do I do this in .NET? Or do I need to just change the entrie way I'm
looking at this?

Thanks again,
Blake

"Armin Zingler" <az*******@freenet.de> wrote in message
news:u8**************@TK2MSFTNGP11.phx.gbl...
"Blake Weaver" <rb******@bellsouth.net> schrieb
Ok, this is probably a no-brainer for most of you but its escaping me
and I can't even seem to find the answer in any documentation. How do
you access a friend variable of one class, from another class in the
same project?


There's no difference to Public variables in the same project.

class C1
friend i as integer
end class

'...

dim o as new c1
o.i = 17 'Access Friend field
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #5
You can just pass a reference to the main form in the
constructor of your second form:
Public Class Form2
Inherits System.Windows.Forms.Form
Private m_Form1 as Form1

Public Sub New(ByVal FirstForm as Form1)

m_Form1 = FirstForm

m_Form1.txtSomeTextBox.Text = "Some data"
End Sub
End Class

.....in Form1

Dim Form as Form2

Form = New Form2(Me)

-----Original Message-----
Ok, perhaps I've got the wrong idea in my head or perhaps I didn't explainmyself right. I've got a class with a variable in, a control on the formactually. I've got another class (another form) that I call from the mainform/class. Now, once I display the new form I want to be able to get atthat particular control on the main form. So:

' In Main Form
Private Sub Button1_Click(ByVal sender As system.Object, ByVal e AsSystem.EventArgs) Handles Button1.Click
Dim NewForm As New frmChildForm()
NewForm.Show()
End Sub

' In Child Form
....
' I want to access a control (just say a textbox) on the Main Form ' In VB6 you could do something like:
MsgBox frmMainForm.txtTextBox.Text
....

How do I do this in .NET? Or do I need to just change the entrie way I'mlooking at this?

Thanks again,
Blake

"Armin Zingler" <az*******@freenet.de> wrote in message
news:u8**************@TK2MSFTNGP11.phx.gbl...
"Blake Weaver" <rb******@bellsouth.net> schrieb
> Ok, this is probably a no-brainer for most of you but its escaping me > and I can't even seem to find the answer in any documentation. How do > you access a friend variable of one class, from another class in the > same project?


There's no difference to Public variables in the same project.
class C1
friend i as integer
end class

'...

dim o as new c1
o.i = 17 'Access Friend field
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

.

Nov 20 '05 #6
"Blake Weaver" <rb******@bellsouth.net> schrieb
How do I do this in .NET? Or do I need to just change the entrie way
I'm looking at this?

http://groups.google.com/gr*********...TNGP09.phx.gbl
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #7
Thanks Sean, thats exactly what I was trying to do. It was just escaping me,
like I said.
You more question though, would it be better to pass the Form ByVal or
ByRef. As I understand it, ByVal is sending a copy of the form and ByRef is
point me back to the form itself. Seems to me, ByVal would eat up quite a
bit of memory that isn't necessary. But there may be pros to it as well...
or other cons for that matter. Whats your opinion? Others are more than
welcome to respond of course.

Thanks again,
Blake

"Sean" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl...
You can just pass a reference to the main form in the
constructor of your second form:
Public Class Form2
Inherits System.Windows.Forms.Form
Private m_Form1 as Form1

Public Sub New(ByVal FirstForm as Form1)

m_Form1 = FirstForm

m_Form1.txtSomeTextBox.Text = "Some data"
End Sub
End Class

....in Form1

Dim Form as Form2

Form = New Form2(Me)

-----Original Message-----
Ok, perhaps I've got the wrong idea in my head or perhaps

I didn't explain
myself right. I've got a class with a variable in, a

control on the form
actually. I've got another class (another form) that I

call from the main
form/class. Now, once I display the new form I want to be

able to get at
that particular control on the main form. So:

' In Main Form
Private Sub Button1_Click(ByVal sender As system.Object,

ByVal e As
System.EventArgs) Handles Button1.Click
Dim NewForm As New frmChildForm()
NewForm.Show()
End Sub

' In Child Form
....
' I want to access a control (just say a textbox) on

the Main Form
' In VB6 you could do something like:
MsgBox frmMainForm.txtTextBox.Text
....

How do I do this in .NET? Or do I need to just change the

entrie way I'm
looking at this?

Thanks again,
Blake

"Armin Zingler" <az*******@freenet.de> wrote in message
news:u8**************@TK2MSFTNGP11.phx.gbl...
"Blake Weaver" <rb******@bellsouth.net> schrieb
> Ok, this is probably a no-brainer for most of you but its escaping me > and I can't even seem to find the answer in any documentation. How do > you access a friend variable of one class, from another class in the > same project?

There's no difference to Public variables in the same project.
class C1
friend i as integer
end class

'...

dim o as new c1
o.i = 17 'Access Friend field
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

.

Nov 20 '05 #8
"Blake Weaver" <rb******@bellsouth.net> schrieb
Thanks Sean, thats exactly what I was trying to do. It was just
escaping me, like I said.
You more question though, would it be better to pass the Form ByVal
or ByRef. As I understand it, ByVal is sending a copy of the form and
ByRef is point me back to the form itself. Seems to me, ByVal would
eat up quite a bit of memory that isn't necessary. But there may be
pros to it as well... or other cons for that matter. Whats your
opinion? Others are more than welcome to respond of course.


Passing the variable ByVal passes a copy of the content of the variable. As
a Form is a reference type, the variable only contains the reference (=4
Bytes) to the Form. Consequently a copy of the reference is passed, not a
copy of the Form.

Passing the variable ByRef passes a reference to the variable. Replace
"variable" by "reference to the Form" (that's what the variable contains),
and you get: Passing the variable ByRef passes a reference to the reference
to the Form.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #9
Actually, passing the form (which is a Reference-Type) ByVal results in the
passing of a copy of the pointer. Yes, that's right, a copy of the pointer,
not a copy of the form itself. Passing Reference-Types ByVal is a bit
confusing, but that's what you wind up with. You only get a copy when you
pass a Value-Type ByVal.
"Blake Weaver" <rb******@bellsouth.net> wrote in message
news:Sf*******************@bignews5.bellsouth.net. ..
Thanks Sean, thats exactly what I was trying to do. It was just escaping me, like I said.
You more question though, would it be better to pass the Form ByVal or
ByRef. As I understand it, ByVal is sending a copy of the form and ByRef is point me back to the form itself. Seems to me, ByVal would eat up quite a
bit of memory that isn't necessary. But there may be pros to it as well...
or other cons for that matter. Whats your opinion? Others are more than
welcome to respond of course.

Thanks again,
Blake

"Sean" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl...
You can just pass a reference to the main form in the
constructor of your second form:
Public Class Form2
Inherits System.Windows.Forms.Form
Private m_Form1 as Form1

Public Sub New(ByVal FirstForm as Form1)

m_Form1 = FirstForm

m_Form1.txtSomeTextBox.Text = "Some data"
End Sub
End Class

....in Form1

Dim Form as Form2

Form = New Form2(Me)

-----Original Message-----
Ok, perhaps I've got the wrong idea in my head or perhaps

I didn't explain
myself right. I've got a class with a variable in, a

control on the form
actually. I've got another class (another form) that I

call from the main
form/class. Now, once I display the new form I want to be

able to get at
that particular control on the main form. So:

' In Main Form
Private Sub Button1_Click(ByVal sender As system.Object,

ByVal e As
System.EventArgs) Handles Button1.Click
Dim NewForm As New frmChildForm()
NewForm.Show()
End Sub

' In Child Form
....
' I want to access a control (just say a textbox) on

the Main Form
' In VB6 you could do something like:
MsgBox frmMainForm.txtTextBox.Text
....

How do I do this in .NET? Or do I need to just change the

entrie way I'm
looking at this?

Thanks again,
Blake

"Armin Zingler" <az*******@freenet.de> wrote in message
news:u8**************@TK2MSFTNGP11.phx.gbl...
> "Blake Weaver" <rb******@bellsouth.net> schrieb
> > Ok, this is probably a no-brainer for most of you but

its escaping me
> > and I can't even seem to find the answer in any

documentation. How do
> > you access a friend variable of one class, from

another class in the
> > same project?
>
> There's no difference to Public variables in the same

project.
>
> class C1
> friend i as integer
> end class
>
> '...
>
> dim o as new c1
> o.i = 17 'Access Friend field
>
>
> --
> Armin
>
> http://www.plig.net/nnq/nquote.html
> http://www.netmeister.org/news/learn2quote.html
>
.


Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
4
by: keepyourstupidspam | last post by:
Hello, I have a class X with a member function setConfigValues(). I want to access this member function in another class Y. Y is not inherited from X. A member function of class X called run()...
9
by: Joel Moore | last post by:
I'm a little confused here. If I have the following: Public ClassA Friend varA As Integer Private varB As Integer Private ClassB Public Sub MethodA() ' How can I access varA and varB here?...
8
by: dwok | last post by:
I have been wondering this for a while now. Suppose I have a class that contains some private member variables. How should I access the variables throughout the class? Should I use properties that...
5
by: MrJim | last post by:
How should variables be declared and referenced in both the base and derived form so they can be accessed?
8
by: toton | last post by:
Hi, I have a parser interface , which is class IParser{ public: void readHeader(Document& doc)= 0; void readCC(CC& cc) = 0; }; Now, there are 3 kinds of parser which extends (implements)...
6
by: earthwormgaz | last post by:
Is the following legal? class Outer { class Inner { private: Inner() { } };
3
by: jthep | last post by:
I have been given the class definition below: class llist { private: record *start; char filename; int readfile(); int writefile(); record * reverse(record *);
1
by: Immortal_Nephi | last post by:
I want to show you an example how two classes can have relationship instead of an inheritance. The inheritance is not an option if you want to define two classes. The relationship between two...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.