Is this cast necesarry?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc

    Is this cast necesarry?

    Hi, reading a book on .NET I see the following code.

    // MyControl.ascx contains a user control
    MyControl c2 = (MyControl)Load Control("MyCont rol.ascx");
    form1.Controls. Add(c2);

    I do not think this cast is necesarry is it? I am almost certain. Could this
    not simply be:

    Control c2 = LoadControl("My Control.ascx");
    form1.Controls. Add(c2);

  • Anthony Jones

    #2
    Re: Is this cast necesarry?


    "Marc" <noreply@chello .nlwrote in message
    news:OOoB33wAJH A.4372@TK2MSFTN GP05.phx.gbl...
    Hi, reading a book on .NET I see the following code.
    >
    // MyControl.ascx contains a user control
    MyControl c2 = (MyControl)Load Control("MyCont rol.ascx");
    form1.Controls. Add(c2);
    >
    I do not think this cast is necesarry is it? I am almost certain. Could
    this
    not simply be:
    >
    Control c2 = LoadControl("My Control.ascx");
    form1.Controls. Add(c2);
    >
    Looks good to me, did you try it?

    --
    Anthony Jones - MVP ASP/ASP.NET



    Comment

    • Marc

      #3
      Re: Is this cast necesarry?


      "Anthony Jones" <Ant@yadayadaya da.comschreef
      Looks good to me, did you try it?
      Not yet, I am just reading the chapter now. I will try something later.
      Thanks for the quick answer, I just wondered while studying.

      Comment

      • =?ISO-8859-1?Q?G=F6ran_Andersson?=

        #4
        Re: Is this cast necesarry?

        Marc wrote:
        Hi, reading a book on .NET I see the following code.
        >
        // MyControl.ascx contains a user control
        MyControl c2 = (MyControl)Load Control("MyCont rol.ascx");
        form1.Controls. Add(c2);
        >
        I do not think this cast is necesarry is it? I am almost certain. Could
        this not simply be:
        >
        Control c2 = LoadControl("My Control.ascx");
        form1.Controls. Add(c2);
        Yes, that would work if all that you want to do is to add the control to
        the page. If you want to access any members that are specific to the
        user control, you have to cast it first.

        --
        Göran Andersson
        _____
        Göran Anderssons privata hemsida.

        Comment

        • Scott M.

          #5
          Re: Is this cast necesarry?

          If you don't do the cast, you will only be able to use the usercontrol as a
          generic "usercontro l" and not your specific type of usercontrol.

          If you had added any additional class members or overrode any members,
          you'll have to cast it to be able to use those members.

          -Scott


          "Marc" <noreply@chello .nlwrote in message
          news:OOoB33wAJH A.4372@TK2MSFTN GP05.phx.gbl...
          Hi, reading a book on .NET I see the following code.
          >
          // MyControl.ascx contains a user control
          MyControl c2 = (MyControl)Load Control("MyCont rol.ascx");
          form1.Controls. Add(c2);
          >
          I do not think this cast is necesarry is it? I am almost certain. Could
          this not simply be:
          >
          Control c2 = LoadControl("My Control.ascx");
          form1.Controls. Add(c2);

          Comment

          Working...