balexandre
balexandre

Reputation: 75103

How to reuse the same variable, but for different types?

I have a little problems understand what's going on behind the scenes of the "type T" to get this right, I'm hopping that some of you can show me a light at the end of the tunnel :)

I have a COM object that I assign almost the some things (properties) but I need to use this for all objects, and I want to do this once and only that will work with all types.

Printer type:

switch (type)
{
    case convert2pdf.ConvertFileType.Word: 
        WordPrintJob oPrintJob = null; break;
    case convert2pdf.ConvertFileType.Excel: 
        ExcelPrintJob oPrintJob = null; break;
    case convert2pdf.ConvertFileType.PowerPoint: 
        PowerPointPrintJob oPrintJob = null; break;
    case convert2pdf.ConvertFileType.IE: 
        IEPrintJob oPrintJob = null; break;
    case convert2pdf.ConvertFileType.Publisher: 
        PublisherPrintJob oPrintJob = null; break;
    case convert2pdf.ConvertFileType.Visio: 
        VisioPrintJob oPrintJob = null; break;

    default: 
        GenericPrintJob oPrintJob = null; break;
}

and then, no matter what my object type that I created, I implement every time this:

PDFSetting oPDFSetting = null;

oPrintJob = oPrinter.GenericPrintJob;
oPDFSetting = oPrintJob.PDFSetting;

/*put water mark on the first page, set the water mark text to "BCL EasyPDF */
oPDFSetting.set_Watermark(0, true);
oPDFSetting.set_WatermarkColor(0, (uint)System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue));
oPDFSetting.set_WatermarkFirstPageOnly(0, true);
oPDFSetting.set_WatermarkText(0, "EasyPDF");

/*set the meta data for the pdf file*/
oPDFSetting.MetaData = true;
oPDFSetting.MetaDataAuthor = "Your Name";
oPDFSetting.MetaDataCreator = "BCL";
oPDFSetting.MetaDataKeywords = "PDF";
oPDFSetting.MetaDataSubject = "Converter";
oPDFSetting.MetaDataTitle = "easyPDF SDK";

How do I use the "type T" thingy (men... after 3 years of C# I still can't understand that no matter what I read, and I read Wikipedia, ASP.NET 3.5 Professional book, tutorials, ...) :(

In other words is, how can I reuse the properties.

I thought about creating an ExtensionMethod, but I will have to write all of them and not reuse any code... I thought about Creating a Generic control and create a new controls that inherit that base one so I could use

GlocalObject oPrintJob = null;

...

WordPrintJob oPrintJob = (WordPrintJob)GlocalObject;

am I making any sense?


Update from the answers

Ok, so, there is no "type T" but base class/interface... I'm then trying to create that Interface so I can inherit from it and I get this image below:

alt text http://www.balexandre.com/temp/2009-06-02_1521_soQuestion_Interface.png

If in the interface I say PrintJob type, how can I return a WordPrintJob type? :-( I don't get it ...

Upvotes: 1

Views: 1534

Answers (3)

chills42
chills42

Reputation: 14523

Along with Lazurus's answer, you may also want to create an Interface, and do most of your coding against it, instead of the base class.

Upvotes: 1

Lazarus
Lazarus

Reputation: 43094

Chris is essentially correct so your switch statement would look thus::

PrintJob oPrintJob = null;
switch (type)
{
    case convert2pdf.ConvertFileType.Word: 
        oPrintJob = new WordPrintJob(); 
        break;
    case convert2pdf.ConvertFileType.Excel: 
        oPrintJob = new ExcelPrintJob(); 
        break;
    case convert2pdf.ConvertFileType.PowerPoint: 
        oPrintJob = new PowerPointPrintJob(); 
        break;
    case convert2pdf.ConvertFileType.IE: 
        oPrintJob = new IEPrintJob(); 
        break;
    case convert2pdf.ConvertFileType.Publisher: 
        oPrintJob = new PublisherPrintJob(); 
        break;
    case convert2pdf.ConvertFileType.Visio: 
        oPrintJob = new VisioPrintJob(); 
        break;
    default: 
        oPrintJob = new GenericPrintJob();
        break;
}

The PDFSettings property would be defined in your base PrintJob class and each of the specific print job classes would inherit from that base class.

Upvotes: 2

ChrisF
ChrisF

Reputation: 137158

You'll need to define a base class PrintJob and have PDFSetting as a property of that.

Then define WordPrintJob, ExcelPrintJob etc. to be subclasses of PrintJob.

It's a long time since I've done this so I can't remember off hand whether you'll be able to access PDFSettings from the sub class or if you'll have to cast the variable to the base class first.

Upvotes: 3

Related Questions