Solved

Accessing other class objects from within a UserControl?

Posted on 2014-01-10
8
282 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now