Add functionality to Console.WriteLine, sort of like defining a ma

E

Erik Eckhardt

I am trying to create a class or function which does one extra thing then
performs a normal Console.WriteLine, preferrably with all the overloads
available and intact.

Where I currently have:

Console.Write("\r
\r");
// note, the previous line is simply clearing any text on the current
console line without leaving that lnie.
Console.WriteLine("{0} {1} {2} {3} ({4})", new object[] {var1, var2, var3,
var4, var5});

I'd like to do

MyClass.OverwriteLine("{0} {1} {2} {3} ({4})", new object[] {var1, var2,
var3, var4, var5});

and get the same result, with all WriteLine overloads available for
OverwriteLine, and hopefully without having to duplicate them all.

Can anyone help me out with this?

I asked some friends and one idea was the following, and it is better than
the extra Console.Write each time, but I am still hoping for more:

static string SomeMethod(string toPad)
{
return "\r
\r" + toPad;
}

Console.WriteLine(SomeMethod("here is a string {0} : {1}"), new
object[]{"some", 5});

I'm using Framework 2.0.

Erik
 
C

Ciaran O''Donnell

There isnt any way to add a static method to the Console class but you can
easily make your own with passed through the calls to the Console class but
adds your prefix on to the string.
The suggestion of having a function to do that probably isnt terrible either
as you are really stuck for options with what you want to do as far as I know.
 
E

Erik Eckhardt

Thanks for the reply.

In order to do what you've suggested I'd have to put in code to handle every
override of Console.WriteLine, wouldn't I?

This seems like something that should be so easy...

macro OverwriteLine(params) {Console.Write("\n ... \n");
Console.WriteLine(params);}

or something like that :)

Do you have a suggestion as to the easiest/fastest way to get all the
redirecting of overrides in place? Anyway, the code complexity seems too much
for the slight benefit it would give. Oh well.

--

Ciaran O''Donnell said:
There isnt any way to add a static method to the Console class but you can
easily make your own with passed through the calls to the Console class but
adds your prefix on to the string.
The suggestion of having a function to do that probably isnt terrible either
as you are really stuck for options with what you want to do as far as I know.


--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


Erik Eckhardt said:
I am trying to create a class or function which does one extra thing then
performs a normal Console.WriteLine, preferrably with all the overloads
available and intact.

Where I currently have:

Console.Write("\r
\r");
// note, the previous line is simply clearing any text on the current
console line without leaving that lnie.
Console.WriteLine("{0} {1} {2} {3} ({4})", new object[] {var1, var2, var3,
var4, var5});

I'd like to do

MyClass.OverwriteLine("{0} {1} {2} {3} ({4})", new object[] {var1, var2,
var3, var4, var5});

and get the same result, with all WriteLine overloads available for
OverwriteLine, and hopefully without having to duplicate them all.

Can anyone help me out with this?

I asked some friends and one idea was the following, and it is better than
the extra Console.Write each time, but I am still hoping for more:

static string SomeMethod(string toPad)
{
return "\r
\r" + toPad;
}

Console.WriteLine(SomeMethod("here is a string {0} : {1}"), new
object[]{"some", 5});

I'm using Framework 2.0.

Erik
 
C

Ciaran O''Donnell

Its basic in terms of logic and idea but in OO practises it isnt something
that should be easy really. Console is an object with methods on it and to
change those you have to redefine them in some way. If they were instance
methods then you would need to inherit from Console and override them all.
But they are static so you need to wrap them up inside a new class with new
statics.
They may well be a way to get access to the Windows API to intercept writes
to the stdout stream but the writes are done 1 character at a time so you
wouldnt know when to inject the carriage return characters.
Would you really need to wrap ALL the write methods on console. perhaps you
could limit yourself to use using Writeline(sring) and Writeline(string,
params object[])





--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


Erik Eckhardt said:
Thanks for the reply.

In order to do what you've suggested I'd have to put in code to handle every
override of Console.WriteLine, wouldn't I?

This seems like something that should be so easy...

macro OverwriteLine(params) {Console.Write("\n ... \n");
Console.WriteLine(params);}

or something like that :)

Do you have a suggestion as to the easiest/fastest way to get all the
redirecting of overrides in place? Anyway, the code complexity seems too much
for the slight benefit it would give. Oh well.

--

Ciaran O''Donnell said:
There isnt any way to add a static method to the Console class but you can
easily make your own with passed through the calls to the Console class but
adds your prefix on to the string.
The suggestion of having a function to do that probably isnt terrible either
as you are really stuck for options with what you want to do as far as I know.


--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


Erik Eckhardt said:
I am trying to create a class or function which does one extra thing then
performs a normal Console.WriteLine, preferrably with all the overloads
available and intact.

Where I currently have:

Console.Write("\r
\r");
// note, the previous line is simply clearing any text on the current
console line without leaving that lnie.
Console.WriteLine("{0} {1} {2} {3} ({4})", new object[] {var1, var2, var3,
var4, var5});

I'd like to do

MyClass.OverwriteLine("{0} {1} {2} {3} ({4})", new object[] {var1, var2,
var3, var4, var5});

and get the same result, with all WriteLine overloads available for
OverwriteLine, and hopefully without having to duplicate them all.

Can anyone help me out with this?

I asked some friends and one idea was the following, and it is better than
the extra Console.Write each time, but I am still hoping for more:

static string SomeMethod(string toPad)
{
return "\r
\r" + toPad;
}

Console.WriteLine(SomeMethod("here is a string {0} : {1}"), new
object[]{"some", 5});

I'm using Framework 2.0.

Erik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top