0

I would like to use a vb.net module in my c# project. I compiled the vb.net module and I added reference to its dll in c# project, but I can't see it in c# project. The module is public, I tried to write its namespace (dll file name) before the module name, I tried to manually insert using moduleNameSpace, I checked that both projects have the same destination framework (4.5 without client profile), but I still can't see it. I have no idea about what I should do further. Can anyone help me? Thanks!

A portion of vb.net module:

Namespace GestioneSetupFile
Public Module GestioneSetupFiles
   Private Const TAB_POS As Short = 30
   Dim Testo, Paragrafo As String
   Dim PtrParagrafo As Integer

   Public DEFAULT_APP_DIR As String = My.Application.Info.DirectoryPath
   'Public DEFAULT_APP_DIR As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\BTSR\PC-LINK NEMO"

    Public DEFAULT_CONFIG_DIR As String = DEFAULT_APP_DIR + "\Config"

    Public Sub CheckConfigDir()
    'Verifica la presenza della cartella Config
    'Se non esiste la crea e per compatibilità con le versioni precedenti ci copia dentro tutti i files .cfg e .dat che trova
    If Not My.Computer.FileSystem.DirectoryExists(DEFAULT_CONFIG_DIR) Then
        'Dim NomeFile As String
        My.Computer.FileSystem.CreateDirectory(DEFAULT_CONFIG_DIR)
        'NomeFile = Dir(DEFAULT_APP_DIR + "\*.cfg")
        'While NomeFile <> ""
        '    'My.Computer.FileSystem.MoveFile(DEFAULT_APP_DIR + "\" + NomeFile, DEFAULT_CONFIG_DIR + "\" + NomeFile)
        '    'per compatibilità con vecchio (se reinstallato) non sposta ma copia i file
        '    My.Computer.FileSystem.CopyFile(DEFAULT_APP_DIR + "\" + NomeFile, DEFAULT_CONFIG_DIR + "\" + NomeFile)
        '    NomeFile = Dir()
        'End While
        'NomeFile = Dir(DEFAULT_APP_DIR + "\*.dat")
        'While NomeFile <> ""
        '    'My.Computer.FileSystem.MoveFile(DEFAULT_APP_DIR + "\" + NomeFile, DEFAULT_CONFIG_DIR + "\" + NomeFile)
        '    'per compatibilità con vecchio (se reinstallato) non sposta ma copia i file
        '    My.Computer.FileSystem.CopyFile(DEFAULT_APP_DIR + "\" + NomeFile, DEFAULT_CONFIG_DIR + "\" + NomeFile)
        '    NomeFile = Dir()
        'End While
    End If
End Sub
End Module
End Namespace

And this is my call in c# project:

using System;
/*Others using*/
using Microsoft.VisualBasic;
using GestioneSetupFile; //Compile error

namespace UltraFeederControl
{
   public partial class Form1 : Form
   {
       private void updateConfigFile()
       {
           GestioneSetupFiles.CheckConfigDir();
       }
   }
}
35
  • 2
    why can't you take the code from the vb.Module and convert it to it's C# equivalent and create a Class in C# or Extension Method that emulates the same functionality.. if you are not sure how to do that.. then post the Modules code signature and it's functionality and one of use could point you in the right direction in regards to converting the code.. Commented Jan 8, 2015 at 21:22
  • 1
    Without a (minimal) definition of your VB module, your attempted usage in C#, and the compiler error you're receiving, no one is going to be able to give you a direct answer. Commented Jan 8, 2015 at 21:25
  • 3
    @MethodMan referencing a .net assembly written in VB.NET should work fine in C# without having modification to the C# project. Once the VB.NET (or C#) source is compiled, any .NET project should be able to reference it. Keeping in mind things like runtime versions, etc. Commented Jan 8, 2015 at 21:37
  • 1
    @MethodMan Yes, I'm doing it, even if I think it's not necessary to add Microsoft.VisualBasic Commented Jan 8, 2015 at 21:39
  • 1
    I would never name my namespace close to the Method name that's way too condfusing and opens up the door for issues like you're seeing currently Vitto Commented Jan 8, 2015 at 22:20

2 Answers 2

1

here is the C# converted code that you need.. make sure if you add this to a Class that you put the

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;

namespace GestioneSetupFile
{
    public static class GestioneSetupFiles
    {
        private const short TAB_POS = 30;
        static string Testo;
        static string Paragrafo;

        static int PtrParagrafo;
        public static string DEFAULT_APP_DIR = My.Application.Info.DirectoryPath;

        public static string DEFAULT_APP_DIR = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\BTSR\\PC-LINK NEMO";

        public static string DEFAULT_CONFIG_DIR = DEFAULT_APP_DIR + "\\Config";
        public static void CheckConfigDir()
        {

            if (!My.Computer.FileSystem.DirectoryExists(DEFAULT_CONFIG_DIR)) 
            {
                string NomeFile = null;
                My.Computer.FileSystem.CreateDirectory(DEFAULT_CONFIG_DIR);
                NomeFile = FileSystem.Dir(DEFAULT_APP_DIR + "\\*.cfg");
                while (!string.IsNullOrEmpty(NomeFile)) 
                {
                    My.Computer.FileSystem.MoveFile(DEFAULT_APP_DIR + "\\" + NomeFile, DEFAULT_CONFIG_DIR + "\\" + NomeFile);
                    My.Computer.FileSystem.CopyFile(DEFAULT_APP_DIR + "\\" + NomeFile, DEFAULT_CONFIG_DIR + "\\" + NomeFile);
                    NomeFile = FileSystem.Dir();
                }
                NomeFile = FileSystem.Dir(DEFAULT_APP_DIR + "\\*.dat");
                while (!string.IsNullOrEmpty(NomeFile)) 
                {
                    My.Computer.FileSystem.MoveFile(DEFAULT_APP_DIR + "\\" + NomeFile, DEFAULT_CONFIG_DIR + "\\" + NomeFile);
                    My.Computer.FileSystem.CopyFile(DEFAULT_APP_DIR + "\\" + NomeFile, DEFAULT_CONFIG_DIR + "\\" + NomeFile);
                    NomeFile = FileSystem.Dir();
                }
            }
        }
    }
}

To see Hot Root NameSpace Work in VB vs C#.NET take a look here and you will see how to fix your problem otherwise the converted portion is what I have started for you is here..

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, but I posted only a portion of module, and however I would like to comprend why I can't simply include it
perhaps you need to fully qualify the namespace I would love to see what the namespace of the C# project looks like.. then name your Visualbasic one to be the same as well or to access it you need to do it but it's fully qualified namespace does that make sense
Vito you can look at this link as well VB.NET Namespave
There are times where it just isn't practical to convert old code. Yes, it would be nice to start a project to rewrite everything, but if you have multiple clients using code that uses a library that has the module in it, you would want to be able to reference it from new C# code in the mean time rather than having duplicate code, and hopefully in a way that will allow you to rewrite that module code later without having to change the new C# code. There's nothing wrong with this question in that regard.
0

There may be a better way to do it, but in my case the module in question was part of a VB project that also had a class library that I could reference. I just created a method in that library that called the function in the module. This gave me access and I didn't need to duplicate code. When we rewrite it, I should not need to change my C# code. It was a little different because I was accessing a function within the module, but here I THINK it would look like this, but you get the idea:

Public Class MyLibrary
    Public Shared Sub GestioneSetupFiles()
        GestioneSetupFile.GestioneSetupFiles()
    End Sub
End Class

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.