Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Interfaces
Message
From
19/01/2010 03:38:09
 
General information
Forum:
ASP.NET
Category:
Other
Title:
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01444635
Message ID:
01444688
Views:
46
>>>>>Hi,
>>>>>
>>>>>I ran into the following issue with Interfaces:
>>>>>
>>>>>Some form class implements an interface, e.g. IMyInterface.
>>>>>
>>>>>The textboxes on the form are based on textboxes in a class library like this:
>>>>>
>>>>>
>>>>>namespace MyCompany
>>>>>{
>>>>>
>>>>>    public class MyTextBox : System.Windows.Forms.TextBox
>>>>>    {
>>>>>        protected override void OnTextChanged(EventArgs e)
>>>>>        {
>>>>>            Control o = this.parent;
>>>>>            if (o != null && (o is IMyInterface))
>>>>>            {
>>>>>                ((IMyInterface)o).MyMethod(false);
>>>>>            }
>>>>>            base.OnTextChanged(e);
>>>>>        }
>>>>>    }
>>>>>
>>>>>}
>>>>>
>>>>>
>>>>>Compiler complaints that IMyInterface "The type of namespace 'IMyInterface' could not be found..." Do I need to declare the Interface in this library too? What else am I missing?
>>>>>
>>>>>TIA.
>>>>
>>>>What is the namespace of IMyInterface? It seems like you might need to add an import statement or fully qualify it.
>>>
>>>This is what I am not sure about. The Namespace of the IMyInterface in the class where the interface belongs. This code (above) is a base class of Textbox used by many other classes. And since this class library is "generic" adding import to it does not make sense to me (or maybe I don't see something simple).
>>
>>The compiler has to know what IMyInterface is inorder to be able to compile your code.
>>
>>So the project you are in needs to reference the project where the interface is defined and the name space needs to either be in a using statement or included in the reference to IMyInterface. Otherwise it has no clue as to what IMyInterface is.
>>
>>1. Add a reference to the project with IMyInterface to your current project
>>2. Add: using MyNamespce; at the start of the file.
>>
>>It should then be able to compile your code.
>
>From what you are telling me it seems that I can't use Interfaces in this particular case (which I kind of suspected). Let me explain. The textbox classes where the event OnTextChanged need to "be aware" of the interface IMyInterface are in a separate project of all base classes used by different projects of the application. So what you are saying is that I need to reference each one of the projects in the class where the text box class reside. And this is "impractical" if not "impossible" since every time I use this base textbox class in a project I will need to reference the new project in the base class library. I hope I am explaining myself clear.
>
>Thank you for your help.

I think Bonnie is right - you're looking at this backwards. The only assembly that needs to be referenced by the assembly containing the TextBox is the one where IMyInterface is defined.

Three options:

(a) (Obviously) Place the TextBox, the Interface AND the Form implementing the interface in the same assembly.

(b) Place the IMyInterface definition in the same assembly as the TextBox. This ensures that the Interface is available anywhere the TextBox is used.

(b) Place the interface in a seperate assembly and reference this in the assembly containing the TextBox and any other assembly where it is required (e.g. any assembly containing a Form implementing the interface).

The latter may seem the most complicated but is probably the best approach. You will almost certainly find a need to place other interfaces or classes in some 'common' location to avoid requiring 'circular references' between assemblies. I tend to have just one such common assembly - but make sure nothing goes in there unless there's no other option.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform