How to access var outside of class?

B

Brett

Say I have an object that is placed onto a form. It will get this
declaration:

Public Class1
Friend WithEvents ObjectName1 As SomeCompany.ObjectName

Private Sub DoSomething
Object1.method1 = True
End Sub



Public Class2
Private MainClass As Class1

Private Sub DoSomethingElse
If Object1.method1 = True Then
Object1.property1 = "hello world"
End If
End Sub


If I want to use the ObjectName1 in another class, I can't. Are Friend
declarations supposed to be seen by all classes within the same application?
If I reference ObjectName1 in Class2, it doesn't know what I'm referring
too. Using MainClass.Object1 isn't correct either.

I really need just a conceptual explanation of what I'm doing wrong and how
I should do it.

Thanks,
Brett
 
C

Cor Ligthert

Brett,

When you make your code complete with all things correct as is needed, than
you will see that mainclass.object1.Method1 will work.

Now it is a bunch of pseudo with a lot of errors.

Cor
 
B

Brett

I think I have it now:

Given this:
Public Class1
Friend WithEvents ObjectName1 As SomeCompanyName.ObjectName

I reference it in Class2 as:
Public Class Class2
Private MainForm As Class1
Me.Class2.Object1.url = "www.abc.com"

The above does compile with no errors.

Brett
 
C

Cor Ligthert

Brett,

A friend declaration can be seen in any other object, however in its
instanced form.

Your mainform is a class and a object in once. You use it in your program as
an object for your mainform. You can use it as a class for other objects,
however than you are not accessing your mainform. You just create a new
object wherin is probably no value.

When you want to access a value in an object, than you have at least in a
way to pass the reference to that value in the object to the using object.
As sample

When you do in form1
dim frm2 as new form2
Than you can use in form1
frm2.value = "MyUrl"
because frm2 belongs to form1 (actualy object me) and therefore as well
me.value = frm2.value

However you can not use in frm2
frm2.value = ?.value
because ? is not an object in form2

There are ways to pass this, however not in a way as you now are trying, and
it is never good OOP when a class becomes completly dependable (not
reusable) from another class.

I hope this gives a better answer and a little bit the answer to your
problem for your question although it is probably confusing,

Cor
 
B

Brett

Actually, that didn't quite work either. I get this on the last line of
code below:

Additional information: Object reference not set to an instance of an
object.
 
P

Phill. W

Brett said:
Say I have an object that is placed onto a form. .. . .
Public Class1
Friend WithEvents ObjectName1 As SomeCompany.ObjectName .. . .
If I want to use the ObjectName1 in another class, I can't.
Are Friend declarations supposed to be seen by all classes within
the same application?

Friend things are accessible by any object in the same Namespace.
But, even then, /only/ if you have a reference to an object that contains
the thing you want to use.

Public Class2

Private Sub DoSomethingElse( byval oaThing as Class1 )
If oaThing.method1 = True Then
oaThing.property1 = "hello world"
End If
End Sub

How you /get/ that reference, of course, is another story.

HTH,
Phill W.
 

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