PC Review


Reply
Thread Tools Rate Thread

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

 
 
Erik Eckhardt
Guest
Posts: n/a
 
      8th Jul 2008
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
 
Reply With Quote
 
 
 
 
Ciaran O''Donnell
Guest
Posts: n/a
 
      9th Jul 2008
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" wrote:

> 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

 
Reply With Quote
 
Erik Eckhardt
Guest
Posts: n/a
 
      9th Jul 2008
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" wrote:

> 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" wrote:
>
> > 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

 
Reply With Quote
 
Ciaran O''Donnell
Guest
Posts: n/a
 
      10th Jul 2008
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" wrote:

> 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" wrote:
>
> > 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" wrote:
> >
> > > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Console.WriteLine AA2e72E Microsoft C# .NET 8 8th Feb 2010 06:14 AM
Console.WriteLine vs. Debug.WriteLine formatting Zytan Microsoft C# .NET 1 27th Feb 2007 02:24 AM
Console.Writeline hangs if user click into the console window Urs Eichmann Microsoft VB .NET 3 20th Jul 2004 06:48 AM
console.writeline portroe Microsoft VB .NET 5 6th Dec 2003 11:39 PM
Console.WriteLine Jason Carter Microsoft VB .NET 2 9th Sep 2003 10:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:42 PM.