none
Imports statements showing up in my vb source file RRS feed

  • Question

  • I was working on a printing utility in a VB source file, and I noticed a new first line:
        Imports System.Drawing.Printing

    On a different occasion, I noticed a different Imports line being added, but I don't remember the namespace name.  I didn't directly add either line to my file.  Maybe I miss-typed something, but I find no documentation for an Imports shortcut key.
    I think VS added it because of what I was working on, intellisense perhaps?  FYI I'm using VS Community 2017, and I've never seen this in earlier versions.

    If it is a feature, I want to turn it off.  If it is a VS bug, I want to report it.  If it is my sloppy typing, I would like to know what keyboard action causes an Imports line to be added at the top of a VB file.  Any ideas?
    Tuesday, October 2, 2018 11:32 AM

Answers

  •  This often happens when creating the event handlers for different controls.  For example,  if I add the Form Closing event to my form class,  it will import the 'System.ComponentModel' which is the Namespace that the 'CancelEventArgs' class is from.  The CancelEventArgs is passed in the second argument of the Closing event.  This is shown in the image below...

     

     This "auto-import" also happens if I add some of the different events from a PrintDocument class.  For example,  if I use the BeginPrint or PrintPage events of a PrintDocument,  it will automatically import the 'System.Drawing.Printing' namespace.  The PrintPageEventArgs and PrintEventArgs from those two events are from the 'System.Drawing.Printing' namespace.

    Imports System.Drawing.Printing 'added automatically
    
    Public Class Form1
        Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
    
        End Sub
    
        Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
        End Sub
    End Class
     

     I don't see a setting in the Options of Visual Studio for stopping this but,  I might have overlooked it.  You might be able to try adding the Namespaces to all the classes in any events like these you may be using.  It might stop it from adding the Import but,  it seems like less work to just keep the import line.

     However,  this is not the only way an Import can be added I'm sure.  Just one way I could remember it happening to me.  8)


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, October 2, 2018 10:53 PM
    • Marked as answer by amercer Wednesday, October 3, 2018 1:40 PM
    Tuesday, October 2, 2018 10:49 PM

