简体   繁体   English

如何从C#中的方法返回VB6 Variant类型的等效项

[英]How to return the equivalent of a VB6 Variant type from a method in C#

I have an older VB6 app that has a function RunReturningVAR it is a db call that could return an int , string , double ..... but not a RecordSet . 我有一个较旧的VB6应用程序,该应用程序具有功能RunReturningVAR,它是一个数据库调用,可以返回一个intstringdouble ......而不是RecordSet It was built very generically so that it can be called by multiple other functions so we don't have multiple locations for DB calls. 它的构建非常通用,因此可以由其他多个函数调用,因此我们没有用于数据库调用的多个位置。 what I currently have is attached. 我目前拥有的是附件。

Public Function RunReturningVar(ByVal vstrSql As String, _
Optional ByVal connval As Boolean = False, _
Optional ByVal copyID As Double = 0, _
Optional ByVal corpVal As Boolean = False, _
Optional ByVal vintConnectionTimeout As Integer = 30) As Variant

VB6 Variant (Can't believe I've found that link!) translates c# dynamic . VB6 Variant (不敢相信我找到了该链接!)翻译c# dynamic

public dynamic RunReturningVar(
    string vstrSql, 
    bool connval = false,
    double copyID = 0,
    bool corpVal = false,
    int vintConnectionTimeout = 30) 
{
    // do your stuff here 
}

It almost translates to object , but Variant is a data type specific for late-binding, and it can hold reference types and value types - unlike c# object that can hold value types only through boxing. 它几乎可以转换为object ,但是Variant是特定于后期绑定的数据类型,它可以保存引用类型和值类型-与c# object不同,后者只能通过装箱保存值类型。

Please note that working with dynamic means you bypass all the compile time type checks, which can result with run time errors you wouldn't normally expect from ac# program. 请注意, dynamic意味着您将绕过所有编译时类型检查,这可能会导致运行时错误,而这通常是ac#程序不会期望的。

Perhaps you could do better with generics, but that would require you to specify the return type from the calling method: 也许您可以使用泛型做得更好,但这需要您从调用方法中指定返回类型:

public T RunReturningVar<T>(
    string vstrSql, 
    bool connval = false,
    double copyID = 0,
    bool corpVal = false,
    int vintConnectionTimeout = 30) where T : new()
{
    // do your stuff here and return T
}

Also, For a public method in a public class, I would strongly suggest against using optional parameters in c#. 另外,对于公共类中的公共方法, 强烈建议不要在c# 中使用可选参数
Use method overloading to specify default values. 使用方法重载来指定默认值。 The reason for that is the way optional parameters works in c#: When a method with an optional parameter is being called, and the optional parameter is omitted, it's default value gets compiled into the method call. 原因是可选参数在c#中的工作方式:当调用带有可选参数的方法,而忽略可选参数时,其默认值将编译到方法调用中。 So if you call to this method from another assembly, omitting some of the optional parameters - like this: 因此,如果您从另一个程序集中调用此方法,则省略一些可选参数-如下所示:

yourObjectReference.RunReturningVar(sql, true);

The c# compiler actually translate that to: C#编译器实际上将其转换为:

yourObjectReference.RunReturningVar(sql, true, 0, false, 30);

This means that if you ever want to change the default value of any of the parameters, other assemblies referencing this one should also be recompiled. 这意味着,如果您想更改任何参数的默认值,则也应重新编译引用该参数的其他程序集。 Therefor, a better alternative is to use method overloading: 因此,更好的替代方法是使用方法重载:

public dynamic RunReturningVar(
    string vstrSql, 
    bool connval,
    double copyID,
    bool corpVal,
    int vintConnectionTimeout) 
{
    // do your stuff here 
}

public dynamic RunReturningVar(
    string vstrSql, 
    bool connval,
    double copyID,
    bool corpVal
    ) 
{
    return RunReturningVar(vstrSql, connval, copyID, corpVal, 30);
}

public dynamic RunReturningVar(
    string vstrSql, 
    bool connval,
    double copyID,
    ) 
{
    return RunReturningVar(vstrSql, connval, copyID, false);
}

and so on. 等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM