Results 1 to 6 of 6

Thread: [RESOLVED] casting to an interface?

  1. #1

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Resolved [RESOLVED] casting to an interface?

    Hi there,

    So I was looking at some code regarding the display of XPS documents in XAM,
    opening an xps document and setting the source in you DocumentViewer object is easy enough, you just open it and call GetFixedDocumentSequence()

    What confuses me is the following - don't understand why they are casting the
    Code:
    DocumentPaginator myPaginator = ((IDocumentPaginatorSource)myDocumentViewer).DocumentPaginator;
    They seem to be casing to an interface and I don't understand why... I don't know that it really matters in day to day life however I figured it was worth asking.

    Of course I can clearly see what is going on when you do something like this
    Code:
    myButton.BackGround = (Brush)this.FindResource("MyBrushResource");

    OK - Thanks a lot!
    Anti DUPLO machine!!!

  2. #2
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: casting to an interface?

    Hi r0k3t,

    The reason for this class is because they are getting the DocumentPaginator object form a flow document. Im not 100% about the specifics but I believe that when flow documents are casted to objects using this interface they can be paginated as if they where fixed documents.

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

    Re: casting to an interface?

    The reason is that the FlowDocument class explicitly implements the IDocumentPaginatorSource.DocumentPaginator property. When a class explicitly implements an interface member, you can only access that member after first casting as the interface type.

    As an example, the DataTable class implements the IListSource interface. As a result, you would think that you should be able to do this:
    CSharp Code:
    1. var table = new DataTable();
    2. var list = table.GetList();
    but that won't compile. You have to do this:
    CSharp Code:
    1. var table = new DataTable();
    2. var list = ((IListSource) table).GetList();
    That's because the DataTable class explicitly implements the IListSource.GetList method.

    Note, a class implementing an interface looks like this:
    csharp Code:
    1. public interface ISomething
    2. {
    3.     void DoSomething();
    4. }
    5.  
    6. public class Something : ISomething
    7. {
    8.     public void DoSomething()
    9.     {
    10.         throw new NotImplementedException();
    11.     }
    12. }
    while the corresponding explicit implementation looks like this:
    csharp Code:
    1. public interface ISomething
    2. {
    3.     void DoSomething();
    4. }
    5.  
    6. public class Something : ISomething
    7. {
    8.     void ISomething.DoSomething()
    9.     {
    10.         throw new NotImplementedException();
    11.     }
    12. }
    Note that you can also implement some members of the interface and explicitly implement others.
    Last edited by jmcilhinney; Dec 21st, 2009 at 02:21 AM.
    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

  4. #4
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: casting to an interface?

    That makes sense to me I wonder though why would you want to explicitly mark a method like that?

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

    Re: casting to an interface?

    Quote Originally Posted by DeanMc View Post
    That makes sense to me I wonder though why would you want to explicitly mark a method like that?
    Plenty of reading on that topic.

    http://www.google.com.au/search?q=ex...ient=firefox-a
    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

  6. #6

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Re: casting to an interface?

    Sweet - thanks! I also read the msdn link and it made sense. I guess I have never needed to do that... but it is another tool in the toolbox I guess.
    Anti DUPLO machine!!!

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