Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Re-use code

Re-use code

Scheduled Pinned Locked Moved C#
helptutorialquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    simsen
    wrote on last edited by
    #1

    Hi, I have the below code in a cs file (and it function perfect). But I know, I have to use that code in other files two. So I want to place the code in a Class file (cs file in App_Code dir). I don't know what I shall do, so I can re-use the code in all the files I want. Can anyone show by example what to do - and how I use ind a file? My code: public String sortExpression { get { if (ViewState["sortExpression"] == null) { ViewState["sortExpression"] = Convert.ToString(grdEmployeeList.Columns[0].SortExpression); } return Convert.ToString(ViewState["sortExpression"]); } set { ViewState["sortExpression"] = value; } } If I put the code directly into the Class file, I get an error, that ViewState is unknown..... Kind regards, simsen :-)

    H K E 3 Replies Last reply
    0
    • S simsen

      Hi, I have the below code in a cs file (and it function perfect). But I know, I have to use that code in other files two. So I want to place the code in a Class file (cs file in App_Code dir). I don't know what I shall do, so I can re-use the code in all the files I want. Can anyone show by example what to do - and how I use ind a file? My code: public String sortExpression { get { if (ViewState["sortExpression"] == null) { ViewState["sortExpression"] = Convert.ToString(grdEmployeeList.Columns[0].SortExpression); } return Convert.ToString(ViewState["sortExpression"]); } set { ViewState["sortExpression"] = value; } } If I put the code directly into the Class file, I get an error, that ViewState is unknown..... Kind regards, simsen :-)

      H Offline
      H Offline
      half life
      wrote on last edited by
      #2

      I don't know what is a ViewState But i know it belongs to refrence( using ...) Maybe u forgat it.

      Have Fun Never forget it

      1 Reply Last reply
      0
      • S simsen

        Hi, I have the below code in a cs file (and it function perfect). But I know, I have to use that code in other files two. So I want to place the code in a Class file (cs file in App_Code dir). I don't know what I shall do, so I can re-use the code in all the files I want. Can anyone show by example what to do - and how I use ind a file? My code: public String sortExpression { get { if (ViewState["sortExpression"] == null) { ViewState["sortExpression"] = Convert.ToString(grdEmployeeList.Columns[0].SortExpression); } return Convert.ToString(ViewState["sortExpression"]); } set { ViewState["sortExpression"] = value; } } If I put the code directly into the Class file, I get an error, that ViewState is unknown..... Kind regards, simsen :-)

        K Offline
        K Offline
        Kristian Sixhoj
        wrote on last edited by
        #3

        You need to add a using statement at the top of your new program, that refers to the namespace which ViewState belongs to.

        Kristian Sixhoej


        "Any fool can learn from his own mistakes, but a wise man learns from mistakes of others"

        S 1 Reply Last reply
        0
        • S simsen

          Hi, I have the below code in a cs file (and it function perfect). But I know, I have to use that code in other files two. So I want to place the code in a Class file (cs file in App_Code dir). I don't know what I shall do, so I can re-use the code in all the files I want. Can anyone show by example what to do - and how I use ind a file? My code: public String sortExpression { get { if (ViewState["sortExpression"] == null) { ViewState["sortExpression"] = Convert.ToString(grdEmployeeList.Columns[0].SortExpression); } return Convert.ToString(ViewState["sortExpression"]); } set { ViewState["sortExpression"] = value; } } If I put the code directly into the Class file, I get an error, that ViewState is unknown..... Kind regards, simsen :-)

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #4
          1. ViewState is serialized and can contain serializable types. Do not convert to string. Use: ViewState["w/e"] = someObject directly. 2) Create a base page that inherits from page then you can reuse the code.

          Need a C# Consultant? I'm available.
          Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

          S 1 Reply Last reply
          0
          • K Kristian Sixhoj

            You need to add a using statement at the top of your new program, that refers to the namespace which ViewState belongs to.

            Kristian Sixhoej


            "Any fool can learn from his own mistakes, but a wise man learns from mistakes of others"

            S Offline
            S Offline
            simsen
            wrote on last edited by
            #5

            Hi Kristian, Microsoft sais that the namespace are UI, and I have that namespace in the using statements. So I have no clue, what to do? using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; /// /// Summary description for ERSGridView /// public class ERSGridView {

            1 Reply Last reply
            0
            • E Ennis Ray Lynch Jr
              1. ViewState is serialized and can contain serializable types. Do not convert to string. Use: ViewState["w/e"] = someObject directly. 2) Create a base page that inherits from page then you can reuse the code.

              Need a C# Consultant? I'm available.
              Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

              S Offline
              S Offline
              simsen
              wrote on last edited by
              #6

              Hi Ennis, How do I create a base page that inherits from page. I now tried to search at google, and I cann't figure out to replace the expamples with my code?

              E 1 Reply Last reply
              0
              • S simsen

                Hi Ennis, How do I create a base page that inherits from page. I now tried to search at google, and I cann't figure out to replace the expamples with my code?

                E Offline
                E Offline
                Ennis Ray Lynch Jr
                wrote on last edited by
                #7

                in App_Code:

                public class MyPageBase : System.Web.UI.Page {

                public string ViewStateItem{
                    get{
                        return (string)ViewState\["someViewStateItem"\];
                    }
                    set{
                        ViewState\["someViewStateItem"\] = value;
                    }
                }
                

                }

                In your Web project

                public class SomeWebPage : MyPageBase { ... }


                Need a C# Consultant? I'm available.
                Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

                S 1 Reply Last reply
                0
                • E Ennis Ray Lynch Jr

                  in App_Code:

                  public class MyPageBase : System.Web.UI.Page {

                  public string ViewStateItem{
                      get{
                          return (string)ViewState\["someViewStateItem"\];
                      }
                      set{
                          ViewState\["someViewStateItem"\] = value;
                      }
                  }
                  

                  }

                  In your Web project

                  public class SomeWebPage : MyPageBase { ... }


                  Need a C# Consultant? I'm available.
                  Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

                  S Offline
                  S Offline
                  simsen
                  wrote on last edited by
                  #8

                  I copied the same code into a new file I called ERSBasePage and I gets the same error here to..... my ERSBasePage.cs file: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// /// Summary description for ERSPageBase /// public class ERSPageBase { public ERSPageBase() { // // TODO: Add constructor logic here // } public string ViewStateItem { get { return (string)ViewState["someViewStateItem"]; } set { ViewState["someViewStateItem"] = value; } } } And the page where I want to inherith: public partial class ERS_Adm_AdmFrontPage : ERSPageBase { protected void Page_Load(object sender, EventArgs e) { The error sais: Error 1 The name 'ViewState' does not exist in the current context C:\Inetpub\wwwroot\ANSI\App_Code\ERSPageBase.cs 24 30 C:\...\ANSI\ As you can see, I am using the page directive UI as I read I shall use regarding ViewState

                  E 1 Reply Last reply
                  0
                  • S simsen

                    I copied the same code into a new file I called ERSBasePage and I gets the same error here to..... my ERSBasePage.cs file: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// /// Summary description for ERSPageBase /// public class ERSPageBase { public ERSPageBase() { // // TODO: Add constructor logic here // } public string ViewStateItem { get { return (string)ViewState["someViewStateItem"]; } set { ViewState["someViewStateItem"] = value; } } } And the page where I want to inherith: public partial class ERS_Adm_AdmFrontPage : ERSPageBase { protected void Page_Load(object sender, EventArgs e) { The error sais: Error 1 The name 'ViewState' does not exist in the current context C:\Inetpub\wwwroot\ANSI\App_Code\ERSPageBase.cs 24 30 C:\...\ANSI\ As you can see, I am using the page directive UI as I read I shall use regarding ViewState

                    E Offline
                    E Offline
                    Ennis Ray Lynch Jr
                    wrote on last edited by
                    #9

                    Look really close at your definition of ERSPageBase. There is something missing that is not missing from my sample.


                    Need a C# Consultant? I'm available.
                    Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups