class reuse btween console & winforms

A

andrewcw

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
 
T

Tobin Harris

andrewcw said:
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
 
S

seeni

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
 
A

andrewcw

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 application
with MessageBoxes.

Thanks,
-Srinivaasan, M




.
 

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