Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 29th, 2006, 08:49 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default Namespaces / Importing / References

I have always had a very hard time with Imports in school as my professors really skimmed over it--and I was taught in "old school" C++ writing simple programs from a command line. In those programs I always had code at the top, such as "Imports" Suchandsuch and "using namespace" without knowing what I was doing. Well, let me take that back, I was told that I was "including" header files of classes--but didn't know if the entire class (implementation) was actually being compiled--and how the compiler knew where to look for the classes I was importing.

Now, with VB 2005, I use "Imports" but, from what I can understand, this is just referencing a namespace. My underlying question is how the compiler knows where to find all the classes I am using?...perhaps I can break down my thought process/questions as such:

a) When I was "#include" ing in C++---how did the compiler know where to find all the classes I was using--for example..the CString class? Suppose I wanted to use someone's "already written" class--how would I use that in my program--would I have to put it somewhere so that the compiler could find it?

b) In VB 2005, then, same question. How does my program know where to find classes--I assume that all the classes are part of the framework and, therefore, simply "importing" the namespace will tell my program where to find the class?

c) In VB 2005--suppose YOU (person reading this) were to write a class for me--how would I go about using that in my own code? (I assume I would somehow have to get it into the framework and then "Import" it?)

I am just getting confused on import/namespace/include. I seem to be getting a fairly good grasp on namespace---but I have a hard time understanding how .NET or my old C++ is finding classes to use--and how I am getting them into my own code.

Kindest Regards,
Robert Searing

 
Old August 29th, 2006, 09:50 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

