Search code examples
c#namespacesusing-directivesvb.net-to-c#

VB.NET namespace abbreviation: How do I make this work in equivalent C# code?


I am a VB.NET programmer by nature and I am having a hard time figuring this out. Any help with the following would be appreciated.

I need to get the C# code (1) below to work. The VB.NET equivalent works just fine, but the C# does not.

Note that both (2) and (3) do work, but this is actually auto-generated code, and I need the VB.NET and C# versions to be as similar as possible.

  1. This does not compile (the fully-qualified name of Engine is ThreeD.QVB.Engine):

    using ThreeD.QVB;
    
    namespace QVBScript
    {
        public class ScriptCode
        {
            public void Main(ref Engine.QVBObjectsDictionary objects,
                             Engine.Commands commands)
            {
                …
    
  2. However, this does work:

    //using ThreeD.QVB; // I'm instead using fully-qualified names in the method
    
    namespace QVBScript
    {
        public class ScriptCode
        {
            public void Main(ref ThreeD.QVB.Engine.QVBObjectsDictionary objects,
                            ThreeD.QVB.Engine.Commands commands)
            {
                …
    
  3. This works, too:

    using eng = ThreeD.QVB.Engine;
    
    namespace QVBScript
    {
        public class ScriptCode
        {
            public void Main(ref eng.QVBObjectsDictionary objects, 
                             eng.Commands commands)
            {
                …
    

Solution

  • In VB.NET if you have an import for the first part of a namespace, you can reference just the later half. In C# you cannot do this. You must have a using for the full namespace, or fully qualify your type names. Different languages, different rules.

    In your last example you do not need to use the alias.

    using ThreeD.QVB.Engine;
    
    namespace QVBScript
    {
        public class ScriptCode
        {
            public void Main(ref QVBObjectsDictionary objects, Commands commands)
            {
                UI.Output Output = (UI.Output)objects["Output"];