Avatar of BlakeMcKenna
BlakeMcKenna
Flag for United States of America asked on

Accessing other class objects from within a UserControl?

I kind of asked this question before but didn't really word it correctly.

I have several projects within my solution including a new Windows Control Library project. In my new control, I need to access public objects, i.e. variables, methods, functions from within my main project but not sure how to do it. I tried adding a reference of my main application to the Windows Control Library project but it would not let me saying:

"A reference to 'WindowsApplication1' could not be added. Adding this project as a reference would cause a circular dependency".

So, my question is, from within the code of my UC, how do I access objects from other classes, specifically my main project?
Visual Basic.NET

Avatar of undefined
Last Comment
BlakeMcKenna

8/22/2022 - Mon
Craig Wagner

The situation you have is that your main application assembly depends on your library and your library depends on your main application assembly (the "circular dependency" that Visual Studio complained about).

You could probably find a way to accomplish what you want using late binding or dynamic types, but in my opinion the problem is more fundamental.

What this tells me is you've organized your assemblies/projects incorrectly. If the library has a dependency on the main application then it can never be used by any other application. Given that, some or all of the contents of the library should actually be in the main application assembly, not a separate assembly.
BlakeMcKenna

ASKER
I kinda wondered if I should have done that...just put the UC in the main assembly. I'll give that a try and see what happens!

Thanks
Jacques Bourgeois (James Burger)

If the user control was designed to be used in only one application, then it should definitively be included in that application.

But it if was designed to be used in many different applications, the dll is the way to go.

What you want to do can be done, but should be part of the design on the class (a user control is a class). Adding it later might require more work. This involves more thinking while before coding.

Think of the Parent property of a TextBox, that references the form that contains it. The code of a TextBox is in a dll written by Microsoft, but can reference and use a Form that is in your application.

You could do something similar in a user control located in a dll, by adding properties that can be set to reference basic objects in the application. If you are interested, tell me and I will prepare short example.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
BlakeMcKenna

ASKER
James,

That was a great explanation. What I am trying to do from within my UC is reference Module variables, subs, functions, etc. from my main assembly. I am also wanting to reference the Business Layer, which is a separate Project within my solution. If you could show me some sample code utilizing this kind of logic, that would help me tremendously!

Thanks again!
Jacques Bourgeois (James Burger)

Will you use that UserControl in only one project, or is it something that you will need with many projects?
BlakeMcKenna

ASKER
One project only
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Jacques Bourgeois (James Burger)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
BlakeMcKenna

ASKER
Thanks James