【问题标题】:Creating a method in which one of the parameters could be an array or a string创建一个方法,其中一个参数可以是数组或字符串
【发布时间】:2019-09-20 20:13:29
【问题描述】:

我正在转换旧的经典 ASP 方法来记录数据。其中一个参数可以是数组或字符串。我声明什么类型的变量?我尝试将它声明为一个对象,然后测试它是否是一个数组。但是,我似乎无法解析出数组内容(应该是一个简单的字符串数组)。

这是经典 ASP 代码的 sn-p:

               Public function security(u,a,d)
' -------------------------------------------------------
' Write to tbl_log_security, returning a 1-Pass or 0-Fail
' -------------------------------------------------------
    u = u + 0
    af = u
    if len(request.Cookies("userid")) > 0 then af = request.Cookies("userid")
    security =                                      1 ' Success
    Dim objCommandLog
    Set objCommandLog =                                Server.CreateObject("ADODB.Connection")   
    objCommandLog.open =                               application("connVRVprimary")
    Err.Clear
    if isarray(d) then
        for I = 0 to ubound(d)
            strDetails = strDetails &               chr(13) & "Detail " & I & ": " & d(i) & " "
        next
    elseif len(d) > 0 then : strDetails = d & " "
    end if

在这里我认为它可能会转换为 C#。

public static bool security(string UserID, string Action, object Details )
{ 
    // Apparently, Details can be a string or an array !
    // UserID is passed in as a string, we need to convert it to an int
    Int32 iUserID; //af
    string strDetails;

    if (!Int32.TryParse(UserID, out iUserID))
    {
        //If it doesn't convert, then use the UserID from Application Object
        iUserID = Convert.ToInt32(ApplicationObject.USERID);
    }

    Type valueType = Details.GetType();
    if (valueType.IsArray)
    {
    for(int i = 0, i < Details.Length; i++)
        {
            strDetails += "Detail " + i + ": " + Details(i);
        }
    }
    else
    { // is string
        strDetails = Details;
    }

Intellisense 告诉我,我无法获取长度属性或迭代它。我怀疑即使认为它可能以数组的形式出现,它也被视为对象。 任何帮助将不胜感激。

【问题讨论】:

    标签: c# methods parameters asp-classic


    【解决方案1】:

    “其中一个参数可以是数组或字符串。我声明什么类型的变量?”

    params 来救援!

    由于details 是方法签名中的最后一个参数,您可以将其定义为params 参数,它允许传递任意数量的项目。

    (旁注:在 C# 中,方法名称通常为 PascalCase,参数通常为 camelCase

    public static bool Security(string userID, string action, params string[] details )
    {
        // Other code omitted for brevity
    
        var strDetails = details == null ? " " 
            : details.Length == 1 ? $"{details[0]} "
            : string.Join(Environment.NewLine,
                details.Select((detail, index) => $"Detail {index}: {detail} "));
    }
    

    【讨论】:

    • 既然 Ed 的回答已经涵盖了它......也许你应该改用params
    • 啊,当我第一次看到 Ed 的回答时,我没有看到关于重载的部分!感谢您的建议,@AlexeiLevenkov
    • 所以我基本上写了两个类似的方法,一个用于字符串,一个用于数组,而C#会将其视为重载并匹配正确的参数签名?
    • 不,你只需要写这一个方法。它处理字符串或数组。 params 关键字指定参数可以接受一个或多个值,它们可以作为数组传递,也可以作为来自调用者的单独值传递,然后将它们捕获到方法中的数组中。请参阅params documentation 了解更多信息。
    • @RufusL 在 Alexei 提醒我之前,我没有添加关于重载的部分。也许阿列克谢应该为这两个答案获得所有的互联网积分。
    猜你喜欢
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 2013-05-17
    相关资源
    最近更新 更多