Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Accessing other class objects from within a UserControl?

Posted on 2014-01-10
8
Medium Priority
?
294 Views
Last Modified: 2014-01-13
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?
0
Comment
Question by:BlakeMcKenna
  • 4
  • 3
8 Comments
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 39772422
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.
0
 

Author Comment

by:BlakeMcKenna
ID: 39772459
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
0
 
LVL 40
ID: 39773418
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:BlakeMcKenna
ID: 39776562
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!
0
 
LVL 40
ID: 39776923
Will you use that UserControl in only one project, or is it something that you will need with many projects?
0
 

Author Comment

by:BlakeMcKenna
ID: 39776945
One project only
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 39777150
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.
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 39777306
Thanks James
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Loops Section Overview
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question