Solved

referencing a class library

Posted on 2015-01-20
8
271 Views
Last Modified: 2015-01-22
making the jump from classic asp over to .NET, I'm plowing through various exercises while teaching myself .NET, I'm not trying to master and understand the concept of what we referred to in classic asp as "include files", re-usable libraries to include in different projects.

I've created a stand alone class library, defined as:

namespace CCG_Data_Objects
{
    public class BDUSA_CCG_DataObjects
    {
        .......whole bunch of sub classes here

Open in new window


I compiled it into its own DLL, and everything looks good on that end.

Now, in a brand new project, I want to reference all of that code. I don't want to include the actual CS file, as if I make changes to it, I want to be able to use it in multiple projects and only have to modify the code once. So, I added the PROJECT for the class library to my new solution and added a reference to the project under REFERENCES. It's my understanding that this'll allow me debug easier having that included in my solution, as opposed to just referencing the the DLL. Is this the only reason to do this?

I now added the following line to my new project that I want to access the code in:

using CCG_Data_Objects;

within my main class, I want to go to access one of the classes within my library, by doing:

BDUSA_CCG_DataObjects.DAL_Class dbHelper = null;

which is defined simply as:

        public class DAL_Class
        {
            string _connString;
            string _libraryID;
            string _siteID;

          public DAL_Class(string dbConnString, int libraryID)
            {
                _connString = dbConnString;
                _libraryID = libraryID.ToString();
            }

            public DAL_Class(string dbConnString, int libraryID, int siteID)
            {
                _connString = dbConnString;
                _libraryID = libraryID.ToString();
                _siteID = siteID.ToString();
            }

