Interface implementation

Last post 01-07-2009 7:52 PM by exlaf. 2 replies.

Sort Posts:

  • Interface implementation

    01-07-2009, 7:06 PM
    • Member
      1 point Member
    • exlaf
    • Member since 03-27-2008, 6:31 PM
    • Posts 4

     Hi!

    this is my first post so Hello everyone!Geeked

    Sorry if my question is bit noobish but here it goes - thing I'm currently struggling to grasp is why predefined classes in .NET framework don't implement their interfaces within their bodies? I'm working currently with StateBag class and it says that class implements IStateManager, IDictionary, ICollection, IEnumerable interfaces, however there are no members of those interfaces within StateBag class declaration

     

    in my custom class I have a field of StateBag type:

    StateBag ViewState = new StateBag();

    if StateBag class implements say IStateManager interface then I would have thought that ViewState field should be able to access *directly* IStateManager members like LoadViewState(), SaveViewState(), TrackViewState() and so on.it's not the case and those members are called:

    ((IStateManager)ViewState).LoadViewState();

    the way they are called is quite confusing for me and it seems to me that StateBag class don't implement those interfaces *directly*

     

    If anyone has explanation for this problem please share your knowledgeBig Smile, once again sorry if the question is bit silly

    thank you in advance

    Filed under:
  • Re: Interface implementation

    01-07-2009, 7:29 PM
    Answer
    • All-Star
      98,112 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,347
    • Moderator
      TrustedFriends-MVPs

    The methods implemented by StateBag for the IStateManager interface were implemented as private methods.  This means you cannot access them when you are using the StateBag object as a StateBag.  If you cast StateBag as an IStateManager type though, then you can access those methods through the IStateManager interface.  This is what is occurring in this example:

    ((IStateManager)ViewState).LoadViewState();  

     ViewState is being cast as IStateManager so that LoadViewState will become accessible.

     

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: Interface implementation

    01-07-2009, 7:52 PM
    • Member
      1 point Member
    • exlaf
    • Member since 03-27-2008, 6:31 PM
    • Posts 4

    ahhhhhh so it's the case of member accessor level, not in a million years would I thought about that so thank you very much for your answer!Yes

     

     

    that begs one more question though.

    considering necsessary casting - which object holds the information about loading, tracking, saving of the viewstate? I guess it must be object of the StateBag class though afromentioned casting is confusing me a little bitEmbarrassed

Page 1 of 1 (3 items)