Test if Object exists

Discussion in 'ASP .Net' started by tshad, Aug 7, 2007.

  1. tshad

    tshad Guest

    I have an object that may or may not exist on a page. Therefore, I test for
    it. But it doesn't seem to work if I do the following:

    if not HomeLink is nothing then
    HomeLink.NavigateUrl="http://www.staffingworkshop.com/"

    I get the error message:
    ********************************************************************
    Compilation Error
    Description: An error occurred during the compilation of a resource required
    to service this request. Please review the following specific error details
    and modify your source code appropriately.

    Compiler Error Message: BC30451: Name 'HomeLink' is not declared.

    Source Error:

    Line 40: if not isPostBack then
    Line 41: if Session("SiteSourceCompany") is nothing then
    Line 42: if not HomeLink is nothing then
    HomeLink.NavigateUrl="http://www.stw.com/" <-- error
    Line 43: end if
    Line 44: Dim OldStart = Session("start") 'Keep this as the clear will
    dump it
    ***********************************************************************

    How do I test to see if an object exists or not?

    Thanks,

    Tom
     
    tshad, Aug 7, 2007
    #1
    1. Advertising

  2. tshad

    John Mott Guest

    What you probably want to do is to use FindControl to see if it exists or
    not, that will prevent the compile error. In this context 'this' is the
    current page.

    HyperLink hl = this.FindControl("HomeLink");
    if (hl != null) {
    hl.NavigateUrl="http://www.staffingworkshop.com/"
    }

    John
    Nice Clean Examples
    www.nicecleanexample.com

    "tshad" <> wrote in message
    news:erWS%...

    >I have an object that may or may not exist on a page. Therefore, I test
    >for it. But it doesn't seem to work if I do the following:
    >
    > if not HomeLink is nothing then
    > HomeLink.NavigateUrl="http://www.staffingworkshop.com/"
    >
    > I get the error message:
    > ********************************************************************
    > Compilation Error
    > Description: An error occurred during the compilation of a resource
    > required to service this request. Please review the following specific
    > error details and modify your source code appropriately.
    >
    > Compiler Error Message: BC30451: Name 'HomeLink' is not declared.
    >
    > Source Error:
    >
    > Line 40: if not isPostBack then
    > Line 41: if Session("SiteSourceCompany") is nothing then
    > Line 42: if not HomeLink is nothing then
    > HomeLink.NavigateUrl="http://www.stw.com/" <-- error
    > Line 43: end if
    > Line 44: Dim OldStart = Session("start") 'Keep this as the clear
    > will dump it
    > ***********************************************************************
    >
    > How do I test to see if an object exists or not?
    >
    > Thanks,
    >
    > Tom
    >
    >
     
    John Mott, Aug 7, 2007
    #2
    1. Advertising

  3. tshad

    lee whitbeck Guest

    On Aug 7, 12:54 pm, "tshad" <> wrote:
    > I have an object that may or may not exist on a page. Therefore, I test for
    > it. But it doesn't seem to work if I do the following:
    >
    > if not HomeLink is nothing then
    > HomeLink.NavigateUrl="http://www.staffingworkshop.com/"
    >
    > I get the error message:
    > ********************************************************************
    > Compilation Error
    > Description: An error occurred during the compilation of a resource required
    > to service this request. Please review the following specific error details
    > and modify your source code appropriately.
    >
    > Compiler Error Message: BC30451: Name 'HomeLink' is not declared.
    >
    > Source Error:
    >
    > Line 40: if not isPostBack then
    > Line 41: if Session("SiteSourceCompany") is nothing then
    > Line 42: if not HomeLink is nothing then
    > HomeLink.NavigateUrl="http://www.stw.com/" <-- error
    > Line 43: end if
    > Line 44: Dim OldStart = Session("start") 'Keep this as the clear will
    > dump it
    > ***********************************************************************
    >
    > How do I test to see if an object exists or not?
    >
    > Thanks,
    >
    > Tom


    That should work, but you must declare HomeLink, which is why the
    compiler is complaining. Once you have it declared in your code, you
    can check if it exists or not.
     
    lee whitbeck, Aug 7, 2007
    #3
    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. Totan
    Replies:
    0
    Views:
    1,079
    Totan
    Apr 17, 2006
  2. Squid Seven
    Replies:
    11
    Views:
    460
    msalters
    Jul 11, 2005
  3. Ulf Meinhardt
    Replies:
    8
    Views:
    6,305
  4. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    762
    Skybuck Flying
    Oct 4, 2009
  5. George
    Replies:
    7
    Views:
    178
    Thomas 'PointedEars' Lahn
    Oct 3, 2009
Loading...

Share This Page