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. Problem by using an Object and a variable inside another function [modified]

Problem by using an Object and a variable inside another function [modified]

Scheduled Pinned Locked Moved C#
helpquestion
3 Posts 2 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.
  • N Offline
    N Offline
    nstk
    wrote on last edited by
    #1

    Hello, trying to build a Windows programm, I 've declared an object in Fornm1.cs by typing

    Person[] P = new Person[10];

    after a class Person I 've created on another file called Person.cs Then on the function that is being activated after clicking on a button inside the Form I pass simple numerical values to a member attribute like below:

        private void btnObjectAdd\_Click(object sender, EventArgs e)
        {
            lbltest\_i.Text = i.ToString();
            P\[i\] = new Person();
            P\[i\].category = i;
            i++;
            if (i == 10)
            {
                for (i = 0; i < 10; i++)
                    P\[i\] = null;
                    i = 0;
            }
         }
    

    Thus I get P[0]=0, P[1]=1, ... P[9]=9. Then I try to call a value of an object on another function like

        private void showToolStripMenuItem\_Click(object sender, EventArgs e)
    {
       int value = P\[2\].category;
    }
    

    and I get value = 2. This works perfect. But if I change

    int value = P[2].category;

    into

    int value = P[i].category;

    then I get an error that I should create an object with 'new'. Variable i is declared as

    int i = 0;

    outside the functions but inside the public partial class Form1 : Form Is that the problem?

    modified on Monday, February 7, 2011 1:14 PM

    M 1 Reply Last reply
    0
    • N nstk

      Hello, trying to build a Windows programm, I 've declared an object in Fornm1.cs by typing

      Person[] P = new Person[10];

      after a class Person I 've created on another file called Person.cs Then on the function that is being activated after clicking on a button inside the Form I pass simple numerical values to a member attribute like below:

          private void btnObjectAdd\_Click(object sender, EventArgs e)
          {
              lbltest\_i.Text = i.ToString();
              P\[i\] = new Person();
              P\[i\].category = i;
              i++;
              if (i == 10)
              {
                  for (i = 0; i < 10; i++)
                      P\[i\] = null;
                      i = 0;
              }
           }
      

      Thus I get P[0]=0, P[1]=1, ... P[9]=9. Then I try to call a value of an object on another function like

          private void showToolStripMenuItem\_Click(object sender, EventArgs e)
      {
         int value = P\[2\].category;
      }
      

      and I get value = 2. This works perfect. But if I change

      int value = P[2].category;

      into

      int value = P[i].category;

      then I get an error that I should create an object with 'new'. Variable i is declared as

      int i = 0;

      outside the functions but inside the public partial class Form1 : Form Is that the problem?

      modified on Monday, February 7, 2011 1:14 PM

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      There seems to be something very wrong with your design. But if "i" is declared in the same class as your showToolStripMenuItem_Click code then there should be no reason why that is the problem. Perhaps you need to post more of your code - and please you the PRE tags to format is more readably.

      return 5;

      N 1 Reply Last reply
      0
      • M musefan

        There seems to be something very wrong with your design. But if "i" is declared in the same class as your showToolStripMenuItem_Click code then there should be no reason why that is the problem. Perhaps you need to post more of your code - and please you the PRE tags to format is more readably.

        return 5;

        N Offline
        N Offline
        nstk
        wrote on last edited by
        #3

        Thanks for the 'pre' tip, i was looking for s'thing like that. I got rid of the confusing 0 by changing the array into

        new Person[11]

        instead of a 10 dimension. After all, we 've got plenty of memory nowdays :-\ As a result everything worked fine! It must have been some other mistake, as the one I thought of.

        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