1

I'm converting an old classic ASP method for data logging. One of the parameters could be an Array or a string. What type of variable do I declare? I tried declaring it as an object, and then testing the to see if it is an array. However, I cant seem to parse out the array contents (Should be a simple string array).

Here is a snippet of the classic ASP code:

               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

And here how I thought it might be converted to 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 is telling me that I can't get the length property or iterate through it. I suspect that even thought it might come in as an array, it is treated as an object. Any help would be appreciated.

1 Answer 1

5

"One of the parameters could be an Array or a string. What type of variable do I declare?"

params to the rescue!

Since details is the last argument in the method signature, you can define it as a params argument, which allows any number of items to be passed.

(Side note: in C#, method names are typically PascalCase, and arguments are typically 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} "));
}
Sign up to request clarification or add additional context in comments.

5 Comments

Since Ed's answer already covers it... maybe you should go for params instead?
Ah, when I first saw Ed's answer I didn't see the part about overloads! Thanks for the suggestion, @AlexeiLevenkov
So I am basically writing two similar methods, one for string and one for array, and C# will treat it like an overload and match to the correct parameter signature ?
No, you only have to write this one method. It handles either a string or an array. The params keyword specifies that the argument can take in one or more values, and they can be passes as an array or as separate values from the caller, then they are captured into an array in the method. See the params documentation for more info.
@RufusL I didn’t add the part about overloads until Alexei reminded me. Maybe Alexei should be getting all the internet points for both answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.