473,320 Members | 1,823 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,320 software developers and data experts.

How to access var outside of class?

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
Nov 21 '05 #1
5 1033
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
Nov 21 '05 #2
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:ea**************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #3
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
Nov 21 '05 #4
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.
"Brett" <no@spam.com> wrote in message
news:Ob**************@tk2msftngp13.phx.gbl...
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:ea**************@TK2MSFTNGP14.phx.gbl...
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


Nov 21 '05 #5
"Brett" <no@spam.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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.
Nov 21 '05 #6

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

Similar topics

5
by: TheFerryman | last post by:
How bad is it to include a non const accessor to a container of objects? For instance template <class WheelType> class CarBase { typedef std::list<WheelType*> WheelList; private:
12
by: James Brown | last post by:
Hi all, Having problems designing a template-class. I'll describe my scenario first then show what I've come up with so far: Need a class to provide pointer/array-like access to an area of...
13
by: jt | last post by:
Being a newbie in C++ and comming from C, I can't find information on how to access a class member function outside its class. Below is a snippet of the class and its member function: look at...
2
by: paul meaney | last post by:
All, myself and another developer have been staring blankly at a screen for the past 48 hours and are wondering just what stunningly obvious thing we are missing. We are trying to load up 2...
2
by: Richard Dixson | last post by:
I created a web form and dropped a table in there called resultsTable. This also resulted in the following being added to the Webform1 class: public System.Web.UI.WebControls.Table resultsTable; ...
9
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order */) { /* Check if the product order can be added...
9
by: JT | last post by:
Here is the overall structure I will be referring to: End-program ProvideWorkFlow.dll Forms and methods that properly manipulate calls to methods in AccessUtils AccessUtils (a web service)...
5
by: selimsivad83 | last post by:
Hi everyone, Thinking in Java, 4th edition, p 232: ... if you don't put an access specifier for class access, it defaults to package access. ... However, if a static member of that class is...
8
by: xz | last post by:
Why C++ (as well as Java) adopts class-based access control instead of instance-based access control? I had never paid attention to whether an access-control is class-based or instance-based but...
7
by: Andy B | last post by:
I have a class I am creating for data access. I need to access controls from inside the class that are on a particular page. How do I do this? or is creating an instance of the page class and using...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.