       ...a whole slew of other public functions

Open in new window


The two functions showing, I cannot seem to access. if I try to do something like:

dbHelper = new dbHelper.DAL_Class(_targetConnString, _libraryID, _targetSiteID);

I get a red line under dbHelper (I'm using VS2013) with a message saying

"type or namespace "dbHelper" cannot be found

if I change it to

dbHelper = new BDUSA_CCG_DataObjects.DAL_Class(_targetConnString, _libraryID, _targetSiteID);

it works, which doesn't make sense to me, since it's a sub-class of DAL_Class. It would seem that

dbHelper = new BDUSA_CCG_DataObjects.DAL_Class.DAL_Class(_targetConnString, _libraryID, _targetSiteID);

should work (it doesn't) more so. Am I going to have to specify BDUSA_CCG_DataObjects every time I want to reference a class in my class library? is there a way to NOT to have to do this, as I understood the whole point of references was to just have that code included as is. I know if I include the actual cs file, then I do NOT have to specify the class name.

I know there's a lot here, I'm looking more to understand than to fix things (although that's my ultimate goal!). I appreciate any knowledge passed my way to help me untangle all of this :)

Cheers
Josh
0
Comment
Question by:Big Monty
  • 3
  • 3
  • 2
8 Comments
 
LVL 32

Expert Comment

by:it_saige
ID: 40560295
Thats because dbHelper is not a namespace or a type (explicitely) it represents a type of 'DAL_Class'.  Consider the following:
using System;
using People;

namespace EE_Q28600217
{
	class Program
	{
		static void Main(string[] args)
		{
			Person p = null;
			p = new Person() { ID = 1, Name = "Peter" };
			Console.WriteLine(p);
			Console.WriteLine("P is a {0} type.", p.GetType());
			Console.WriteLine("The type for p is located in the {0} namespace.", p.GetType().Namespace);
			Console.ReadLine();
		}
	}
}

namespace People
{
	public class Person
	{
		public int ID { get; set; }
		public string Name { get; set; }

		public Person() : this(-1, string.Empty) { ;}

		public Person(int ID) : this(ID, string.Empty) { ;}

		public Person(int ID, string Name)
		{
			this.ID = ID;
			this.Name = Name;
		}

		public override string ToString()
		{
			return string.Format("{0} is defined with and ID of - {1}", Name, ID);
		}
	}
}

Open in new window

Which produces the following output -Capture.JPGp *is an* object of the type 'Person' in the namespace 'People'.  p is not the type or namespace.

-saige-
0
 
LVL 32

Author Comment

by:Big Monty
ID: 40560376
i understand dbHelper is not a namespace, what I'm trying to figure out is why do I need to specify BDUSA_CCG_DataObjects when trying to access my sub classes, but if I include the cs file directly into my solution, I do not.
0
 
LVL 32

Accepted Solution

by:
it_saige earned 400 total points
ID: 40560486
I was locking in on your comment concerning:
dbHelper = new dbHelper.DAL_Class(_targetConnString, _libraryID, _targetSiteID);

I get a red line under dbHelper (I'm using VS2013) with a message saying

"type or namespace "dbHelper" cannot be found
However, in answer to your most immediate question, you should not have to include the code file directly into your project.  You should be able to add the reference, add the using statement specifying the appropriate namespace and then access the class itself.  For example -

1. I took the Person class from the preceding example and created a new Library project. I then compiled this project and added a reference to the DLL to the Original project.

Capture.JPG

2. I can now either access the Person class by explicitly defining the path 'People.Person' or by way of adding a using statement.

Capture.JPGCapture.JPGDoing it in this way, I can still access the dll's methods in the case of debugging because VS can locate the PDB file.Capture.JPGIf I hit F11 to step into the assignment:You will notice that it loaded the associated code file for the Person class but Person.cs does not appear in my Solution Explorer window.Now it is also possible that you have a Namespace.Class with the same nomenclature; e.g. -
namespace People
{
	namespace Person
	{
		public class Person
		{
			public int ID { get; set; }
			public string Name { get; set; }

			public Person() : this(-1, string.Empty) { ;}

			public Person(int ID) : this(ID, string.Empty) { ;}

			public Person(int ID, string Name)
			{
				this.ID = ID;
				this.Name = Name;
			}

			public override string ToString()
			{
				return string.Format("{0} is defined with and ID of - {1}", Name, ID);
			}
		}
	}
}

Open in new window

Which then produces problematic results as the compiler can't differentiate between do you want to use the class or the namespace.Capture.JPGTo which you can either add the associated namespace to your using statement -Capture.JPGOr explicitly define it -Capture.JPGHTH,

-saige-
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40560600
What do you mean by "sub class"? Can you show a simplified example?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
ID: 40560604
If you mean something like this:

public class Outer
{
    public class Inner
    {
    
    }
}

Open in new window


...then you must include Outer when trying to instantiate Inner, even though Inner is also public. I don't know the exact technical reason--I'm sure it's spelled out in the C# specification somewhere--but that's how it works. In other words, to create a new instance of Inner:

Outer.Inner instance = new Outer.Inner();

Open in new window

0
 
LVL 32

Author Comment

by:Big Monty
ID: 40560611
thanks for putting this all together, I really appreciate it :) very helpful...

you should not have to include the code file directly into your project.  You should be able to add the reference, add the using statement specifying the appropriate namespace and then access the class itself

I am able to do this with a reference to the class library project. I believe the reference to the DLL works as well, but I was told by somebody here at work that I wouldn't be able to step into it. I'll try it once I have more code accessing the class library

I can now either access the Person class by explicitly defining the path 'People.Person' or by way of adding a using statement.

say you had a sub class in your Person class:

public class Person
		{
			public int ID { get; set; }
			public string Name { get; set; }
 
                        public class PhysicalAttributes {
                               public string head { get; set; }
                               public string chest { get; set; }
                               ....
           
                               public string isMaleOrFemale( int ID ) {
                                     ...
                               }
                        }
                 }

Open in new window


The only way, given that you have a reference set to the class library, to call PhysicAttributes would be:

Person.PhysicalAttributes( 1 );

correct?

As I was about to type out my reason for asking, I just had an "AHA" moment, and think I finally understand why in another project of mine that just has the class library code included in it, and I don't need to specify the base class name in this example, is well, there is no base class :) I think if I just remove the base class definition from my class library, it'll work in the same manner as this other project

thanks for sticking with me on this, I'm going to go play around and report back!
0
 
LVL 32

Author Comment

by:Big Monty
ID: 40560612
Kaufmed, that was exactly my "AHA" moment!
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40560618
NP. I would caution you to use the term "nested class" or "nested type" versus "sub class". Sub-classing in Object Oriented programming--which is what .NET is--refers to creating a class which derives from another class.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

9 Experts available now in Live!

Get 1:1 Help Now