.NET Connection in VB.NET

.NET Connection in VB.NET

spayne76BBK
Advocate Advocate
801 Views
1 Reply
Message 1 of 2

.NET Connection in VB.NET

spayne76BBK
Advocate
Advocate

I've recently had success with creating a .NET Joint in C#.  I am now trying to create the same code using VB.NET.

 

I ran into the same error code I was getting when I was trying to create a C# .NET Joint. (The thread I am referring to is Issues with Advance Steel Walkthrough - .NET Connections )  In the end, it turns out that I was using the wrong class library template in Visual Studio.

 

I have verified that I am using the .NET Framework 4.7.2 for this project; the same as the C# project and I believe I am using the correct class this time.  However, I get the following error when I try to run the code.

 

spayne76BBK_0-1677880221581.png

This seems to be a generic error. Trying to debug this doesn't seem to work as the Query subroutine is not running before the error is generated.

 

Does anyone know if it is possible to write a .NET Connection in VB.NET? Or, does it have to be a C# program? I can't find anywhere that would indicate that this is the case. I am assuming that they both produce the same result when they build/compile the project into the *.dll file. (Maybe I am wrong?)

 

There are some differences I have noted between the way the IRule interface is implemented in C# vs VB.NET. Below is the C# and VB.NET code that gets generated when you add the IRule interface to the class.

 

C# Code:

 

 

using System;
using Autodesk.AdvanceSteel.ConstructionTypes;

namespace MyNetJointInterface
{
    public class MyNetJoint : IRule
    {
        public Autodesk.AdvanceSteel.CADLink.Database.ObjectId JointId { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

        public void CreateObjects()
        {
            throw new NotImplementedException();
        }

        public void GetRulePages(IRuleUIBuilder builder)
        {
            throw new NotImplementedException();
        }

        public string GetTableName()
        {
            throw new NotImplementedException();
        }

        public void Load(IFiler filer)
        {
            throw new NotImplementedException();
        }

        public void Query(IUI ui)
        {
            throw new NotImplementedException();
        }

        public void Save(IFiler filer)
        {
            throw new NotImplementedException();
        }
    }
}

 

 

 

VB.NET Code:

 

 

Imports Autodesk.AdvanceSteel.CADLink.Database
Imports Autodesk.AdvanceSteel.ConstructionTypes

Namespace MyNetJointInterface
  Public Class MyNetJoint
    Implements IRule

    Public Property JointId As ObjectId Implements IRule.JointId
      Get
        Throw New NotImplementedException()
      End Get
      Set(value As ObjectId)
        Throw New NotImplementedException()
      End Set
    End Property

    Public Sub Query(ui As IUI) Implements IRule.Query
      Throw New NotImplementedException()
    End Sub

    Public Sub CreateObjects() Implements IRule.CreateObjects
      Throw New NotImplementedException()
    End Sub

    Public Sub Save(filer As IFiler) Implements IRule.Save
      Throw New NotImplementedException()
    End Sub

    Public Sub Load(filer As IFiler) Implements IRule.Load
      Throw New NotImplementedException()
    End Sub

    Public Sub GetRulePages(builder As IRuleUIBuilder) Implements IRule.GetRulePages
      Throw New NotImplementedException()
    End Sub

    Public Function GetTableName() As String Implements IRule.GetTableName
      Throw New NotImplementedException()
    End Function
  End Class
End Namespace

 

 

 

I have attached the working C# implementation and the translated VB.NET code.

 

Accepted solutions (1)
802 Views
1 Reply
Reply (1)
Message 2 of 2

spayne76BBK
Advocate
Advocate
Accepted solution

I've managed to figure out the issue. Visual Studio handles namespaces a bit differently depending on the programming language you choose; in this case VB.NET and C#.

 

In C# the default namespace in a Visual Studio project determines the initial namespace added to the top of the file for each new item added to a project. It does not get appended to the front of the namespace during compile.

spayne76BBK_0-1678142241454.png

 

In VB.NET it's a different story. The Root namespace is added to every namespace in the project when it is compiled.

spayne76BBK_1-1678142301032.png

 

The class below will compile to MyNetJoint.MyNetJoint.MyTestJoint

 

 

 

Namespace MyNetJoint
  Public Class MyTestJoint ...
End Namespace

 

 

 

 Whereas the code below will compile to MyNetJoint.MyTestJoint

 

 

namespace MyNetJoint
{
    public class MyTestJoint ...
}

 

 

 

I managed to figure this out while I was trying to read the Common Intermediate Language (CIL) created by the two different languages. (VB.NET and C#)

 

I used ILSpy ( availble at GitHub and/or Visual Studio Marketplace) to read the CIL and noticed that the MyNetJoint written in C# had a difference namespace where the MyTestJoint class resided. (The first MyNetJoint in the list is the C# dll and the second MyNetJoint in the list is the VB.NET dll). After adjusting my VB.NET dll to have the same namespace as the C# dll, Advance Steel was able to run my .NET Joint.

spayne76BBK_3-1678143370949.png

 

I did some additional digging to see if it was best practice to omit the Root namespace from the project, or to leave the Root namespace as is and remove the namespace from each of the *.vb class files.  In the end, it seems that common practice is to leave the Root namespace blank in the application settings and to specify the namespace in each file. I believe this is standard practice in C#, (explicitly  defining namespaces), however, in VB.NET I can blissfully start writing code without having to define a namespace. Only to end up in this very predicament; chasing down nested namespaces. 😒

 

I have attached the updated code for anyone to use in the future.