C # Decimal value for VB6 currency

I have one C # DLL and one Visual Basic 6 DLL.

In C #, there is an x ​​field with a Decimal data type. VB6 has a y field with the data type Currency .

What would be the best way to pass x to y and vice versa?

I am currently converting fields to Double , but I'm not sure if there are consequences of rounding.

Update 1:

Based on a helpful tip, this is my code as follows:

public void FromVbToNet(long vb6curr) { decimal value = vb6curr / 10000; } 

The problem is that when I try to call this from VB6 via interop, I get a compilation error:

"A function or interface that is marked as restricted, or the function uses an automation type that is not supported in Visual Basic."

So how do I declare vb6curr? String , Object , Dynamic ?

Update 2:

If someone needs this for reference, after further reading, I came up with the following solution:

 [return: MarshalAs(UnmanagedType.Currency)] public decimal GetDecimalFromNetDll() { decimal value = ... // Read from database return value; } public void SetDecimalInNetDll([MarshalAs(UnmanagedType.Currency)] decimal value) { // Save to database } 

I call this from my unmanaged code in VB6 with the Currency parameter, and everything seems to be working so far.

+5
source share
2 answers

After some reading, I came up with this solution (see also the "Update 2" section).

I had to marshal the Decimal type in .Net for the Currency type in unmanaged VB6 code and vice versa.

 [return: MarshalAs(UnmanagedType.Currency)] public decimal GetDecimalFromNetDll() { decimal value = ... // Read from database return value; } public void SetDecimalInNetDll([MarshalAs(UnmanagedType.Currency)] decimal value) { // Save to database } 

For more information, see: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute%28v=vs.110%29.aspx

+7
source

The VB6 Currency data type is stored as an integer of 64 bits, implicitly scaled to 10,000 . Armed with this knowledge, it is easy to convert this type to .net Decimal .

On the VB6 side, you are passing data as Currency . On the C # side, you pass it as long . Then, on the C # side, you scale 10,000 to convert between your Decimal value and long value.

For example, if you have a VB6 Currency value stored in C # long, you convert to Decimal as follows:

 long vb6curr = ...; decimal value = vb6curr / 10000; 

In another direction, it will be:

 decimal value = ...; long vb6curr = Convert.ToInt64(value*10000); 
+9
source

Source: https://habr.com/ru/post/1209315/


All Articles