TK.
TK.

Reputation: 47913

C# Default scope resolution

I have inherited a c# class 'Button' (which I can't change) which clashes with the BCL class 'Windows.Forms.Button'. Normally, Id be very happy to go:

MyPackage.MyClass.Button;

But there are a large number or references to this class which is a pain to have to re-type.

Is there any way to get the compiler (linker?) to default to using the customised version of Button over the BCL version?

Upvotes: 6

Views: 1299

Answers (5)

Mr. Mark
Mr. Mark

Reputation: 69

You can at least make it a small bit less painful/wordy with "using":

using MPMC = MyPackage.MyClass;

then you can say:

MPMC.Button

Upvotes: 0

TK.
TK.

Reputation: 47913

It appears that I can do the following:

using Button = MyPackage.MyClass.Button;

works and it preserves all references within the code to Button. Although Im tempted not to go down this route as it is still ambiguious (at least to the reader) which Button is being used.

Upvotes: 0

Philip Rieck
Philip Rieck

Reputation: 32578

if you want to use it by default, replace

using Windows.Forms;

with

using MyPackage.MyClass;

If you do that, you'll need to fully qualify all the buttons from Windows.Forms.

Or, if you want to, you can alias the namespace

using My = MyPackage.MyClass;
//... then
My.Button b = ...

Or alias the button

using MyButton = MyPackage.MyClass.Button;

Upvotes: 4

Joel Coehoorn
Joel Coehoorn

Reputation: 416121

Add this to the top of the file:

using MyButton = MyPackage.MyClass.Button;

Now you can reference your custom button using a distinct name. You may need to do something similar for the stock button if you use that anywhere in the same file.

Upvotes: 21

Vincent McNabb
Vincent McNabb

Reputation: 34729

You could remove using Windows.Forms; from the top of the code. That would of course mean that you would have to reference all Windows.Forms items specifically.

Upvotes: 2

Related Questions