Reading/Calling String variable from .dll file

IP 41 Reputation points
2022-04-21T15:50:30.52+00:00

Hi there,

I have recently created a New.dll file using VS 2022 in C# for later implementation into my VB6 program - I have recently taken over software development!

For the meantime, I have created a test software in VS22 C# to test if the New.dll file works.
So far so good, I have added Reference from my test software to the New.dll and added "using [namespace]" to my test program.

Issue: I need to read a string variable from the New.dll file and display it on a textbox on my test software.
How can I read a string variable which is used in New.dll and display on my test software?

FYI - Here is the function where the string variable is processed on my New.dll file - m_sDataSent is the string variable.

public void SendData(string str)
{
if (connection == true)
{
if (Comms != null)
Comms(this, m_sDataSent);
}
}

Many Thanks!

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,307 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2022-04-22T05:58:24.347+00:00

    @IP , as Bruce said, we could set public property and public method to get the value from the dll.

    I make a code example for you and hope it could help you.

    New.dll:(.NET Framework Class Library)

    namespace New  
    {  
        public class Example  
        {  
            public string Getvalue { get; set; }  
            public void SendData()  
            {  
      
                if(condition)  
                {  
                    string m_sDataSent = "Hello";  
                    Getvalue = m_sDataSent;  
                }  
            }  
        }  
    }  
    

    Winform project:

    private void button1_Click(object sender, EventArgs e)  
            {  
                Example example = new Example();  
                example.SendData();  
                textBox1.Text = example.Getvalue;  
            }  
    

    After clicking the button, the textbox will show "Hello" text.

    195370-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,931 Reputation points
    2022-04-21T17:56:34.65+00:00

    In c#, external modules can only access public strings. You would need to make the string public, or supply a public method to set it.


  2. RLWA32 40,771 Reputation points
    2022-04-22T08:41:43.797+00:00

    I have recently created a New.dll file using VS 2022 in C# for later implementation into my VB6 program...

    You should be aware that a VB6 application cannot directly use a .net class library (DLL) unless the class library exposes its classes and methods through COM. There are many available references about how to do this. One starting point is COM Wrappers. Additionally, since VB6 applications are 32-bit you need to take care that the COM registration for your .net class library is made in the 32-bit view of the registry.