如何从 C# 中的方法返回等效的 VB6 Variant 类型 [英] How to return the equivalent of a VB6 Variant type from a method in C#

查看:18
本文介绍了如何从 C# 中的方法返回等效的 VB6 Variant 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧的 VB6 应用程序,它有一个函数 RunReturningVAR,它是一个可以返回 intstringdouble 的 db 调用..... 但不是 RecordSet.它的构建非常通用,因此可以由多个其他函数调用,因此我们没有多个位置用于 DB 调用.附上我目前拥有的.

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. 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 (不敢相信我找到了那个链接!)翻译 c# 动态.

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

它几乎可以翻译成object,但Variant是一种特定于后期绑定,它可以保存引用类型和值类型 - 不像 c# object 只能通过装箱来保存值类型.

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.

请注意,使用 dynamic 意味着您绕过所有编译时类型检查,这可能会导致您通常不会从 c# 程序中得到的运行时错误.

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 a c# program.

也许你可以用泛型做得更好,但这需要你指定调用方法的返回类型:

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
}

另外,对于公共类中的公共方法,我会强烈建议不要在 c# 中使用可选参数.
使用方法重载来指定默认值.原因是可选参数在 c# 中的工作方式:当调用带有可选参数的方法时,如果省略了可选参数,则将其默认值编译到方法调用中.因此,如果您从另一个程序集调用此方法,省略一些可选参数 - 如下所示:

Also, For a public method in a public class, I would strongly suggest against using optional parameters in 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. So if you call to this method from another assembly, omitting some of the optional parameters - like this:

yourObjectReference.RunReturningVar(sql, true);

c# 编译器实际上将其翻译为:

The c# compiler actually translate that to:

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);
}

等等.

这篇关于如何从 C# 中的方法返回等效的 VB6 Variant 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