Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
O.k, it's late, I'm drawing a blank and am about to ask a stupid question.
OOP Basic Concepts.

I'm trying to inherit a .Net class called System.Printing.PrintSystemJobInfo
(From the .Net reference assemblies System.Printing.dll version 3.0.0.0)

Here is my non functioning code:

C#
namespace MyNameSpace
{
    class PrintSystemJobInfoCustom : System.Printing.PrintSystemJobInfo
    {
        protected PrintSystemJobInfoCustom(){}

        public DateTime TimeJobSubmittedCustom { get; set; }
    }
}


The error I'm getting with the constructor is :
The type 'System.Printing.PrintSystemJobInfo' has no constructors defined


But as you can see I did create a contructor, but obviously not correctly. When I remove this contructor, it gives me the same error, but at a class level.
I made the constructor protected because it gave me this error when it was public:
'System.Printing.PrintSystemJobInfo.PrintSystemJobInfo(System.Printing.PrintQueue)' is inaccessible due to its protection level


Is something in that class preventing inheritance, or am I overlooking something simple?
Posted

You cannot inherit from a class with no public or protected constructors.
You get the first error because the compiler can't find a public default constructor of PrintSystemJobInfo.
And you get the second error because when you define no constructor the default constructor of the base is used which in this case is private.

May I ask why you want to inhirit from PrintSystemJobInfo?
 
Share this answer
 
v2
Comments
TheyCallMeMrJames 22-Aug-11 11:14am    
I'd like to know, too. What could you be doing by inheriting there?
MatthysDT 22-Aug-11 11:17am    
Thank you.
I want to add my own TimeJobSubmitted field since the one I get is offset by 2 hours, I expect this is because it send me the GMT DateTime, I have no idea why. And I don't like hardcoding the GMT+2 for my timezone. And to add to this, at one stage, the DateTime actually came through as GMT+2 without me changing any code that should have affected it, so at this stage I do not trust the value of that field and would like to add my own.

If I can't inherit, I'll create a class that instantiates the PrintSystemJobInfo object, it's just more cumbersome to work with.
Manfred Rudolf Bihy 22-Aug-11 11:22am    
Proposed as the solution! 5+
From MSDN, we see that the System.Printing.PrintSystemJobInfo does not have a public constructor (most probably it's private or internal), so you cannot inherit the class. You can get an instance of the class using the static Get() method.

However, if you must create an instance, you can try using the Decorator[^] pattern.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 22-Aug-11 11:23am    
Good idea! 5+
MatthysDT 22-Aug-11 11:24am    
Thank you!
I'll check out and try implementing that pattern 1st thing tomorrow and get back to you ASAP!
BobJanova 22-Aug-11 11:57am    
Decorator seems to require inheriting from the class you're 'decorating', too, so it won't work in this case.
[no name] 22-Aug-11 12:08pm    
IMO, Decorator involves composition and not inheritance.
How about an extension method?
public DateTime GetTimeJobSubmittedCustom(this PrintSystemJobInfo target){
 return target.TimeJobSubmitted + get the time zone offset from settings;
}


You can get the current time zone from the locale settings somewhere.
 
Share this answer
 
Comments
MatthysDT 22-Aug-11 12:03pm    
Thank you.
Like I mentioned earlier, that I had an instance where that field was correct according to my timezone without my tampering, so at this stage I don't trust it enough to use it at all. Could be paranoia and there is probably a good explanation, but I'd rather populate and use a custom field.
[no name] 22-Aug-11 12:09pm    
+5, Nice and simple. But I have an aversion to extension methods unless it is the only possible solution. They're not part of standard OOP.
BobJanova 22-Aug-11 14:09pm    
In general I agree with you. But some people like them so I will still submit them as a solution where they are appropriate!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900