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. where i need to declare my array?

where i need to declare my array?

Scheduled Pinned Locked Moved C#
designdata-structureshelpquestionlearning
3 Posts 3 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.
  • K Offline
    K Offline
    kaka760
    wrote on last edited by
    #1

    hello everyone! beginner Q! i try to build an array, by btn click i like to add integer; my problem is every click i get a new array, so where i need to declare my array the code: namespace array { public partial class _Default : System.Web.UI.Page { int counter; int[] arr1 = new int[50]; protected void Page_Load(object sender, EventArgs e) { } protected void btnAdd_Click(object sender, EventArgs e) { int item = int.Parse(TextBox1.Text); addItemToArray(item); counter++; } public void addItemToArray(int item) { arr1[counter] = item; } } } 10x Kaka760

    S V 2 Replies Last reply
    0
    • K kaka760

      hello everyone! beginner Q! i try to build an array, by btn click i like to add integer; my problem is every click i get a new array, so where i need to declare my array the code: namespace array { public partial class _Default : System.Web.UI.Page { int counter; int[] arr1 = new int[50]; protected void Page_Load(object sender, EventArgs e) { } protected void btnAdd_Click(object sender, EventArgs e) { int item = int.Parse(TextBox1.Text); addItemToArray(item); counter++; } public void addItemToArray(int item) { arr1[counter] = item; } } } 10x Kaka760

      S Offline
      S Offline
      Samuel Cherinet
      wrote on last edited by
      #2

      the button is a server side control, so the page reloads everytime you click it, try using java script for the button.

      1 Reply Last reply
      0
      • K kaka760

        hello everyone! beginner Q! i try to build an array, by btn click i like to add integer; my problem is every click i get a new array, so where i need to declare my array the code: namespace array { public partial class _Default : System.Web.UI.Page { int counter; int[] arr1 = new int[50]; protected void Page_Load(object sender, EventArgs e) { } protected void btnAdd_Click(object sender, EventArgs e) { int item = int.Parse(TextBox1.Text); addItemToArray(item); counter++; } public void addItemToArray(int item) { arr1[counter] = item; } } } 10x Kaka760

        V Offline
        V Offline
        Vitaliy Tsvayer
        wrote on last edited by
        #3

        The reason you get new array object on every click, is that new instance of class _Default is created for every request. So you need to put this array object into store which is shared among different requests that is Session.

        Vitaliy Tsvayer Tikle

        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