4 Replies - 846 Views - Last Post: 13 June 2009 - 05:26 AM Rate Topic: -----

#1 eXcellion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-June 09

Using a variable from one object in another

Post icon  Posted 13 June 2009 - 02:44 AM

So I have this button that uses an array, and I have another on which I'd like to check something of that array after it's been filled a couple of times.

But I can't refer tot that sarray because:

Error 1 The name 'frequentie' does not exist in the current context C:\Users\Pieter\Documents\Visual Studio 2008\Projects\blackjack\blackjack\Form1.cs 99 33 blackjack


Why can't I use that array?

		private void btnCheck_Click(object sender, EventArgs e)
		{

			for (int i = 0; i < 53; i++)
			{
				MessageBox.Show(frequentie[i]);
						}
				}

			


(and as said, it gets used and is declared at a previous object)

This post has been edited by eXcellion: 13 June 2009 - 02:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Using a variable from one object in another

#2 avani@swayamsoftware.com  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 02-June 09

Re: Using a variable from one object in another

Posted 13 June 2009 - 02:54 AM

Quote

declare your array as public in the beginning of the constructor of class.

and then execute your code.

Was This Post Helpful? 0
  • +
  • -

#3 eXcellion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-June 09

Re: Using a variable from one object in another

Posted 13 June 2009 - 03:01 AM

As I have just started programming C# I don't really now how to do that.
(sorry - I just tried googling it but still don't quite get that idea)
Was This Post Helpful? 0
  • +
  • -

#4 avani@swayamsoftware.com  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 02-June 09

Re: Using a variable from one object in another

Posted 13 June 2009 - 03:11 AM

public partial class Form2 : Form
	{
		string[] frequentie;
		public Form2()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			// write your code for array element checking
			for (int i = 0; i < frequentie.Length; i++)
			{
				MessageBox.Show(frequentie[i]);
						}
		}

		private void Form2_Load(object sender, EventArgs e)
		{
			// write your code to initialize and fill the array here
			frequentie = new string[10];
		}
	}
[quote]
Try this. I hope it will be use full to you.

and sorry for my poor English.
[/quote]
 

Was This Post Helpful? 1
  • +
  • -

#5 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Using a variable from one object in another

Posted 13 June 2009 - 05:26 AM

View PosteXcellion, on 13 Jun, 2009 - 04:01 AM, said:

As I have just started programming C# I don't really now how to do that.
(sorry - I just tried googling it but still don't quite get that idea)

Quote

*edit*
To access a variable inside a class it has to be public. You will also need to use the variable you used to create the object it use it.


For example, in your class you will have something like:

string[] frequentie;



First, to access it outside your class it has to be public:

public string[] frequentie;



To access it outside of your class, you would need to use:

MessageBox.Show(objectVariableName.frequentie[i]);



Where objectVariableName would be the name of your variable for the class.

This post has been edited by SixOfEleven: 13 June 2009 - 05:30 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1