I used a tool to convert C# to VB. When I run the program in VB I get the following error:
Events cannot be declared with a delegate type that has a return type.
How do I correct this code?
C#:
using System;
[assembly: CLSCompliant(true)]
namespace Link.API
{
public delegate decimal DecimalStringDelegate(string s);
public delegate long OrderDelegateStatus(Order o);
public delegate void LongDelegate(long val);
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Link.API
{
public interface TLServer
{
event LongDelegate newOrderCancelRequest;
event OrderDelegateStatus newSendOrderRequest;
string ClientName(int clientnum);
bool SymbolSubscribed(string sym);
Basket AllClientBasket { get; }
}
}
VB.NET:
Imports System.Text
Imports System.Collections.Generic
Imports System
<Assembly: CLSCompliant(True)>
Namespace Link.API
Public Delegate Function DecimalStringDelegate(ByVal s As String) As Decimal
Public Delegate Function OrderDelegateStatus(ByVal o As Order) As Long
Public Delegate Sub LongDelegate(ByVal val As Long)
End Namespace
Namespace Link.API
Public Interface Server
Event newOrderCancelRequest As LongDelegate
Event newSendOrderRequest As OrderDelegateStatus
Function ClientName(ByVal clientnum As Integer) As String
Function SymbolSubscribed(ByVal sym As String) As Boolean
ReadOnly Property AllClientBasket() As Basket
End Interface
End Namespace
Since,conversion of datatype is implicit type as 'int' is a subset of 'longtype' hence no need to explicitly convert data from one type to another. Compiler will automatically do conversion.
Type Casting is basically a process in C in which we change a variable belonging to one data type to another one. In type casting, the compiler automatically changes one data type to another one depending on what we want the program to do.
Also known as 'automatic type conversion'. Done by the compiler on its own, without any external trigger from the user. Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid loss of data.
In this case, the double value is automatically converted to integer value 34. This type of conversion is known as implicit type conversion. In C, there are two types of type conversion: Implicit Conversion.
As the error alludes to, VB does not support events that use a delegate with a return type. So you can't convert the code directly. One workaround would be to change the delegate to use a ByRef
parameter instead:
Public Delegate Sub DecimalStringDelegate(ByVal s As String, ByRef retVal as Decimal)
Public Delegate Sub OrderDelegateStatus(ByVal o As Order, ByRef retVal As Long)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With