Results 1 to 7 of 7

Thread: [RESOLVED] Implementing Interfaces?

  1. #1

    Thread Starter
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Resolved [RESOLVED] Implementing Interfaces?

    Hi

    Here is a nice simple Yes/No question, Can I use an interface like this...

    This is a class with an interface, and one that implements this interface
    Code:
    Public class Ifaces
        Public interface theInterface
            Public sub doSomething()
        End Interface
    End Class
    
    Public class Test
       Implements Ifaces.theInterface
       Public sub dosomething implements,blablabla
          msgbox("Hello Interface World")
       end sub
    End sub
    Here is the question..Its about using the object declared as the interface...
    In the real instance of this application, Test will be unknown, all I know is that it will implement the interface, it will be loaded as a plugin, I just named it here as test to avoid the rest of the code for loading the assembly from which it will come from...
    Code:
    Dim objTest as Test
    Dim obj as Ifaces.theInterface = objTest
    obj.dosomething
    Sorry if I am not clear enough with my explanation...

    Signatures suck

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Implementing Interfaces?

    Ummm....yes? Why are you declaring the Interface in a class? That one piece doesn't look right. An interface gets declared on its own. Defining it inside a class, as you have, should work, as you can define one class entirely within another, but doing so is kind of a strange thing to do. If it were declared Private, then the object (an interface in this case) would be visible, and could only be instantiated within the containing class. Since it is declared as Public, I think you could use it outside the class, but why bother defining it inside the class if you want to use it outside the class?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: Implementing Interfaces?

    Quote Originally Posted by Shaggy Hiker
    Ummm....yes? Why are you declaring the Interface in a class? That one piece doesn't look right. An interface gets declared on its own. Defining it inside a class, as you have, should work, as you can define one class entirely within another, but doing so is kind of a strange thing to do. If it were declared Private, then the object (an interface in this case) would be visible, and could only be instantiated within the containing class. Since it is declared as Public, I think you could use it outside the class, but why bother defining it inside the class if you want to use it outside the class?
    The interface will not be public if it is declared private nomatter what the class was declared with, and I do it this way so that when someone wants to write a plugin, all they have to do to get access to all interfaces is add a refrence, which i gues it should be a namespace rather than a class in that case, thanx for pointing that out, it is kinda strange when i think about it....

    Signatures suck

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Implementing Interfaces?

    Right, if it was declared Private, it would BE private, which would be a REALLY odd thing to do with an interface, though it has some value when talking about classes.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: Implementing Interfaces?

    Yeh, it would be ODD, and I misread your post before too, I thaught you said it would be private, sorry.
    so is it ok to refer to an instantiated object as the type of interface it implements or i spose the name of interface it implements?

    Signatures suck

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Implementing Interfaces?

    The whole point of interfaces is that you CAN refer to instances of any class that implements an interface as that type. I like to think of an interface as a role that a type can fill.

    My favourite example is the DataSource property of a DataGridView. You can assign any object to that property as long as it implements the IList or IListSource interface. Those interfaces expose specific members that the DataGridView will use to extract data in order to display it. The grid doesn't care what type of object it is as long as it implements those members, and the interface guarantees that it does. That is exactly why interfaces exists. Internally the DataGridView will refer to the object as an IList or IListSource, not as its actual type.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: Implementing Interfaces?

    Quote Originally Posted by jmcilhinney
    Internally the DataGridView will refer to the object as an IList or IListSource, not as its actual type.
    Got it, thanx...

    Signatures suck

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width