Solved

referencing a class library

Posted on 2015-01-20
8
302 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 33

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 33

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 33

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40560600
What do you mean by "sub class"? Can you show a simplified example?
0
 
LVL 75

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 33

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 33

Author Comment

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Winforms -- Capture CTRL+SHIFT+D at the form not working? 3 25
Syntax Issue with SSIS module 26 101
How to read text with RegEx... 2 25
asp.net mvc 2 21
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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