Can I write as follow?

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

    #1

    Can I write as follow?

    interface IWebPage
    {
    class SessionStructur e {};
    void SaveSession();
    void LoadSession();
    }

    public class BasePage : IWebPage
    {
    class SessionStructur e
    {
    int a;
    int b;
    }

    void SaveSession() { ............. };
    void LoadSession() { ............. };
    }


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Can I write as follow?

    Well, there are a few problems here.

    The first is that you should make the Save and LoadSession methods on
    the BasePage class virtual, so that they can be overridden by derived
    classes.

    However, you can not extend the SessionStructur e class like that. You
    would have to create a new type.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "ABC" <abc@abc.com> wrote in message
    news:Ohw$bryFGH A.3900@TK2MSFTN GP10.phx.gbl...[color=blue]
    > interface IWebPage
    > {
    > class SessionStructur e {};
    > void SaveSession();
    > void LoadSession();
    > }
    >
    > public class BasePage : IWebPage
    > {
    > class SessionStructur e
    > {
    > int a;
    > int b;
    > }
    >
    > void SaveSession() { ............. };
    > void LoadSession() { ............. };
    > }
    >[/color]


    Comment

    • KJ

      #3
      Re: Can I write as follow?

      No. As the compiler will tell you, interfaces cannot declare types.
      Perhaps you might share why you want to do this, so alternatives may be
      offered.

      Comment

      • ABC

        #4
        Re: Can I write as follow?

        What alternatives can do similar case?


        "KJ" <n_o_s_p_a__m@m ail.com> wrote in message
        news:1137039159 .803066.174180@ g44g2000cwa.goo glegroups.com.. .[color=blue]
        > No. As the compiler will tell you, interfaces cannot declare types.
        > Perhaps you might share why you want to do this, so alternatives may be
        > offered.
        >[/color]


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Can I write as follow?

          ABC <abc@abc.com> wrote:[color=blue]
          > What alternatives can do similar case?[/color]

          We need to know what you're actually trying to do before we can answer
          that. It's not at all obvious how you expected to be able to use that
          interface.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • Mark Rae

            #6
            Re: Can I write as follow?

            "ABC" <abc@abc.com> wrote in message
            news:ObD1RAzFGH A.344@TK2MSFTNG P11.phx.gbl...
            [color=blue]
            > What alternatives can do similar case?[/color]

            What are you actually trying to do...?


            Comment

            • ABC

              #7
              Re: Can I write as follow?

              as each webpage has difference session structure to hold data, but each
              webpage have similar behaviors to load and save session data to sessions
              pool. I want to standardize the programmers coding, so the interface should
              have a base session class and programmers should do some codes to add
              properties to session structures, all programmers must use same session
              structure name. I don't like they have own programming styles.





              "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
              news:MPG.1e300a bf1f4ede7898ccc 0@msnews.micros oft.com...[color=blue]
              > ABC <abc@abc.com> wrote:[color=green]
              >> What alternatives can do similar case?[/color]
              >
              > We need to know what you're actually trying to do before we can answer
              > that. It's not at all obvious how you expected to be able to use that
              > interface.
              >
              > --
              > Jon Skeet - <skeet@pobox.co m>
              > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              > If replying to the group, please do not mail me too[/color]


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Can I write as follow?

                ABC <abc@abc.com> wrote:[color=blue]
                > as each webpage has difference session structure to hold data, but each
                > webpage have similar behaviors to load and save session data to sessions
                > pool. I want to standardize the programmers coding, so the interface should
                > have a base session class and programmers should do some codes to add
                > properties to session structures, all programmers must use same session
                > structure name. I don't like they have own programming styles.[/color]

                No, you can't enforce naming like this. (Nor would the interface be
                expressing anything you could actually *use* given only a reference to
                an instance of the interface.) I can't see how it's agood idea though
                anyway - it would make things very awkward if you had two web pages
                which actually needed the same session structure. They'd have to
                declare the same thing twice, which is bad practice.

                --
                Jon Skeet - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                • KJ

                  #9
                  Re: Can I write as follow?

                  Have you considered using inheritance, or a combination of inheritance
                  and delegation?

                  Comment

                  Working...