[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

referencing a class library

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
Big Monty
Asked:
Big Monty
  • 3
  • 3
  • 2
2 Solutions
 
it_saigeDeveloperCommented:
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
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Author Commented:
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
 
it_saigeDeveloperCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
käµfm³d 👽Commented:
What do you mean by "sub class"? Can you show a simplified example?
0
 
käµfm³d 👽Commented:
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
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Author Commented:
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
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Author Commented:
Kaufmed, that was exactly my "AHA" moment!
0
 
käµfm³d 👽Commented:
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now