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?
BlakeMcKennaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Craig WagnerSoftware ArchitectCommented:
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.
BlakeMcKennaAuthor Commented:
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)PresidentCommented:
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.
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

BlakeMcKennaAuthor Commented:
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)PresidentCommented:
Will you use that UserControl in only one project, or is it something that you will need with many projects?
BlakeMcKennaAuthor Commented:
One project only
Jacques Bourgeois (James Burger)PresidentCommented:
Good, that is the easiest one to implement.

Then, put the UserControl in the project.

From there, it will be able to access anything that is declared Public or Friend in any module in the application.

To be able to use the business layer (a .dll I suppose), you first need to reference it through the References tab of the project's Properties window. There is a button there to Add a reference. The way to go dépends on whether you are the programmer developing the .dll or if you have access only to the compiled .dll.

If you have access only to the compiled .dll, then go for Browse in the compiled .dll and navigate to your .dll. Knowing its namespace, you will be able to use all the Public elements of the .dll.

If you write the code for the business layer yourself, you could use the same technique. However, you will probably want to be able to have access to both the code of the application and the business layer while developing and debugging. In such a case, add the business layer project to the application solution through File...Add...Existing project. And in the reference tab, instead of referencing the compile .dll, go through Solution and select your business layer project. You will be able to use all the Public elements of the .dll, but will also be able to debug it at the same time that you use it in your application.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BlakeMcKennaAuthor Commented:
Thanks James
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.