How to create a function like WriteLine which can accept an unspecified number of parameters?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I'm new to C# programming and not sure how to do this, but basically, I
want to create a function exactly like how System.Console.WriteLine()
works.

eg, if I do:
System.Console.WriteLine("A = " + valA + " B = " + valB);
it displays all of the above together.

I want to create my own function which will work like:
SystemLog(??? xyz)
{
....
StreamWriter sw = ...;
System.Console.WriteLine(xyz);
sw.WriteLine(xyz);
....
}

How can I do this?

Thanks!
 
What you had writted is basically what you want, but here's how I'd do it.

/////////////////////////

using System;
using System.IO;

....

public static void LogSomething(string logEntry) {

string logFilePath = @"C:\log.txt";

FileStream fs = new FileStream(logfilePath, FileMode.Append);
StreamWriter sw = new StreamWriter(fs);

sw.WriteLine(logEntry);

sw.Close();
fs.Close();

}

// you then call that function like you wish:

LogSomething("this is" + " " + "a log line. " + valA);

// the end.
/////////////////////////////////////////

don't worry about opening the file and closing the file over and over
again, it introduces only about 200ms of latency per 1000 opens & closes.
 
Ah thanks! The mistake I was making was that I was not using:

public static void LogSomething(...) ...

It works fine now!

Thanks!
 
Back
Top