[u]Exactly</u> what I have wondered as I read about Imports in the text books and in Help. (One point though: Imports doesn't strictly reference a namespace, as I understand it. It shorthands it, making the compilation process substitute the entire class-referencing string for the 'abbreviated' specification you enter into your program code, much as a Const is replaced with the literal value at compilation. But then, erhaps this is exactly what adding a reference does, though.)

I am eagerly awaiting reading the responses you get to this!
 
Old August 29th, 2006, 10:11 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

I understand imports to simply reference a namespace---so that you don't have to qualify the class/methods each time. I would assume, however, that the simple fact that the classes are part of the framework, that simply stating the namespace let's the compiler know where to look--not sure...which leads to my other question---how do I import classes someone else has written?

It's just so confusing--when I first didn't grasp what I was doing in C++ with #include...sigh.

Regards,
Rob

 
Old August 29th, 2006, 02:55 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Hi Rob

You are having a hard time comprehending what’s kept where because you somehow skipped or missed the very place where ‘it all actually resides’.
The implantation of any class in .NET, when you use it, comes from the ‘Assembly’ it resides in.

For an instance, most of the standard .NET classes like ‘CString’ that you use reside in a library assembly called mscorlib.dll.

Now if I were to write a class, lets say ‘Consultancy’ for you, I would make it into a Class Library project. Lets callthe project AnkurLib. I would define the class in a namespace ‘Programming’.
With ‘AnkurLib’ chosen as the project name, building it would yield an assembly called AnkurLib.dll.

Now if you want to use Consultancy class, you will ‘Add a Reference’ in your project under which you will actually browse for ‘AnkurLib.dll’ and ‘include’ it in your project. By default what this will do is to make a copy of AnkurLib.dll and store it in your project’s bin folder along with our program’s executable.
Nothing you have done in the code yet.

To make an object of class Consultancy you will either write something like

Dim S as Programming.Consultancy

Or you will import the Programming namespace using import statement and than use Consultancy directly writing a code something like

Dim S as Consultancy


I’ll discuss namespaces in a short while.
To review:
ALL CLASSES, the ones that you’ve written or the standard ones reside in their corresponding assemblies.





How are namespaces important now?

Well getting back to AnkurLib project. Lets say you want me to provide you consultancy not only on Programming issues but on Administration issues as well.
For this purpose I may define another class ‘Consultancy’ in AnkurLib Project but I’ll define that in another namespace called Administration.
With that I would have defined two classes with the same name but they wont clash with each other as they have been defined in different namespaces. And if you want to make an object of the other consultancy you can do that by writing something like

Dim S as Administration.Consultancy



Importing a namespace makes all classes defined under that namespace available for the code in the scope of the import statement.
Import Programming
makes all the classes defined in namespace Programming available to the code following it.

Import Administration
makes all the classes defined in namespace Administration available to the code following it.

If you would use both the statements in your code both definitions of Consultancy will become available to the code following which will be the case of ambiguity could be resolved if I had designed my library properly but it should make the concept clear.

You add an assembly to a project to make all the classes in that assembly available for use in that project.
You use the namespaces to make all the classes in that namespace available for use in the code in the import statement’s scope.


You can further control the availability of classes by making them public/private
etc.

Regards
Ankur
 
Old August 29th, 2006, 03:16 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Interesting.
So I opened a project, and added mscorlib.dll.
Now (apparently) I have the following 2:
Code:
  mscorlib.System.Diagnostics  ' Coming from mscorlib.dll
  System.Diagnostics           ' Coming from System.DLL (and having a far 
                               ' different collection of available 
                               ' pieces—N—parts)
 
Old August 29th, 2006, 03:40 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

Ankur,

Thank you VERY much for your reply. I think if you could clarify a few points..this jigsaw puzzle might fall into place....thank you for your time.

a:
You are having a hard time comprehending what’s kept where because you somehow skipped or missed the very place where ‘it all actually resides’.
The implantation of any class in .NET, when you use it, comes from the ‘Assembly’ it resides in.

For an instance, most of the standard .NET classes like ‘CString’ that you use reside in a library assembly called mscorlib.dll.


So, what happens when I build my project--does it only include in my compiled file (.exe or .dll I assume) those mscorlib.dll classes that I use. In other words, when I build my project, it won't put the entire mscorlib into it, obviously--but only those classes I use..such as CString?

b:
Now if I were to write a class, lets say ‘Consultancy’ for you, I would make it into a Class Library project. Lets callthe project AnkurLib. I would define the class in a namespace ‘Programming’.
With ‘AnkurLib’ chosen as the project name, building it would yield an assembly called AnkurLib.dll.


Ok--what would happen if I didn't put it in namespace "Programming"? Would I be able to refer to the Consultancy class alone--(assuming that the namespaces I am using don't have their own "Consultancy" class?

c:
Importing a namespace makes all classes defined under that namespace available for the code in the scope of the import statement.
Import Programming
makes all the classes defined in namespace Programming available to the code following it.

Import Administration
makes all the classes defined in namespace Administration available to the code following it.


So--if I imported Programming and Administration and had a Consultancy class in either..the following would provide an error:

Dim s As Consultancy

..because it woulnd't know which "namespace" i am referring?

d:
Lastly---and this is all starting to make sense now---you are saying that all the classes I use so far...(eg System, System.Data.SqlClient..etc) are all part of this mscorlib.dll file...and all I am doing by Import is providing the proper namespace?

Consequently--If I am referencing assemblies--it will only build into my project the classes that I actually use? (For example--if I reference your Ankur.dll file and you have 10 classes...and I only use the Consultancy class---that is all that will be built into my program/.dll?)

(On a side note--when I was writing my C++ files prior to Visual Studio--was the same thing happening with the #include? Was there some "mega" library that C++ referred to when I was including things?)

Ankur..THANK YOU...THANK YOU...THANK YOU!!!!!

 
Old August 30th, 2006, 06:48 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Before I start answering your questions, here is something you need to know.

There are two types of assemblies in terms of how they are used.

1) Private assemblies: When you add a reference to these assemblies, a copy of them gets stored in your application directory along with your executable. AnkurLib.dll in our discussion is an example of such assemblies. Now class ‘Consultancy’ does not get compiled in to your exe by default but is taken out from AnkurLib.dll when it is needed.

2) Shared assemblies: When you add a reference to these assemblies, only a reference to them is added to your executable and again classes don’t get compiled in to our executable. Mscorlib.dll, system.dll etc are all shared assemblies. These assemblies usually reside in a shared reservoir called Global Assembly Cache (GAC) and can be found at C:\WINDOWS\assembly if you are using XP or C:\WINNT\assembly if you are using Win 2000.


To avoid any confusion you can always click a reference under ‘References’ folder in Solution Explorer and the properties pane would show you things like the assembly it refers to and by having a look at ‘Copy Local’ property’s value (True/False) you can determine whether the assembly is shared (False) or private (True).

