Author Topic: Passing multiple variables from VB.NET to C#  (Read 4282 times)

0 Members and 1 Guest are viewing this topic.

gablackburn

  • Guest
Passing multiple variables from VB.NET to C#
« on: September 17, 2012, 05:21:12 PM »
Long story short, a guy at my office wrote a complex routine in C++ which does a lot of calculations with 3D coordinates.  Another guy wrote a routine in C# to access the C++ .DLL and return some results.  Since VB.NET cannot use UNSAFE, I can't access the C++ .DLL.  However, C# can.  I'd like to be able to call the C# Class Library from VB.NET but I'm having problems getting C# to return more than one variable.

I've written some simplified code below, but if I can get this to work, then I'll be able to get his code to work.

To start off, I'm not all that familiar with C#, so take it easy on me.  This is the C# code I wrote to do the calculation...
Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nsClassLibrary
{
    public class VBSamples
    {
        public double RetOneDouble(double GetDouble)
        {
            double dbl = GetDouble + 11;
            return dbl;
        }
    }
}

In VB.NET, after referencing the C# .DLL, I can use this code to call the C# Class Library to return one variable .

Code: [Select]
Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim obj As New nsClassLibrary.VBSamples
        MessageBox.Show(obj.RetOneDouble(22))
        Me.Close()
    End Sub
End Class


The problem comes when I pass two or more doubles/variables, C# it handles it fine, but it cannot return both values back to VB.



So I feel like a STRUCT may work for me, I just can't get it to work I'm obviously doing something wrong when setting this up, again, I'm not familiar with C#.
Code: [Select]
    struct struTwoDoubles
    {
        public double GetDbl1, GetDbl2;
    }

But I haven't had any luck with it...

Code: [Select]
        private struTwoDoubles foo(double dbl1, double dbl2)
        {
            struTwoDoubles abc = new struTwoDoubles();
            abc.GetDbl1 = dbl1 * 2;
            abc.GetDbl2 = dbl2 * 3;
            return (abc.GetDbl1,abc.GetDbl2);
        }

I'm not sure what else to try here and I feel I've done a lot of Googling in the last few days, but this isn't addressed much.

Any input will be helpful, thanks in advance.

GAB

BlackBox

  • King Gator
  • Posts: 3770
Re: Passing multiple variables from VB.NET to C#
« Reply #1 on: September 17, 2012, 05:35:40 PM »
Just a guess, but wouldn't an Overload (same Method, different parameters and return) work?  :?


Pseudo code:
Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Foo
  7. {
  8.     public class Functions
  9.     {
  10.         public static double myFunction(double num)
  11.         {
  12.             double dbl = num + 11;
  13.             return dbl;
  14.         }
  15.  
  16.         public static List<double> myFunction(double num1, double num2)
  17.         {
  18.             double dbl1 = num1 * 2;
  19.             double dbl2 = num2 * 2;
  20.             List<double> result = new List<double>;
  21.             result.Add(dbl1);
  22.             result.Add(dbl2);
  23.             return result;
  24.         }
  25.     }
  26.  
  27.     public class Samples
  28.     {
  29.         public void Foo()
  30.         {
  31.             List<double> foo = Functions.myFunction(22, 10);
  32.         }
  33.     }
  34. }
  35.  
"How we think determines what we do, and what we do determines what we get."

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Passing multiple variables from VB.NET to C#
« Reply #2 on: September 17, 2012, 07:10:44 PM »
< .. >
The problem comes when I pass two or more doubles/variables, C# it handles it fine, but it cannot return both values back to VB.

< .. >

c# and VB.Net will only return one output value.
Some options to handle multiple values include ;

 •Use resultbuffer
 •Use output parameters (using the out or ref keywords)
 •Return an array
 •Return a structure that contains multiple members
 •Return an instance of a class
 •Use a dictionary or key-value pair or tuple as output
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: Passing multiple variables from VB.NET to C#
« Reply #3 on: September 17, 2012, 08:29:42 PM »
how about pasing the arguments by reference?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Passing multiple variables from VB.NET to C#
« Reply #4 on: September 17, 2012, 10:16:04 PM »
how about pasing the arguments by reference?

Yep :)

•Use output parameters (using the out or ref keywords)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Passing multiple variables from VB.NET to C#
« Reply #5 on: September 17, 2012, 11:05:40 PM »
But I haven't had any luck with it...

Code: [Select]
        private struTwoDoubles foo(double dbl1, double dbl2)
        {
            struTwoDoubles abc = new struTwoDoubles();
            abc.GetDbl1 = dbl1 * 2;
            abc.GetDbl2 = dbl2 * 3;
            return (abc.GetDbl1,abc.GetDbl2);
        }


You probably will not have much luck trying to return 2 doubles when the function signature defines returning a struTwoDoubles type.
 
Also if your trying to call the function from another assembly it will need to be visible. Your function is marked as private.
 
 
A little console app how to return your struct
Code - C#: [Select]
  1.  
  2.      class Program
  3.     {
  4.         static void Main(string[] args)
  5.         {
  6.             struTwoDoubles stw = foo(56.5, 45.6);
  7.             Console.WriteLine(stw.GetDbl1);
  8.             Console.WriteLine(stw.GetDbl2);
  9.             Console.ReadLine();
  10.         }
  11.  
  12.         private static struTwoDoubles foo(double dbl1, double dbl2)
  13.         {
  14.             struTwoDoubles abc = new struTwoDoubles();
  15.             abc.GetDbl1 = dbl1 * 2;
  16.             abc.GetDbl2 = dbl2 * 3;
  17.             return abc;
  18.         }
  19.     }
  20.     struct struTwoDoubles
  21.     {
  22.         public double GetDbl1, GetDbl2;
  23.     }
  24. }
  25.  

gablackburn

  • Guest
Re: Passing multiple variables from VB.NET to C#
« Reply #6 on: September 18, 2012, 12:55:33 PM »
Thanks everyone who commented.  I got some help from the C# guy and he was able to modify his code. 

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: Passing multiple variables from VB.NET to C#
« Reply #7 on: September 18, 2012, 08:48:47 PM »
how about pasing the arguments by reference?

Yep :)

•Use output parameters (using the out or ref keywords)

DOH!  :oops:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Passing multiple variables from VB.NET to C#
« Reply #8 on: September 18, 2012, 09:01:25 PM »
It's cool Daniel :)

My rule say we're allowed 5 oopsies a day ( accumulative).

ie
Code - C#: [Select]
  1. var days     = 51;
  2. var oopsies  = 254;
  3. var proceed  = oopsies % (days * 5) > 0;
  4.  
  5. print (proceed);
  6.  
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Passing multiple variables from VB.NET to C#
« Reply #9 on: September 18, 2012, 11:01:19 PM »
The proper ways to return multiple values from any function were covered in Kerry's first post.

If you examine exported functions, for example in the windows API, reference variables and structs tend to be the most common method to get alot of information from a single function, although there are many functions that return bitwise values allowing more than one bit of information (predefined information) to be transmitted between functions.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Passing multiple variables from VB.NET to C#
« Reply #10 on: September 19, 2012, 12:07:44 AM »
Hi,

Another way if you target the Framework 4, is to return a Tuple.
Speaking English as a French Frog

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Passing multiple variables from VB.NET to C#
« Reply #11 on: September 19, 2012, 12:26:33 AM »
Hi,

Another way if you target the Framework 4, is to return a Tuple.

•Use a dictionary or key-value pair or tuple as output

DOH! #2