Resolved Why did MS include the PrinterSetting in DefaultPageSetting given that it is in PrintDocument.

aaaron

Well-known member
Joined
Jan 23, 2011
Messages
219
Programming Experience
10+
This is not a dire need post - just something I've wondered about for years

VB.NET:
    System.Diagnostics.Debug.WriteLine($"Are the two PrinterSettings equal {Object.ReferenceEquals(mPrintDocument.PrinterSettings, mPrintDocument.PrinterSettings.DefaultPageSettings.PrinterSettings)}");
    mPrintDocument.PrinterSettings.PrinterName = mPrintDocument.PrinterSettings.DefaultPageSettings.PrinterSettings.PrinterName;

It started with some downloaded code which appears to be written by someone knowledgeable.

It contained the second line above.

If that code makes sense to you I'd appreciate it if you'd explain what it does.

The thing I've wondered about is: Why did MS include the PrinterSetting property in DefaultPageSetting given that is it already in PrintDocument.

As you can see from the above I've checked and, in the test anyway, the references are equal.

Are they sometimes different?
 
Solution
The second line does nothing. PrintDocument.PrinterSettings and PrintDocument.PrinterSettings.DefaultPageSettings.PrinterSettings is always the same object. That DefaultPageSettings belongs to PrinterSettings and can not be changed because it is a readonly property. PrintDocument.DefaultPageSettings also returns the same object initially, but that can be changed.
If you set DefaultPageSettings then they are not the same anymore.

It is also convenient to access e.PageSettings.PrinterSettings in event handlers.

If that code makes sense to you I'd appreciate it if you'd explain what it does.
It checks if two objects are the same. In VB you can use Is operator
 
Last edited:
Greate, your second line sounds like it could very well be MS' reason. Thanks.

However, the first line gives me pause. I should have included more code:

VB.NET:
            mPrintDocument.PrinterSettings.PrinterName = "JUNK1";
            mPrintDocument.PrinterSettings.DefaultPageSettings.PrinterSettings.PrinterName = "JUNK2";
            //  mPrintDocument.PrinterSettings.PrinterName = "JUNK3";
          
            System.Diagnostics.Debug.WriteLine($"Are the two PrinterSettings equal {Object.ReferenceEquals(mPrintDocument.PrinterSettings, mPrintDocument.PrinterSettings.DefaultPageSettings.PrinterSettings)}");

I'll redo to check but, I had done the above with various lines commented out and always got True for the equals question.

And I looked at PrinterName with debug - always the same.

Oh, I wonder if you mean to assign to DefaultPageSettings rather than, as I did, simply change one of its properties. A yes here would clear up all.
 
Last edited:
VB.NET:
    System.Diagnostics.Debug.WriteLine($"Are the two PrinterSettings equal {Object.ReferenceEquals(mPrintDocument.PrinterSettings, mPrintDocument.PrinterSettings.DefaultPageSettings.PrinterSettings)}");
    mPrintDocument.PrinterSettings.PrinterName = mPrintDocument.PrinterSettings.DefaultPageSettings.PrinterSettings.PrinterName;

It started with some downloaded code which appears to be written by someone knowledgeable.

It contained the second line above.

If that code makes sense to you I'd appreciate it if you'd explain what it does.

The thing I've wondered about is: Why did MS include the PrinterSetting property in DefaultPageSetting given that is it already in PrintDocument.

As you can see from the above I've checked and, in the test anyway, the references are equal.

Are they sometimes different?

It checks if two objects are the same. In VB you can use Is operator
Actually I asked about the second line.
From that statement I'd surmise that PrintDocument.PrinterSettings are effective in the printing and the default values (which are not) may have been earlier set.
So the statement would make the value the user inserted into the default value effect the printing which it would not have otherwise done..

But, if I understand your post, it's the default page settings that are passed to event handler. Not the PrintDocument.PrinterSetting.

Help :)
 
Last edited:
The second line does nothing. PrintDocument.PrinterSettings and PrintDocument.PrinterSettings.DefaultPageSettings.PrinterSettings is always the same object. That DefaultPageSettings belongs to PrinterSettings and can not be changed because it is a readonly property. PrintDocument.DefaultPageSettings also returns the same object initially, but that can be changed.
 
Solution
PrinterDocument Diargram.png
 
Almost correct. It is really this simple, PrintDocument.DefaultPageSettings only has two states:
  1. Default: PrintDocument starts with PrinterSettings which also has the DefaultPageSettings, PrintDocument.DefaultPageSettings just returns a reference to this object.
  2. User-defined: If you set a new PrintDocument.DefaultPageSettings object it no longer references the one from PrintDocument.PrinterSettings. Once set it remains in this state for the lifetime of the PrintDocument instance.
PrintDocument constructor:
defaultPageSettings = new PageSettings(printerSettings);
PrinterSetting property setter:
printerSettings = value;
if (!userSetPageSettings) // "if not user set PageSettings"
{
    defaultPageSettings = printerSettings.DefaultPageSettings;
}
DefaultPageSettings property setter:
defaultPageSettings = value;
userSetPageSettings = true;
 
I'll try again later. I was hoping you'd answer by telling me what is wrong my figure.

Actual I thought I had it right finally and wanted to share with anyone who later had the same problem

Thanks for taking so much time with me.
 
Last edited:
I was hoping you'd answer by telling me what is wrong my figure.
The two DefaultPageSettings (printer and document) are not different objects, not until you make them different objects.
The two PrinterSettings (printer and default page) are not always same object.
 
Nothing wrong there, but PrintDocument.PrinterSettings.DefaultPageSettings.PrinterSettings is really nothing to consider, you can put as many dots you want after PrintDocument.PrinterSettings and it will still be the one and only PrintDocument.PrinterSettings object.
 
Back
Top