Quote:
quote:a:
You are having a hard time comprehending what’s kept where because you somehow skipped or missed the very place where ‘it all actually resides’.
The implantation of any class in .NET, when you use it, comes from the ‘Assembly’ it resides in.

For an instance, most of the standard .NET classes like ‘CString’ that you use reside in a library assembly called mscorlib.dll.

So, what happens when I build my project--does it only include in my compiled file (.exe or .dll I assume) those mscorlib.dll classes that I use. In other words, when I build my project, it won't put the entire mscorlib into it, obviously--but only those classes I use..such as CString?
Your executable doesn’t get CString or Consultancy or any outer class compiled in them. When you use them, their implimentation gets loaded from the assembly they reside in.

....
 
Old August 30th, 2006, 06:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Quote:
quote:b:
Now if I were to write a class, lets say ‘Consultancy’ for you, I would make it into a Class Library project. Lets callthe project AnkurLib. I would define the class in a namespace ‘Programming’.
With ‘AnkurLib’ chosen as the project name, building it would yield an assembly called AnkurLib.dll.

Ok--what would happen if I didn't put it in namespace "Programming"? Would I be able to refer to the Consultancy class alone--(assuming that the namespaces I am using don't have their own "Consultancy" class?
Yes you would be able to use Consultancy directly.

Quote:
quote:c:
Importing a namespace makes all classes defined under that namespace available for the code in the scope of the import statement.
Import Programming
makes all the classes defined in namespace Programming available to the code following it.

Import Administration
makes all the classes defined in namespace Administration available to the code following it.

So--if I imported Programming and Administration and had a Consultancy class in either..the following would provide an error:

Dim s As Consultancy

..because it woulnd't know which "namespace" i am referring?
It WILL be flagged as an error like I said following these statements in my last post. Its an error because at this point you have two definitions of Consultancy available to you, so it will be flagged as a case of ambiguity by the compiler.

....
 
Old August 30th, 2006, 06:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Quote:
quote:d:
Lastly---and this is all starting to make sense now---you are saying that all the classes I use so far...(eg System, System.Data.SqlClient..etc) are all part of this mscorlib.dll file...and all I am doing by Import is providing the proper namespace?

Consequently--If I am referencing assemblies--it will only build into my project the classes that I actually use? (For example--if I reference your Ankur.dll file and you have 10 classes...and I only use the Consultancy class---that is all that will be built into my program/.dll?)

(On a side note--when I was writing my C++ files prior to Visual Studio--was the same thing happening with the #include? Was there some "mega" library that C++ referred to when I was including things?)
Much of the answer to this query is already with you I’m sure. You may even want to rephrase this query based on the information you have with you now. I do have to tell you that there is no ‘mega’ library in .NET either. Mscorlib is ‘one of them’ and not the only one. Go to GAC or see the properties of various references in a simple windows application and you would come to know that there are a number of shared assemblies at your disposal when u run even the most simple of applications on framework.

In C++ when you included the header, the class info would get added to the client application. Now if you have bound the library statically all the class implementation would get compiled in you exe, if you have bound it dynamically the class implementation would get loaded from the library when you use it. And even in C++ we had the concept of namespaces.


Regards
Ankur
 
Old August 30th, 2006, 08:27 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

Ankur,

First, thank you for your help--it is tremendously appreciated. This will be my last question on this topic and I am only wanting to see if I understand this correctly.

With .NET---nothing is included (except private assemblies, and none of these would be included with the standard .NET framework--they would have to be downloaded or sent to me). Things are referenced "just in time"?

Lastly, with old C++, when you "#include" you always included the header (.h) file in your exe -- it depended upon dynamic or static binding?---If that is true--I how would I know which was the case. In my classes, I only ever typed #include (using classes such as Cstring or iostream or whatever).

Thank you kindly,
Rob Searing






Similar Threads
Thread Thread Starter Forum Replies Last Post
Namespaces and DLLs Akobold C# 0 March 18th, 2007 08:08 PM
Namespaces Amateur BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 November 27th, 2006 05:19 PM
namespaces anchal C# 1 July 3rd, 2006 02:53 PM
XML Namespaces billy_bob_the_3rd XML 1 January 31st, 2005 03:41 PM
NAMESPACES - WHAT ARE THEY? p_nut33 C# 2 July 31st, 2003 03:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.