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

Accessing Friend Variables

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


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

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

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

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

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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.