Accessing Friend Variables

  • Thread starter Thread starter Blake Weaver
  • Start date Start date
B

Blake Weaver

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
 
* "Blake Weaver said:
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.
 
Blake Weaver said:
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
 
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.
 
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
 
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)
 
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
 
Blake Weaver said:
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.
 
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.
 
Back
Top