All replies

  • When you add components on the Form - Studio do very many things to make this component usable.

    One of those things - adding once required namespaces to where it needed.

    Reference to Dll with required classes also added.


    Sincerely, Highly skilled coding monkey.

    Tuesday, October 2, 2018 11:54 AM
  • Thanks for the reply, but I don't think this is the answer.  I wasn't adding or changing components, and I wasn't changing project properties.  I was reviewing and editing code, and I was using F1 to display .NET documentation.
    Tuesday, October 2, 2018 12:10 PM
  • Let's look at an example where we have a class you created that uses SqlClient. Note the errors.

    By pressing ALT+. the following appears

    By ignoring (in this case) option 1 or 2 the project will not compile. So even if you could get rid of this you would not be able to run the project.

    Import statements are auto added to things like the designer file of a form but will not auto add imports to the other parts of a form unless you you (using the link above) select one of the options.

    Extensions such as Resharper permit what you want but that does cost $$$'s.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, October 2, 2018 2:14 PM
    Moderator
  • I don't recall selecting an Imports line from any popup, and I don't generally use Alt+.  Sloppy typing on my part is certainly a possibility, but I don't recall any visual feedback like a popup.  For certain I didn't type in the offending Imports line.

    The VB file in question contains about 3000 lines made of a few classes and a few modules for extension methods.  I was editing near the middle of the file in a class containing a declaration of a System.Drawing.Printing.PrintDocument variable.  System and System.Drawing are among my project wide imports (project, properties, references).  My precise declaration is:

        Private WithEvents pd As Printing.PrintDocument = Nothing

    On the other hand, it may be a coincidence that the added Imports was about printing.  I continue to be baffled.

    Tuesday, October 2, 2018 7:19 PM
  • I've (to my memory) never seen an Import statement automatically added without my intervention.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, October 2, 2018 7:30 PM
    Moderator
  •  This often happens when creating the event handlers for different controls.  For example,  if I add the Form Closing event to my form class,  it will import the 'System.ComponentModel' which is the Namespace that the 'CancelEventArgs' class is from.  The CancelEventArgs is passed in the second argument of the Closing event.  This is shown in the image below...

     

     This "auto-import" also happens if I add some of the different events from a PrintDocument class.  For example,  if I use the BeginPrint or PrintPage events of a PrintDocument,  it will automatically import the 'System.Drawing.Printing' namespace.  The PrintPageEventArgs and PrintEventArgs from those two events are from the 'System.Drawing.Printing' namespace.

    Imports System.Drawing.Printing 'added automatically
    
    Public Class Form1
        Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
    
        End Sub
    
        Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
        End Sub
    End Class
     

     I don't see a setting in the Options of Visual Studio for stopping this but,  I might have overlooked it.  You might be able to try adding the Namespaces to all the classes in any events like these you may be using.  It might stop it from adding the Import but,  it seems like less work to just keep the import line.

     However,  this is not the only way an Import can be added I'm sure.  Just one way I could remember it happening to me.  8)


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, October 2, 2018 10:53 PM
    • Marked as answer by amercer Wednesday, October 3, 2018 1:40 PM
    Tuesday, October 2, 2018 10:49 PM
  • @Iron,

    I've done FormClosing before as shown below and a Import is not added.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, October 2, 2018 11:21 PM
    Moderator
  • @Iron,

    I've done FormClosing before as shown below and a Import is not added.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

     I am using VS 2015 Community and it adds the imports to my code every time,  for the events I mentioned anyways.  If I delete the Import line,  it shows errors on the classes that are declared in the event subs.  Maybe the namespace is being included somewhere/somehow behind the scenes in VS2017?  I think that is what version you are using.

     

    If you say it can`t be done then i`ll try it

    Wednesday, October 3, 2018 12:01 AM
  • @Iron,

    There are no references in the form designer either. I have VS2015 Enterprise edition and will try with it later, right now I'm I just started the current update to VS2017.

    BTW I don't doubt you get this but curious why you get the import and I don't. Never was a consideration before.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, October 3, 2018 12:24 AM
    Moderator
  •  I am using VS 2015 Community and it adds the imports to my code every time,  for the events I mentioned anyways.  If I delete the Import line,  it shows errors on the classes that are declared in the event subs.  Maybe the namespace is being included somewhere/somehow behind the scenes in VS2017?  I think that is what version you are using.

     

    If you say it can`t be done then i`ll try it

    I cant duplicate it either. I start new forms project. Add formclosing event. Nothing gets imported. I do not have component model reffed. I checked with or without option strict.

    VS 2015 win 7


    Wednesday, October 3, 2018 12:39 AM
  • Okay, tried it with VS2015, no Import statement added, same as VS2017.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, October 3, 2018 12:52 AM
    Moderator
  • Adding an event handler is the answer to my issue.  I remember having added a PrintDocument.QueryPageSettings event handler.  I just replicated the problem by removing and adding it again - the Imports line showed up.

    For those who could not replicate...  Maybe your project wide imports already contain the resulting import.  Maybe your project wide imports conflict with the resulting import.  I added the event handler by clicking on the event in the events pulldown, and maybe that user action is the essential ingredient.

    There may be other user actions that cause this to happen.  Regardless, at least I now understand it.  I must say that I don't like it, and I don't see how to disable it, so I'll have to live with it.  My coding style is to import namespaces sparingly - I will import for a frequently used name.  In my code where a name appears only a few times, I will typically fully qualify it.  So, IMO, and for my coding style, automatic addition of an Imports is unwanted name pollution/clutter.  But I have a workaround thanks to IronRazerz.  And thanks also to all responders.
    Wednesday, October 3, 2018 1:40 PM