Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Accessing other class objects from within a UserControl?

Posted on 2014-01-10
8
Medium Priority
?
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

715 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