By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,876 Members | 1,357 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,876 IT Pros & Developers. It's quick & easy.

class reuse btween console & winforms

P: n/a
I have a class with a number of functions. For some form
of errors I wanted to use the MessageBox function of
Window.Forms.

I want to reuse this code in a console application, but
now I see I would have to remove all the MessageBox calls
for error messages and return the errors. Is there a way
to include that functionality in a console applic. I am
just doing the classes in console to save a bit of time.

Thanks Andrew
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"andrewcw" <an************@boeing.com> wrote in message news:13****************************@phx.gbl...
Is there a way
to include that functionality in a console applic. I am
just doing the classes in console to save a bit of time.
I've not tried this, but you could try something like....

// create an interface for error UI
public interface IErrorUI {
void ShowError( string err );
}

// create WinForms version
public class WinFormsErrUI: IErrorUI{
public void ShowError( string err ){
MessageBox.Show( "There was an error: " + err );
}
}

//create a Console Version
public class ConsoleErrUI : IErrorUI{
public void ShowError( string err ){
Console.Error.WriteLine( "There was an error: " + err );
}
}

// then, an example class
public class SomeClass{
public void DoSomething( IErrorUI errUI ){
try{
//some operation here that may throw exception
} catch( Exception e ) {
errUI.ShowError( e.Message );
}
}
}

Quite a lot of effort doing it this way, but it may buy you flexibility. You might want to look into the Abstract Factory pattern, which could be used to instantiate the correct type (Console/WinForms) objects depending on the "mode" you're running in.

Hope this helps!

Tobin Harris

Thanks Andrew

Nov 15 '05 #2

P: n/a
Just add a reference of System.Windows.Forms in your console application.
and add a using statement
using System.Windows.Forms;
in your console application code. Then you can debug the console application
with MessageBoxes.

Thanks,
-Srinivaasan, M

"andrewcw" <an************@boeing.com> wrote in message
news:13****************************@phx.gbl...
I have a class with a number of functions. For some form
of errors I wanted to use the MessageBox function of
Window.Forms.

I want to reuse this code in a console application, but
now I see I would have to remove all the MessageBox calls
for error messages and return the errors. Is there a way
to include that functionality in a console applic. I am
just doing the classes in console to save a bit of time.

Thanks Andrew

Nov 15 '05 #3

P: n/a
Thanks ! This is what I needed
-----Original Message-----
Just add a reference of System.Windows.Forms in your console application.and add a using statement
using System.Windows.Forms;
in your console application code. Then you can debug the console applicationwith MessageBoxes.

Thanks,
-Srinivaasan, M

"andrewcw" <an************@boeing.com> wrote in message
news:13****************************@phx.gbl...
I have a class with a number of functions. For some form
of errors I wanted to use the MessageBox function of
Window.Forms.

I want to reuse this code in a console application, but
now I see I would have to remove all the MessageBox calls for error messages and return the errors. Is there a way to include that functionality in a console applic. I am
just doing the classes in console to save a bit of time.

Thanks Andrew

.

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.