Question Defining Controls from a different class

josh2009

Member
Joined
Sep 16, 2009
Messages
14
Programming Experience
Beginner
Hi newbie here. Just started working with VS 2005 VB and have a question. How do I define the controls from another class? Here is my code (highlighted) -

Imports System.Windows.Forms

Namespace Bryanlgh.Echo.Views
Namespace Bryanlgh.Core.Tasks
Public Class Report
Inherits System.Windows.Forms.UserControl

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If cboPcpMd.Text = "" Then
MessageBox.Show("Please enter a Primary Care Physician!")
End If
If cboPhysicianPerforming.Text = "" Then
MessageBox.Show("Please enter an Attending Physician!")
End If
End Sub
End Class

The controls cboPcpMd & cboPhysicianPerforming are from the class PatientInfoJovyEcho with a Namespace of Bryanlgh.Echo.Views. So in this other class Report, I added the Namespace Bryanlgh.Echo.Views but when I compile I get 3 messages -
1. 'Namespace' statement must end with a matching 'End Namespace'.
2. Name 'cboPcpMd is not declared.
3. Name 'cboPhysicianPerforming' is not declared

I also tried using Imports Bryanlgh.Echo.Views instead of Namespace and it also came up with 3 error messages -
1. Namespace or type specified in the Imports ' Bryanlgh.Echo.Views' doesn’t contain a public member or cannot be found.
2. Name 'cboPcpMd is not declared.
3. Name 'cboPhysicianPerforming' is not declared

How should I define these controls? Any help will be greatly appreciated.

Thanks.
Victor
 
Let's work on your terminology a bit. Those controls aren't from another class. Controls ARE classes themselves. Each class, and other types too, are members of a namespace and they are defined in an assembly, i.e. a DLL. Each assembly may contain members of multiple namespaces and different members of the same namespace may be defined in different assemblies.

So, if you want to use a type that is defined in an external assembly then the first thing you need to do is reference that assembly, which is done on the References page of the project properties. Note that, if you add a component to a form or user control in the designer then a reference to the assembly it's defined in is added automatically. You can add items to the Toolbox by right-clicking.

So, adding a reference is all you need to be able to use any type that assembly contains. If that's all you do though, you'll have to qualify the name of the type with its namespace every time you refer to it. To avoid that you import the namespace. You can do that project-wide, again, on the References page of the project properties. To do it for a single code file you add an Imports statement to the top of that code file.
 
Hi,

Thanks for the quick reply. As you can tell I'm a newbie with VS 2005 and don't have the terminology down yet. The reason why I called it class was because up at the top of my code window where I added my button to access the controls, it says Public Class Report that's why I referred to it as an assembly and that controls are really types if I understand you correctly. Anyways, in my assembly Report, I've added this button to access another type defined in an external assembly which is PatientInfoJovyEcho with a Namespace of Bryanlgh.Echo.Views. In my assembly Report, I went into Properties and clicked on CardioDocProject properties and under Assembly name I have CardioDocProject. My assembly (which I referred to as class) Report is actually in the same project CardioDocProject, so I am confused now. PatientInfoJovyEcho and Report are two different screens each in its own separate tab but they both reside in the same application. I then proceeded to clicking on the tab References and under the Imported Namespaces I see Microsoft.VisualBasic. And right above it, there are several Reference Names listed. Now underneath, I see a box with different items listed Microsoft Visual Basic with the check box having a check mark in it. There are several System.* names with check marks on them. I also see the project CardioDocProject and I checked it then saved my changes and went back into the vendor provided toolkit to compile and I still get the error messages that cboPCPMD and cboPhysicianPerforming are not declared. Pls help. Thanks
 
Back
Top