3

I have a function that accepts a string array as a parameter

public static void LogMethodStart(string[] parameters)
{
    AddLogEntry("Starting Method", parameters); //this does not work
}

public static void AddLogEntry(string[] parameters)
{ 
    using(StreamWriter sw = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), FileName), true))
    {
        foreach (string p in parameters)
        {
            stream.WriteLine(p.ToString() + DateTime.Now.ToString());
        }
    }
}

Is there a way to pass an element to be included as an Array without having to do some Array.resize() operation and check for null etc...?

5
  • Sounds like you might want the params keyword. stackoverflow.com/q/7580277/97382 Commented Jun 10, 2015 at 22:55
  • nope, it gives an error saying "Can't convert string[] to string" ... probably because the params works on individual objects, not collections/lists/arrays Commented Jun 10, 2015 at 23:00
  • You can't just slap params on your existing code without making a few changes. Your description of what you're trying to accomplish is pretty vague so it's tough to be any more specific than that, but bottom line is that if you want to pass a variable number of arguments (of the same type) to a method the params is the way to go. It would also be helpful if you explained what your this does not work comment means. Just a quick glance seems like it should be fine. Commented Jun 10, 2015 at 23:04
  • @CraigW. What I'm trying to get at is how to call AddLogEntry() inside LogMethodStart(). notice that essentially I'm trying to prepend the array that will be passed into AddLogEntry()... I am not passing in arguments of the same type, one is an array, one is an object of the type of the array... run my code and see you get the compile error I mention Commented Jun 10, 2015 at 23:13
  • @CraigW. params don't help to remove null checks as one can still use original syntax to pass null ( Foo((string[])null) ) Commented Jun 10, 2015 at 23:35

1 Answer 1

9

Change your method signature to this:

public static void LogMethodStart(params string[] parameters)
{
    AddLogEntry("Starting Method", parameters); //this does not work
}

Then you can call it in a couple of ways:

LogMethodStart("string1","string2");
LogMethodStart(new string[] {"string1","string2"});

Both of these will look the same inside the method.

EDIT:

Modify your LogMethodStart body:

var newParams = new List<string>(parameters);
newParams.Insert(0,"Starting Method");
AddLogEntry(newParams.ToArray());
Sign up to request clarification or add additional context in comments.

1 Comment

And LogMethodStart((string[])null); to keep author of LogMethodStart awake.

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.