Click here to Skip to main content
14,635,966 members
Rate this:
Please Sign up or sign in to vote.
Hi all,

I've written a long time ago a long Visual Basic project, where a class has several properties with multiple arguments.

Now I should implement that project in an existing solution written in C#. The only way I found to call these property getter/setter is through reflection.
Do you know another way?

What I have tried:

VB
Public Class Manager
    Public Property Message() As String
        Get
            Return Message(Nothing, Nothing)
        End Get
        Set(value as String)
            Message(Nothing, Nothing) = value
        End Set
    End Property
    Public Property Message(useSQLInfo As Boolean?) As String
        Get
            Return Message(useSQLInfo, Nothing)
        End Get
        Set(value as String)
            Message(useSQLInfo, Nothing) = value
        End Set
    End Property
    Public Property Message(useSQLInfo As Boolean?, manageRN As Boolean?) As String
        Get
            [...]
        End Get
        Set(value as String)
            [...]
        End Set
    End Property
End Class


C#
class Foo {
    Manager manager = CreateManager();
    [...]
    void SaveExchange([...]) {
        if (manager != null) {
            string managerMessage = ManagerGetMessage(manager, true, null);
            [...]
        }
    }
    private static MethodInfo mgmMethodInfo = null;
    string ManagerGetMessage(Manager manager, bool? useSQLInfo, bool? manageRN) {
        if (mgmMethodInfo == null)
            mgmMethodInfo = manager.GetType().GetProperty("Message", typeof(string),
                    new Type[] { typeof(bool?), typeof(bool?) } ).GetGetMethod();
        return mgmMethodInfo.Invoke(manager, new object[] { useSQLInfo, manageRN} );
    }
}


If anyone knows another method is well accepted.

Thank you
Lucio
Posted
Updated 25-Jul-17 0:27am
Comments
Graeme_Grant 25-Jul-17 6:18am
   
Are you trying to use both VB & C# in the same compiled DLL/EXE or are each in their own DLL/EXE?
Menci Lucio 25-Jul-17 6:40am
   
They are two different projects that create different dll/exe in the same solution (for debugging). I was able to call all methods and other properties of Manager inside the C# project.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

If the VB code is in a class library, then you can reference and use it like you can a C# class library.

Here is a test to show how this works:

1. Add a C# Console app to a solution
2. Add a VB class library to the solution
3. reference the VB class Library in the C# console app
4. use the following code:

VB Class Library
Public Class Class1

    Public Function GetString() As String
        Return "Hello from VB!"
    End Function

End Class

C# Console app
namespace ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var vbClass = new ClassLibrary1.Class1();

            Console.WriteLine(vbClass.GetString());
            Console.ReadKey();
        }
    }
}

5. Now compile and run and it will work. :)

** Property Test update

VB Class Library
Public Class Class1

    Public Function GetString() As String
        Return "Hello from VB!"
    End Function

    Private test As String
    Public Property TestProperty() As String
        Get
            Return test
        End Get
        Set(ByVal value As String)
            test = value
        End Set
    End Property

End Class

C# Console app
using System;

namespace ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var vbClass = new ClassLibrary1.Class1();

            // Test 1
            Console.WriteLine(vbClass.GetString());

            // Test 2
            vbClass.TestProperty = "This is a VB property test";
            Console.WriteLine(vbClass.TestProperty);

            Console.ReadKey();
        }
    }
}

Output
Hello from VB!
This is a VB property test


** Update 2: Accessing VB Property with paramater
Intellisense had the answer for you. See below:

VB Class Library
Imports System.Runtime.CompilerServices

Public Class Class1

    Public Function GetString() As String
        Return "Hello from VB!"
    End Function

    Private test As String
    Public Property TestProperty() As String
        Get
            Return test
        End Get
        Set(ByVal value As String)
            test = value
        End Set
    End Property

    Public Property Message() As String
        Get
            Return Message(Nothing, Nothing)
        End Get
        Set(value As String)
            Message(Nothing, Nothing) = value
        End Set
    End Property
    Public Property Message(useSQLInfo As Boolean?) As String
        Get
            Return Message(useSQLInfo, Nothing)
        End Get
        Set(value As String)
            Message(useSQLInfo, Nothing) = value
        End Set
    End Property

    Private flag1 As Boolean?
    Private flag2 As Boolean?
    Private messageText As String

    Public Property Message(useSQLInfo As Boolean?, manageRN As Boolean?) As String
        Get
            Return messageText
        End Get
        Set(value As String)
            messageText = value
        End Set
    End Property

End Class

C# Console app
using System;

namespace ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var vbClass = new ClassLibrary1.Class1();

            // Test 1
            Console.WriteLine(vbClass.GetString());

            // Test 2
            vbClass.TestProperty = "This is a VB property test";
            Console.WriteLine(vbClass.TestProperty);

            // test 3
            vbClass.set_Message(true, "test message");
            Console.WriteLine(vbClass.get_Message(true));

            Console.ReadKey();
        }
    }
}
   
v3
Comments
Menci Lucio 25-Jul-17 6:42am
   
This is my start point. My problem is that I had to use reflection in C# uniquely to raise property getter/setter with arguments written in VB
Graeme_Grant 25-Jul-17 6:48am
   
See the update above - works as expected.
Menci Lucio 25-Jul-17 6:51am
   
TestProperty has not arguments. See the

Public Property Message(useSQLInfo As Boolean?, manageRN As Boolean?) As String

in my source into the first post
Graeme_Grant 25-Jul-17 7:02am
   
See update 2, test 3! Special wrapper functions are made available to C#! :)
Menci Lucio 25-Jul-17 9:09am
   
The VB class is so big that I did not see the intellisense suggestion...
Thank you
Graeme_Grant 25-Jul-17 14:19pm
   
You are welcome :)
BillWoodruff 25-Jul-17 14:30pm
   
+5 Excellent
Graeme_Grant 25-Jul-17 14:58pm
   
Thanks Bill :)
Karthik_Mahalingam 26-Jul-17 1:14am
   
5
Graeme_Grant 26-Jul-17 4:38am
   
Thanks! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100