Click here to Skip to main content
15,888,351 members
Home / Discussions / C#
   

C#

 
GeneralRe: CreateInstanceAndUnwrap -- File or Dependency Not Found Pin
Nick Parker8-Mar-04 9:19
protectorNick Parker8-Mar-04 9:19 
GeneralRe: CreateInstanceAndUnwrap -- File or Dependency Not Found Pin
leppie8-Mar-04 7:14
leppie8-Mar-04 7:14 
GeneralRe: CreateInstanceAndUnwrap -- File or Dependency Not Found Pin
Matthew Hazlett8-Mar-04 9:06
Matthew Hazlett8-Mar-04 9:06 
GeneralRe: CreateInstanceAndUnwrap -- File or Dependency Not Found Pin
Matthew Hazlett8-Mar-04 16:35
Matthew Hazlett8-Mar-04 16:35 
GeneralRe: CreateInstanceAndUnwrap -- File or Dependency Not Found Pin
leppie9-Mar-04 6:20
leppie9-Mar-04 6:20 
Question.NET interfaces - why doesn't this work? Pin
Judah Gabriel Himango7-Mar-04 19:00
sponsorJudah Gabriel Himango7-Mar-04 19:00 
AnswerRe: .NET interfaces - why doesn't this work? Pin
Meysam Mahfouzi8-Mar-04 0:02
Meysam Mahfouzi8-Mar-04 0:02 
GeneralRe: .NET interfaces - why doesn't this work? Pin
Judah Gabriel Himango8-Mar-04 4:32
sponsorJudah Gabriel Himango8-Mar-04 4:32 
You're not making sense. Of course you can't do that conversion; classes A and B are not compatible even if they share a method (or even all methods).

However, an interface is supposed to work like that; ICloneable specifies a .Clone method, and all objects that implement ICloneable have a .Clone method. If I create my own interface IFoo that specifies a .Clone method, any class that is ICloneable should be convertible to IFoo.

Here's an example situation I ran into last night that brought about this question. I have a component that is passed either a Sys.Win.Forms.Form object or a Sys.Win.Forms.Control object. It is completely up to the consumer of my component whether to pass a Form or a Control. In my component class, the only thing I care about is that the passed object, either Form or Control, have a .Handle getter property and a .Invalidate() method. Because both Form and Control have these types, it would be ideal if I could create my own interface and store that type, like so:

interface IHandleWithInvalidate
{
  IntPtr Handle{get;}
  void Invalidate();
}

class myComponent
{
    private IHandleWithInvalidate passedObject;
    
    public myComponent(Form f)
    {
        this.passedObject = f;
    }

    public myComponent(Control c)
    {
        this.passedObject = c;
    }

    private void DoSomething()
    {
        this.passedObject.Invalidate();
        IntPtr h = this.passedObject.Handle;
    }
}


Alas, .NET won't let me do this. Instead, I have to keep 2 variables, and throughout my code, I have to check which one to use, nearly doubling the amount of code I have to write. For example, the above snippet would now look like this:

class myComponent
{
    private Form passedForm;
    private Control passedControl;
    
    public myComponent(Form f)
    {
        this.passedForm = f;
    }

    public myComponent(Control c)
    {
        this.passedControl = c;
    }

    private void DoSomething()
    {
        if(this.passedForm != null)
        {
            this.passedForm.Invalidate();
            IntPtr h = this.passedForm.Handle;
        }
        else
        {
            this.passedControl.Invalidate();
            IntPtr h = this.passedControl.Handle;
        }
    }
}


As you can see in the DoSomething() method, I have to write 10 lines of code instead of 2. Given that I'll be using my passed object often, one can clearly see the side effects of using such a model.

I think C# should have some construct to allow me to accomplish a similar task without writing extra lines of code; if not interfaces, then maybe something like C unions. What do you guys think?


---------------------------
He who knows that enough is enough will always have enough.

-Lao Tsu

AnswerRe: .NET interfaces - why doesn't this work? Pin
Werdna8-Mar-04 4:20
Werdna8-Mar-04 4:20 
AnswerRe: .NET interfaces - why doesn't this work? Pin
Heath Stewart8-Mar-04 4:22
protectorHeath Stewart8-Mar-04 4:22 
GeneralRe: .NET interfaces - why doesn't this work? Pin
Judah Gabriel Himango8-Mar-04 8:48
sponsorJudah Gabriel Himango8-Mar-04 8:48 
GeneralRe: .NET interfaces - why doesn't this work? Pin
Tom Larsen8-Mar-04 9:24
Tom Larsen8-Mar-04 9:24 
GeneralRe: .NET interfaces - why doesn't this work? Pin
Judah Gabriel Himango8-Mar-04 10:08
sponsorJudah Gabriel Himango8-Mar-04 10:08 
GeneralRe: .NET interfaces - why doesn't this work? Pin
Heath Stewart8-Mar-04 10:22
protectorHeath Stewart8-Mar-04 10:22 
AnswerRe: .NET interfaces - why doesn't this work? Pin
Tom Larsen8-Mar-04 9:13
Tom Larsen8-Mar-04 9:13 
GeneralRe: .NET interfaces - why doesn't this work? Pin
Judah Gabriel Himango8-Mar-04 10:04
sponsorJudah Gabriel Himango8-Mar-04 10:04 
GeneralRe: .NET interfaces - why doesn't this work? Pin
Nick Parker8-Mar-04 17:57
protectorNick Parker8-Mar-04 17:57 
Generalloop error Pin
ASGill7-Mar-04 16:42
ASGill7-Mar-04 16:42 
GeneralRe: loop error Pin
Judah Gabriel Himango7-Mar-04 16:51
sponsorJudah Gabriel Himango7-Mar-04 16:51 
GeneralRe: loop error Pin
ASGill7-Mar-04 18:06
ASGill7-Mar-04 18:06 
GeneralRe: loop error Pin
Judah Gabriel Himango7-Mar-04 18:51
sponsorJudah Gabriel Himango7-Mar-04 18:51 
GeneralRe: loop error Pin
Dave Kreskowiak8-Mar-04 3:01
mveDave Kreskowiak8-Mar-04 3:01 
GeneralRe: loop error Pin
ASGill8-Mar-04 3:15
ASGill8-Mar-04 3:15 
GeneralRe: loop error Pin
Dave Kreskowiak8-Mar-04 8:08
mveDave Kreskowiak8-Mar-04 8:08 
GeneralReading from text file and spliting information into array Pin
lordjpg7-Mar-04 14:04
lordjpg7-Mar-04 14:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.