share a form level var with app - but don't make var global?

R

Rich

I am trying to get straight on using Friend and Shared. I pass a form object
(a Form Class object - Form1 for example) to a app level procedure and I want
to expose some form level variables to the procedure.

'call procedure from Form1
Private Sub btn1_Click...

someProce(me)

End Sub

Public someProc(frm As Form1)
'--if I declare a var in form1 as public I can do this which I do not want
console.writeline(var.tostring)

'--I want to require the form1 var to be prefaced with frm
console.writeline(frm.var.tostring)

If I declare the var in form1 as Friend var as String

then I need to preface var with frm in the procedure. Is this the correct
way to do this?

While I am at it - when is it time to use Shared? Is Shared like Friend?
Is Shared only for procedure/functions/class objects?

thanks
Rich
 
A

Armin Zingler

Rich said:
I am trying to get straight on using Friend and Shared. I pass a
form object (a Form Class object - Form1 for example) to a app level
procedure and I want to expose some form level variables to the
procedure.

'call procedure from Form1
Private Sub btn1_Click...

someProce(me)

End Sub

Public someProc(frm As Form1)
'--if I declare a var in form1 as public I can do this which I do not
want
console.writeline(var.tostring)

Must be another var you are referring to here (above). It's not var inside
frm.
'--I want to require the form1 var to be prefaced with frm
console.writeline(frm.var.tostring)

If I declare the var in form1 as Friend var as String

then I need to preface var with frm in the procedure. Is this the
correct way to do this?

While I am at it - when is it time to use Shared? Is Shared like
Friend?

No.
[F1] is your "Friend". ;-)
Is Shared only for procedure/functions/class objects?

Shared is used with class members (fields, properties, events, methods).

Instances of objects actually only contain fields (including the delegates
for events). One set of fields per instance. Therefore, instance methods
(including properties) can access fields of the related object. There can be
0 to whatever number of instances.

Shared fields exist exactly once per containing class. Shared methods can
only access shared fields because the methods have no relation to an
instance.

Instance members are referred to by the reference to the object
(Myobjectvar.member).
Shared members are referred to by naming the class (Classname.member).

That's the whole story.

Public/Private/Friend/Protected determine from where the member can be
accessed.


Armin
 
R

Rich

Shared fields exist exactly once per containing class. Shared methods can
only access shared fields because the methods have no relation to an
instance.

Instance members are referred to by the reference to the object
(Myobjectvar.member).
Shared members are referred to by naming the class (Classname.member).

That's the whole story.

Public/Private/Friend/Protected determine from where the member can be
accessed.
<<

this is the explanation I was looking for. Thank you. So if I have it
straight:

Public Class myClass
public Shared function myfunc() As String
....
End Function

Friend myFunc2() as string
....
End Function
End Class

console.writeline(myClass.myfunc.toString)

dim obj As New myClass
console.writeline(obj.myFunc2.toString)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top