none
"using" in C# vs "Imports" in VB.NET RRS feed

  • Question

  • Hi, all,
    I am a VB.NET programmer which currently tries to write something in C# and may be in future migrate to C#.
    In VB.NET using Imports keyword I can import not only namespaces, but also classes, enums etc. For example:
    Namespace MyNamespace
    
    Public Class MyConstants
    
    Public Enum LongNameConstants
       one
       two
       three
    End Enum
    
    End Class
    
    End Namespace
    
    'in other file
    
    Imports MyNamespace.MyConstants.LongNameConstants
    Public Class MyClass
       Private m_e As LongNameConstants = one
    End Class
    

    In C# using using keyword I can only import namespaces. So I have to write long, and sometimes very long names, like this:

    using MyNamespace;
    
    public class MyClass
    {
       private MyConstants.LongNameConstants m_e = MyConstants.LongNameConstants.one;
    }
    

    Is there any trick to make these names shorter?
    In other words, is there a way for "using" not only namespaces, but also types?
    Saturday, October 24, 2009 12:44 PM

Answers

  • Hi,

    I am not sure: As I just did a test, "using" is better.

    The first file:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text; 

    namespace MyNamespace
    {
        class MyConstants
        {        
        }

        public
    enum LongNameConstants
        {
                one,
                two,
                three
        }
    }


    during testing: 

    using

     

     

     

    MyNamespace;

     

    and I have already, have access to the MyConstants class and I have access to
    the LongNameConstants enum.

    Have a nice day...

    Best regards,
    Fisnik  

    and I have already, have access to the MyConstants class and I have access to
    the LongNameConstants enum.

    Have a nice day...

    Best regards,
    Fisnik  


    Coder24.com

    • Marked as answer by Bin-ze Zhao Wednesday, October 28, 2009 9:43 AM
    Saturday, October 24, 2009 1:03 PM
  • Yes in VB you can use Imports keyword with types.

    But in c# using keyword can be only used with type too but with the only way below.

    using

     

    cmd = System.Data.SqlClient.SqlCommand;

     

    if you type cmd you get SqlCommand object.

    From MSDN

    The using directive has two uses:

    To permit the use of types in a namespace so you do not have to qualify the use of a type in that namespace:

    using System.Text;

    To create an alias for a namespace or a type.

    using Project = PC.MyCompany.Project;

    http://msdn.microsoft.com/en-us/library/sf0df423(VS.80).aspx

    • Marked as answer by Bin-ze Zhao Wednesday, October 28, 2009 9:44 AM
    Saturday, October 24, 2009 1:23 PM

All replies

  • Hi,

    I am not sure: As I just did a test, "using" is better.

    The first file:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text; 

    namespace MyNamespace
    {
        class MyConstants
        {        
        }

        public
    enum LongNameConstants
        {
                one,
                two,
                three
        }
    }


    during testing: 

    using

     

     

     

    MyNamespace;

     

    and I have already, have access to the MyConstants class and I have access to
    the LongNameConstants enum.

    Have a nice day...

    Best regards,
    Fisnik  

    and I have already, have access to the MyConstants class and I have access to
    the LongNameConstants enum.

    Have a nice day...

    Best regards,
    Fisnik  


    Coder24.com

    • Marked as answer by Bin-ze Zhao Wednesday, October 28, 2009 9:43 AM
    Saturday, October 24, 2009 1:03 PM
  • Well, I can use multiple Imports:
    Imports MyNamespace
    Imports MyNamespace.MyConstants
    Imports MyNamespace.MyConstants.LongNameConstants

    and this give me access to MyClass, MyConstants and LongNameConstants

    Also, what if I don't want to write each time Math.Abs(x, y) , for example?
    In VB I can write:
    Imports System.Math
    and then just write each time Abs(x, y) without Math.
    Saturday, October 24, 2009 1:18 PM
  • Yes in VB you can use Imports keyword with types.

    But in c# using keyword can be only used with type too but with the only way below.

    using

     

    cmd = System.Data.SqlClient.SqlCommand;

     

    if you type cmd you get SqlCommand object.

    From MSDN

    The using directive has two uses:

    To permit the use of types in a namespace so you do not have to qualify the use of a type in that namespace:

    using System.Text;

    To create an alias for a namespace or a type.

    using Project = PC.MyCompany.Project;

    http://msdn.microsoft.com/en-us/library/sf0df423(VS.80).aspx

    • Marked as answer by Bin-ze Zhao Wednesday, October 28, 2009 9:44 AM
    Saturday, October 24, 2009 1:23 PM
  • Sorry but using won't do that. Sometimes it is inconvenient.

    Rod
    Saturday, October 24, 2009 1:46 PM
  • I didn't think the OP's example looked right - and on testing it definitely is not.  Even though you can use 'Imports' on a type in VB, this doesn't allow you to refer to enum values unqualified - VB (and C#) always required you to qualify enum values.  Curiously, VB allows you to use 'Imports' on an enum, but it doesn't do you much good since you still can't access the values unqualified.

    Also to correct one of the responses, C# does allow you to use 'using' on types, but only when you supply an alias.
    e.g., the following is correct in C# and provides most of the benefit of 'Imports' when used on types:

    using myAlias = SomeNamespace.SomeClass;

    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Saturday, October 24, 2009 3:16 PM
  • VB lets me use Imports on an enumeration, at least in this code:

        Imports System.Drawing.Drawing2D.PenAlignment
        Imports System.Drawing.Drawing2D.LineCap
        ...
        Dim p As New Pen(Color.Red)
        p.Alignment = Center
        p.EndCap = ArrowAnchor

    Cool tip about using an alias in C#.

    Rod

    Saturday, October 24, 2009 3:32 PM
  • That's strange - VS 2008 won't let me use the OP's sample - I get an error: "name one is not declared" when I try it.
    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Saturday, October 24, 2009 3:46 PM
  • I write in VB for years and never use fully qualified enumeration names - just because of laziness :)
    So I can give a full guarantee that VB does allow that.
    Saturday, October 24, 2009 4:05 PM
  • Ahhh - I see what the problem is - my test VB project had a root namespace - this has to be included in the 'Imports' qualification in order to allow unqualified access to the enum value.  Not sure why the lack of full qualification in the 'Imports' wasn't flagged as an error by the compiler though... gotta love VB!


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Saturday, October 24, 2009 4:41 PM
  • I've just seen alert in Inbox with your previous answer - so sometimes it's worthwhile not to answer immediately :)
    Glad that problem is gone.
    Saturday, October 24, 2009 5:00 PM
  • VB lets me use Imports on an enumeration, at least in this code:

        Imports System.Drawing.Drawing2D.PenAlignment
        Imports System.Drawing.Drawing2D.LineCap
        ...
        Dim p As New Pen(Color.Red)
        p.Alignment = Center
        p.EndCap = ArrowAnchor

    Cool tip about using an alias in C#.

    Rod

    Hi ALL,

    Yes I know this is an old thread, but for those of you who also dabble in Vb.Net

    You can also use an ALIAS name in Vb.Net too.

    E.G:

     

    Imports Envt = System.Environment
    
    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        MessageBox.Show("Hello " & Envt.NewLine & "world!!")
    
      End Sub
    End Class
    

     


    Regards, John
    Monday, July 19, 2010 2:15 AM
  • Realize this is an old thread, but just wanted to say thanks for the discussion and info.  I've started coding in C# and this is definitely an annoyance that I was hoping to avoid, but it seems that you can't.  I guess they just spoiled us a bit too much with VB.net by allowing you to import namespace types and call the methods without instantiating or using fully qualified names. 
    Saturday, April 2, 2011 4:07 PM