3 Replies - 710 Views - Last Post: 05 July 2011 - 07:53 PM Rate Topic: -----

#1 cablenewt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-January 11

Unable to access Object variables inside another method.

Posted 05 July 2011 - 07:44 PM

I have this class and I'm unable to access the man[] array's variables from the Radiobutton event method. How do I go about accessing the man's Name so I can update the label7 text? Thank you.

This code specifically:
 private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                label7.Text = man[0].Name; // Man[0] does not seem to exist from within this method.
            }
            
        }




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ADayAtTheraces
{
    public partial class Form1 : Form
    {
        

        
        
        public Form1(){

            
 InitializeComponent(); 

            Guy[] man = new Guy[3];
            man[0] = new Guy() { Name = "Joe", Cash = 50, MyBet = null, MyRadioButton = radioButton1 , MyLabel = label4};

            man[1] = new Guy() { Name = "Bob", Cash = 75, MyBet = null, MyRadioButton = radioButton2, MyLabel = label5};
            man[2] = new Guy() { Name = "Al", Cash = 45, MyBet = null, MyRadioButton = radioButton3, MyLabel = label6 };
            for (int i = 0; i < man.Length; i++)
            {
               
                man[i].UpdateLabels();
                
            }
            {
                
            }
           Greyhound[] dog = new Greyhound[4];
           dog[0] = new Greyhound() { MyPictureBox = pictureBox2, Location = pictureBox2.Location.X, RacetrackLength = racetrack.Size.Width};
           dog[1] = new Greyhound() { MyPictureBox = pictureBox3, Location = pictureBox2.Location.X, RacetrackLength = racetrack.Size.Width };
           dog[2] = new Greyhound() { MyPictureBox = pictureBox4, Location = pictureBox2.Location.X, RacetrackLength = racetrack.Size.Width };
           dog[3] = new Greyhound() { MyPictureBox = pictureBox5, Location = pictureBox2.Location.X, RacetrackLength = racetrack.Size.Width };

            
            
           
            
        }

        



        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                label7.Text = man[0].Name;
            }
            
        }

        
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Unable to access Object variables inside another method.

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1511
  • View blog
  • Posts: 5,916
  • Joined: 21-March 08

Re: Unable to access Object variables inside another method.

Posted 05 July 2011 - 07:48 PM

This code...

Guy[] man = new Guy[3];



is in the constructor. Therefore, it's scope is limited to the constructor only. It can't be used outside of the constructor.

Move that code to outside of the constructor(and all other methods/events), and it will work.
Was This Post Helpful? 3
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3876
  • View blog
  • Posts: 11,415
  • Joined: 18-April 07

Re: Unable to access Object variables inside another method.

Posted 05 July 2011 - 07:50 PM

You can define the "man" array inside the Form class, but outside of any functions. In your constructor you can then initialize it...

public partial class Form1 : Form
{
        
    // Define Man here
    private Guy[] man;
        
        
    public Form1(){
        // Initialize man here
        man = new Guy[3];
        man[0] = new Guy() { Name = "Joe", Cash = 50, MyBet = null, MyRadioButton = radioButton1 , MyLabel = label4};

    }

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
            if (radioButton1.Checked)
            {
                label7.Text = man[0].Name; // Can now see it since it is a form level variable
            }
    }



Notice that man was defined as a class level variable, we initialized it in the constructor and now events will be able to see it.

:)
Was This Post Helpful? 3
  • +
  • -

#4 cablenewt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-January 11

Re: Unable to access Object variables inside another method.

Posted 05 July 2011 - 07:53 PM

Thank you both very much, it's so simple now that you've explained it. Once again thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1