Using interfaces for sharing properties Part II...

Discussion in 'ASP .Net' started by darrel, Jan 24, 2005.

  1. darrel

    darrel Guest

    Karl pointed me towards the use of interfaces to share information between
    controls. I'm getting the hang of it, but still running into some
    logic/syntax problems.

    What I'm trying to do is have a usercontrol set a property. I then want the
    parent page to read that property, and set a new property defined within an
    interface. I then want ANOTHER usercontrol to be able to access that
    property from the interface.

    Ultimately, I want one UC to be able to set a variabel that other UC's on
    the page can read without any of the UC's needing to know what the parent
    page actually is.

    The 'getVariables' usercontrol that sets the variable is set up as such:
    ---------------------------------------------------------
    Private testVariable as String = "monkey"
    Property PropTestVariable as string
    Get
    return testVariable
    end Get
    set
    testVariable = value
    end set
    end Property
    ---------------------------------------------------------

    the parent page is trying to read it as such:
    ---------------------------------------------------------
    public UC_getVariables as getVariables
    Public testVariableFromGetVariables = UC_getVariables.PropTestVariable
    ---------------------------------------------------------

    The problem is that the above gives me the old Object reference not set to
    an instance of an object. and is where I'm currently stuck on things. Is
    there any obvious syntax problems or logic problems in my above code?

    -Darrel
     
    darrel, Jan 24, 2005
    #1
    1. Advertising

  2. darrel

    darrel Guest

    > Private testVariable as String = "monkey"

    DUH! I had that PRIVATE instead of PUBLIC.

    d'oh. All fixed now.

    (slaps forehead)

    -Darrel
     
    darrel, Jan 24, 2005
    #2
    1. Advertising

  3. darrel

    Karl Seguin Guest

    Darrel:
    Is there an actual user control of type getVariables with an id
    "UC_getVariables" on your page?

    ala:
    <uc1:getVariables id="UC_getVariables" runat="server" />

    or dynamically created:
    dim UC_getVariables as getVariables =
    ctype(Page.LoadControl("getVariables.ascx"), getVariables)

    ??

    Seems like you are naming it, just not creating it.

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "darrel" <> wrote in message
    news:...
    > Karl pointed me towards the use of interfaces to share information between
    > controls. I'm getting the hang of it, but still running into some
    > logic/syntax problems.
    >
    > What I'm trying to do is have a usercontrol set a property. I then want

    the
    > parent page to read that property, and set a new property defined within

    an
    > interface. I then want ANOTHER usercontrol to be able to access that
    > property from the interface.
    >
    > Ultimately, I want one UC to be able to set a variabel that other UC's on
    > the page can read without any of the UC's needing to know what the parent
    > page actually is.
    >
    > The 'getVariables' usercontrol that sets the variable is set up as such:
    > ---------------------------------------------------------
    > Private testVariable as String = "monkey"
    > Property PropTestVariable as string
    > Get
    > return testVariable
    > end Get
    > set
    > testVariable = value
    > end set
    > end Property
    > ---------------------------------------------------------
    >
    > the parent page is trying to read it as such:
    > ---------------------------------------------------------
    > public UC_getVariables as getVariables
    > Public testVariableFromGetVariables = UC_getVariables.PropTestVariable
    > ---------------------------------------------------------
    >
    > The problem is that the above gives me the old Object reference not set

    to
    > an instance of an object. and is where I'm currently stuck on things. Is
    > there any obvious syntax problems or logic problems in my above code?
    >
    > -Darrel
    >
    >
     
    Karl Seguin, Jan 24, 2005
    #3
  4. darrel

    darrel Guest

    > Is there an actual user control of type getVariables with an id
    > "UC_getVariables" on your page?


    No. I'm declaring it as such:

    public UC_getVariables as getVariables

    (the actual usercontrol is 'getVariables')

    We seemed to post at the exact same time. The problem (I think) is that my
    property I was trying to read was Private, not public. That fixed things.

    However, since you're here ;o) I have a followup.

    I can now read this property from the parent page. And I can set it as such:
    ----------------------------------------------
    Public x = UC_getVariables.PropTestVariable
    Property contentID as object Implements IGetGlobalPageProperties.contentID
    Get
    return x
    end Get
    set
    x = value
    end set
    end Property
    ----------------------------------------------

    But this is where I'm back to square one. I'm not sure how to reference the
    above property from another child control on the page. You had mentioned
    earlier that I can reference the interface from there. I've tried something
    like this:

    IGetGlobalPageProperties.contentID

    But I'm left with the 'refrence to a non-shared member...' error.

    -Darrel
     
    darrel, Jan 24, 2005
    #4
  5. darrel

    darrel Guest

    > But this is where I'm back to square one. I'm not sure how to reference
    the
    > above property from another child control on the page. You had mentioned
    > earlier that I can reference the interface from there. I've tried

    something
    > like this:


    Karl:

    I'm re-reading your tutorial:

    http://openmymind.net/communication/index.html#4.2

    I'm struggling a bit with the logic.

    I think I got to the point where you make samplePage implement
    IResultContainer

    In my example, the parent page implements 'IGetGlobalPageProperties' which,
    in turn, sets a property for 'contentID'
    But it looks like I'm missing that second step...of declaring a class to
    make use of the contnetID property.

    -Darrel
     
    darrel, Jan 24, 2005
    #5
  6. darrel

    Karl Seguin Guest

    Looking at your previous post, I think ur trying to access contentId from
    IGetGlobalPAgeProperties directly. What you need to do is get it from the
    page, which implements IGetGlobalPageProperties

    in your user control you do:

    dim properties as IGetGlobalPageProperties = ctype(Page,
    IGetGlobalPageProperties)

    and you should then be able to do properties.ContentId

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "darrel" <> wrote in message
    news:%...
    > > But this is where I'm back to square one. I'm not sure how to reference

    > the
    > > above property from another child control on the page. You had mentioned
    > > earlier that I can reference the interface from there. I've tried

    > something
    > > like this:

    >
    > Karl:
    >
    > I'm re-reading your tutorial:
    >
    > http://openmymind.net/communication/index.html#4.2
    >
    > I'm struggling a bit with the logic.
    >
    > I think I got to the point where you make samplePage implement
    > IResultContainer
    >
    > In my example, the parent page implements 'IGetGlobalPageProperties'

    which,
    > in turn, sets a property for 'contentID'
    > But it looks like I'm missing that second step...of declaring a class to
    > make use of the contnetID property.
    >
    > -Darrel
    >
    >
     
    Karl Seguin, Jan 25, 2005
    #6
  7. darrel

    darrel Guest

    > Looking at your previous post, I think ur trying to access contentId from
    > IGetGlobalPAgeProperties directly. What you need to do is get it from

    the
    > page, which implements IGetGlobalPageProperties
    >
    > in your user control you do:
    >
    > dim properties as IGetGlobalPageProperties = ctype(Page,
    > IGetGlobalPageProperties)
    >
    > and you should then be able to do properties.ContentId


    Hey Karl,

    As always, thanks for all the help!

    I did the above, but that gives me this error:

    --------------------------
    Specified cast is not valid.
    dim propertiesFromParent as IGetGlobalPageProperties = ctype(Page,
    IGetGlobalPageProperties)
    --------------------------

    To clarify what I have:

    default.aspx (implements IGetGlobalPageProperties and reads properties from
    getVariables.ascx)
    - getVariables.ascx (sets properties)_
    - otherControl.ascx (uses the cast above in an attempt to read the
    contentID property from default.aspx (via the interface)

    I'm not quite sure what the cast is doing. How does it know to cast the
    parent page?

    I tried this:

    dim propertiesFromParent as IGetGlobalPageProperties = ctype(Page.parent,
    IGetGlobalPageProperties)

    Which gets past the above error, but I can't access the property as such:

    propertiesFromParent.ContentID
    because I get the ' Object reference not set to an instance of an object. '
    error.

    -Darrel
     
    darrel, Jan 25, 2005
    #7
  8. darrel

    Karl Seguin Guest

    Darrel:

    Page is a reference to the parent page...Page.Parent gives you a null
    reference becaue the parent page has no parent.

    if ctype(Page, IGet..) returns an invalid cast, it means that the page the
    user control is placed on doesn't implement IGetGlobalPageProperties

    if otherControl.ascx is placed on default.aspx which implements
    IGetGlobalPageProperties, then the cast should work and something is amiss.
    If you want to be able to use otherControl on a page which doesn't implement
    IGetGlobalPageProperties as well, then you need to do:

    if not Page Is IGetGlobalPageProperties then
    'what to do if the control isn't placed on a page which implements the
    interface?
    'maybe use a default?
    else
    ctype(Page, IGetGlobalPageProperties)..
    end if


    If you have a simple 1 page and 2 user control example (+ interface) feel
    free to email me a zip @ karlseguin @ hotmail . com (remove spaces)

    Cheers,
    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "darrel" <> wrote in message
    news:ek$...
    > > Looking at your previous post, I think ur trying to access contentId

    from
    > > IGetGlobalPAgeProperties directly. What you need to do is get it from

    > the
    > > page, which implements IGetGlobalPageProperties
    > >
    > > in your user control you do:
    > >
    > > dim properties as IGetGlobalPageProperties = ctype(Page,
    > > IGetGlobalPageProperties)
    > >
    > > and you should then be able to do properties.ContentId

    >
    > Hey Karl,
    >
    > As always, thanks for all the help!
    >
    > I did the above, but that gives me this error:
    >
    > --------------------------
    > Specified cast is not valid.
    > dim propertiesFromParent as IGetGlobalPageProperties = ctype(Page,
    > IGetGlobalPageProperties)
    > --------------------------
    >
    > To clarify what I have:
    >
    > default.aspx (implements IGetGlobalPageProperties and reads properties

    from
    > getVariables.ascx)
    > - getVariables.ascx (sets properties)_
    > - otherControl.ascx (uses the cast above in an attempt to read the
    > contentID property from default.aspx (via the interface)
    >
    > I'm not quite sure what the cast is doing. How does it know to cast the
    > parent page?
    >
    > I tried this:
    >
    > dim propertiesFromParent as IGetGlobalPageProperties = ctype(Page.parent,
    > IGetGlobalPageProperties)
    >
    > Which gets past the above error, but I can't access the property as such:
    >
    > propertiesFromParent.ContentID
    > because I get the ' Object reference not set to an instance of an object.

    '
    > error.
    >
    > -Darrel
    >
    >
     
    Karl Seguin, Jan 25, 2005
    #8
  9. darrel

    darrel Guest

    > if ctype(Page, IGet..) returns an invalid cast, it means that the page the
    > user control is placed on doesn't implement IGetGlobalPageProperties


    Hmm...here's what I have in default.aspx:

    =================================================
    Public Class default9th
    Inherits System.Web.UI.Page
    implements IGetGlobalPageProperties

    ....

    'read the variable from the UC 'getVariables'
    Public contentIDsetByGetVariablesUC = UC_getVariables.PropTestVariable

    'set the property
    Property contentID as object Implements IGetGlobalPageProperties.ContentID
    Get
    return contentIDsetByGetVariablesUC
    end Get
    set
    contentIDsetByGetVariablesUC = value
    end set
    end Property
    =================================================

    Then, in my 'otherControl' I have this:



    =================================================
    dim propertiesFromParent as IGetGlobalPageProperties = ctype(page,
    IGetGlobalPageProperties)

    response.Write (propertiesFromParent.contentID)
    =================================================

    Am I making an obvious syntax/logic blunder somewhere?

    > if otherControl.ascx is placed on default.aspx which implements
    > IGetGlobalPageProperties, then the cast should work and something is

    amiss.
    > If you want to be able to use otherControl on a page which doesn't

    implement
    > IGetGlobalPageProperties as well, then you need to do:
    >
    > if not Page Is IGetGlobalPageProperties then
    > 'what to do if the control isn't placed on a page which implements the
    > interface?
    > 'maybe use a default?
    > else
    > ctype(Page, IGetGlobalPageProperties)..
    > end if


    Ah...good point. Another reason why the 'controls reading from parent pages'
    maybe isn't the best technique.

    Originally, the whole point of this excercise was that I'm storing a bunch
    of page-centric information in an XML file and I felt (perhaps erroneously)
    that having each control read from the same XML file to be redundant and not
    efficient. However, I'm beginning to rethink that and thinking that perhaps
    that is the best way. Cache the XML file, and let each control read directly
    from it rather than having a UC act as this 'middle man' passing info back
    up to the parent page.

    > If you have a simple 1 page and 2 user control example (+ interface) feel
    > free to email me a zip @ karlseguin @ hotmail . com (remove spaces)


    That's generous of you! I might take you up on that. ;o)

    -Darrel
     
    darrel, Jan 25, 2005
    #9
  10. darrel

    darrel Guest

    > Darrel,
    > I've attached a very simple and basic working example. From what you've
    > given me everything looks ok...see if you can't spot the difference

    between
    > what I'm sending and what you have. Lemme know what you find out.


    Wow. Spent about an hour going through your example and comparing it line by
    line. I had a few errors on my part here and there.

    One last question...which I don't think is directly related to interfaces,
    is this:

    I was declaring my Usercontrols like this:

    protected UC_controlName as controlName

    while you were using:

    protected controlName as controlName

    My way would result in ' Object reference not set to an instance of an
    object.' errors.

    Anyways, I got it working, and I understand about 90% of it, so I've come a
    long way with this. Thank you! I owe you a beer! (or two!)

    BTW, I've started reading through some of your other articles on your site.
    That's a great resource!

    -Darrel
     
    darrel, Jan 25, 2005
    #10
  11. darrel

    Karl Seguin Guest

    thanks... and glad it's mosty working..

    how you declare it shouldn't matter. however, you might misunderstand (a
    sign that my method is ambiguous (and it is)). I'm not doing:

    protected controName as controlName

    I'm doing

    protected instancceId as controlName

    if you look at the aspx file you'll see I have:

    <uc1:GetVariables id=getVariables runat="server"></uc1:GetVariables>

    in page.aspx you can see that I have id=getVariables had I done
    id=uc_GetVariables I would then end up with the following declaration:

    protected uc_GetVariables as GetVariables

    It was confusing of me to give control the same ID as the class name...my
    guess is that you didn't actually have a control named uc_GetVariables in
    the aspx page...hence you didn't have an instance...hence the exception..

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "darrel" <> wrote in message
    news:%...
    > > Darrel,
    > > I've attached a very simple and basic working example. From what you've
    > > given me everything looks ok...see if you can't spot the difference

    > between
    > > what I'm sending and what you have. Lemme know what you find out.

    >
    > Wow. Spent about an hour going through your example and comparing it line

    by
    > line. I had a few errors on my part here and there.
    >
    > One last question...which I don't think is directly related to interfaces,
    > is this:
    >
    > I was declaring my Usercontrols like this:
    >
    > protected UC_controlName as controlName
    >
    > while you were using:
    >
    > protected controlName as controlName
    >
    > My way would result in ' Object reference not set to an instance of an
    > object.' errors.
    >
    > Anyways, I got it working, and I understand about 90% of it, so I've come

    a
    > long way with this. Thank you! I owe you a beer! (or two!)
    >
    > BTW, I've started reading through some of your other articles on your

    site.
    > That's a great resource!
    >
    > -Darrel
    >
    >
     
    Karl Seguin, Jan 25, 2005
    #11
  12. darrel

    darrel Guest

    > protected instancceId as controlName

    (me slaps forehead)

    ARGH! Of course! I wasn't connecting it to the UC ID declared in the ASPX
    page. I normally preface all my control names with what they are (textbox_,
    uc_, label_, etc.) and I didn't this time as I was just throwing things
    together to test with.

    That's another headache gone ;o)

    Thanks again, Karl!

    -Darrel
     
    darrel, Jan 25, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. darrel
    Replies:
    0
    Views:
    323
    darrel
    Jan 21, 2005
  2. iceColdFire
    Replies:
    4
    Views:
    351
    iceColdFire
    May 17, 2005
  3. Ra Carter
    Replies:
    1
    Views:
    733
    sloan
    Mar 17, 2010
  4. ezeget.com

    Web Part INterfaces SSRS Deep INtegration and Sharepoint

    ezeget.com, Oct 9, 2007, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    189
    ezeget.com
    Oct 25, 2007
  5. Jack
    Replies:
    8
    Views:
    283
Loading...

Share This Page