locked
Trouble with Imports System.Drawing RRS feed

  • Question

  • Good evening,

    I created a class in my windows project and it did such wondrous things that I decided to add it to the "Class Library."  This is my first attempt at doing this so I hope that you have patience with my newbie attempt.

    Within the project I highlighted and copied the class to the clipboard. I then created a new project using the Class Library template and pasted the class into the new Class1.vb file. 

    As I said, the class was working great in the windows project, but in the new class library declarations for a graphics object, point, rectStyles and other graphics objects are not recognized.

    I assume that I need to import the System.Drawing class to this file.  The windows project worked without importing anything.  This is the state of my code.

    Imports System.Drawing

    Public Class DrwTxtRect

    #
    Region "Internal Variables"
      Private _g As graphics
     
    Private _txt As String
      Private _txtSize As SizeF
     
    Private _rectLoc As Point
     
    Private _rectSize As Size
     
    Private _rectStyle As rectStyles
     
    Private _txtFit As Boolean
      Private _txtPos As txtPositions
     
    Private _txtFont As Font
     
    Private _txtBrush As Brush
     
    Private _rectPen As Pen
     
    Private _rectBrush As Brush
     
    Private _rectBrdr As Int32
    #
    End Region
     
    ‘Class Code goes here
    End Class

    Intellisense is not happy with the Imports statement. It whines that "The Namespace or type specified in the Imports 'System.Drawing' doesn't contain any public member or cannot be found." In the declaration for _g as graphics, Intellisense complains that "Type 'graphics' is not defined."

    Your assistance in solving this problem and in my education would be appreciated, thanks.

    Tuesday, June 27, 2006 2:26 AM

Answers

  • OK, a point to note with a class library.

    You need to add a reference to you class library.    The imports statement will merely shorten the namespace for any namespaces classes in you class library.

    As an example,   You can type in a windows application

    Dim x as new textbox

    The fully qualified name is in fact

    Dim x as New System.Windows.Forms.textbox

    But you wouldnt want to type this everytime so you project has a default project level imports statment

    Imports System.Windows.Forms

    which allows you to type the shortened form.   You can see this by going to My Projects -> References ->Imported namespaces

    A reference to the appropriate dll is automatically placed in your project for the windows forms applications.   You can type the fully qualified name in and that will work as well.

    But if you go to My project -> References   you will see a reference to system.windows.forms.dll    

    Remove this from you project and the apart from the windows forms application breaking you will notice that either.   You can notice this by creating a console application and hey presto - no system.windows.forms.dll will be referenced and the following two commands will fail.   You can put an imports statement in your console application and it still wont help.

    dim x as New System.Windows.Forms.textbox

    or

    dim x as New textbox

    You need both a reference to the appropriate DLL (Class Library) and the imports statement to allow you to use the shortened names.    If you only have the imports the functions will not be there.     Also an imports statement in the class dll

    So in a console application add a reference to system.windows.forms.dll and then try these two lines.   Only the fully qualified name will work, then put at the top of the file Imports System.Windows.Forms and then both of them will work.

    If you specify the imports statement in the file - it also has file level scoping.   If you want it to apply to the entire project then you need to add the imports namespace in the My Project -> References -> Imported Namespaces.

    I would be 100% sure you havent added a reference to the class library and if you used the imports statement in the class library then its only scoped to the file/project that it was used in.   So this would be the class library.

     

    Tuesday, June 27, 2006 3:32 AM

All replies

  • OK, a point to note with a class library.

    You need to add a reference to you class library.    The imports statement will merely shorten the namespace for any namespaces classes in you class library.

    As an example,   You can type in a windows application

    Dim x as new textbox

    The fully qualified name is in fact

    Dim x as New System.Windows.Forms.textbox

    But you wouldnt want to type this everytime so you project has a default project level imports statment

    Imports System.Windows.Forms

    which allows you to type the shortened form.   You can see this by going to My Projects -> References ->Imported namespaces

    A reference to the appropriate dll is automatically placed in your project for the windows forms applications.   You can type the fully qualified name in and that will work as well.

    But if you go to My project -> References   you will see a reference to system.windows.forms.dll    

    Remove this from you project and the apart from the windows forms application breaking you will notice that either.   You can notice this by creating a console application and hey presto - no system.windows.forms.dll will be referenced and the following two commands will fail.   You can put an imports statement in your console application and it still wont help.

    dim x as New System.Windows.Forms.textbox

    or

    dim x as New textbox

    You need both a reference to the appropriate DLL (Class Library) and the imports statement to allow you to use the shortened names.    If you only have the imports the functions will not be there.     Also an imports statement in the class dll

    So in a console application add a reference to system.windows.forms.dll and then try these two lines.   Only the fully qualified name will work, then put at the top of the file Imports System.Windows.Forms and then both of them will work.

    If you specify the imports statement in the file - it also has file level scoping.   If you want it to apply to the entire project then you need to add the imports namespace in the My Project -> References -> Imported Namespaces.

    I would be 100% sure you havent added a reference to the class library and if you used the imports statement in the class library then its only scoped to the file/project that it was used in.   So this would be the class library.

     

    Tuesday, June 27, 2006 3:32 AM
  • Spotty,

    Thanks, I added the reference System.Drawing to The Class Library project and Intellisense stopped whining about the Imports statement and the declarations for the Drawing objects.  I have not yet fully grasped half of what I need to know about “Reference” and “Imports,” but your response has gotten me started on the road of Knowing.  Thanks.

    Tuesday, June 27, 2006 12:54 PM