473,411 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

One class of variables, two sets of variables

My C# program has 3 files of program code:
Form.h
chessPlayer.cs
ColorLevelForm.cs
This line of code:
Expand|Select|Wrap|Line Numbers
  1. ChessPlayer chessGame = new ChessPlayer();
in Form.h allows me to access public variables and arrays in Chessplayer().
That same line of code in ColorLevelForm.cs also allows me to access variables and arrays in Chessplayer(). The problem is that two separate variable sets are created by the code in each file when I run the program. If I store a number in ‘PlyMax’ (a public variable in chessPlayer.cs) with the ColorLevelForm.cs code, That same variable remains unchanged in Form.h! Let me know if you need more detailed info the help me solve this problem.

Thank you
Dan
Sep 8 '11 #1

✓ answered by arie

There is easier solution.

in Form1 (main form) do:
Expand|Select|Wrap|Line Numbers
  1. Form2 frm = new Form2();
  2. frm.ShowDialog(this); 
  3. // or frm.Show(this); if you don't want it to be modal
  4.  
This code makes Form1 instance the Owner of Form2 instance.
Then in Form2 you just do:
Expand|Select|Wrap|Line Numbers
  1. this.Owner.chessGame.PlyMax = 2;
  2.  

7 1767
michaeldebruin
134 100+
If I understand it correctly then this might help, otherwise please give me some more details. I might be able to help.
So is it correct that the file names standing above your code are 3 totally different files and that you want to put 1 variable from Chessplayer.cs and one from ColorLevelForm.cs in the Form.h? If this is the case then try to make a settings file and store the 2 variables in there and store the values in the settings file.
If I am totally wrong, again give me some more details. Also posting your code would help.
Sep 9 '11 #2
Thanks for your reply. I tried your idea of making a settings file. I put the variable ‘PlyMax’ in it. It did not help. I don’t know if I did it right. But I don’t think a settings file is what I need to solve my problem. Here is some code from the three files;

Form1.h :
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace Sargon_C_sharp_2011
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         ChessPlayer chessGame = new ChessPlayer();  //links data and routines in ChessPlayer.cpp to this form
  15.  
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.  
  20.             chessGame.InitBoard(); //Initialize Board[] array...InitBoard() is found in Chessplayer.cpp
  21.                 // ... more code
  22.         }
  23.  
  24.  
  25.  
  26.         private void MseClickSquare(object sender, MouseEventArgs e)
  27.         {
  28.           // code here gets players move and runs the chess engine in chessPlayer()
  29.          }
  30.  
  31.         private void PaintChessBoard(object sender, PaintEventArgs e)
  32.         {   //Paint the chessboard and add chess pieces
  33.  
  34.             // more code
  35.  
  36.                             bc = chessGame.Board[bix++];// gets value from Board[] in chessPlayer.cpp
  37.  
chessPlayer.cpp
Expand|Select|Wrap|Line Numbers
  1. Code from ChessPlayer.cs:
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace Sargon_C_sharp_2011
  9. {
  10.    public class ChessPlayer
  11.     {
  12.         //this has all the data and routines use by the chess engine
  13.  
  14.        public int[] Board = new int[120];  //Internal chess board
  15.        public int PlyMax = 1;  //level of play
  16.  
  17.        //...   Much more code
  18.  
ColorLevelForm2.cpp
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace Sargon_C_sharp_2011
  11. {
  12.     public partial class ColorLevelForm2 : Form
  13.     {
  14.  
  15.         ChessPlayer chessGame = new ChessPlayer();  //this makes different set of variables from Form1
  16.  
  17.  
  18.         public ColorLevelForm2()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.  
  24.         private void LevelRBclicked(object sender, MouseEventArgs e)
  25.         {
  26.            // other code
  27.             if (this.rBLevel2.Checked)  // radio button in the form show when selected with the Menu
  28.             {
  29.                 this.chessGame.PlyMax = 2; //Does not change this variable in Form1
  30.                 return;
  31.             }
  32.  
These two comment lines in ColorLevelForm2.cpp:
//this makes different set of variables from Form1
//Does not change this variable in Form1
Sum up the problem.

Any ideas as to what code I need to add?
Sep 10 '11 #3
michaeldebruin
134 100+
Oke what are you trying to make? And what is the code which sum up the problem supposed to do?
Sep 11 '11 #4
Thank you again for responding. Here is the answer to that question as best I can.

When the user goes to the menu there is a choice called 'Color/Level'. This brings up a little window with radio buttons. When the user clicks on a radio button, the code in 'Colorform2.cpp' is activated.
Here three variables can be set. On of which is 'PlyMax' This determines the level of play. In 'chessPlayer.cpp' where the heart of to chess program is, there is a line of code:
Expand|Select|Wrap|Line Numbers
  1. public int PlyMax = 1;  //level of play
Form1.cpp can change this variable:
Expand|Select|Wrap|Line Numbers
  1. chessGame.PlyMax = i;  // 'i' is an integer between 1 and 8
But when 'Colorform2.cpp' tries to do the same thing:
Expand|Select|Wrap|Line Numbers
  1. chessGame.PlyMax = 2 // = 2 or any integer between 1 and 8
a different memory location gets changed.

This line of code in 'Colorform2.cpp'
Expand|Select|Wrap|Line Numbers
  1.         ChessPlayer chessGame = new ChessPlayer();  //this .....
sets a side a different(new) set of memory locations for chessPlayer.cpp which is useless.

I need a line or two of code somewhere to allow 'Colorform2.cpp' to get to same set of memory locations (for important variables) as the other two files of code.

Got any ideas?
Dan
Sep 11 '11 #5
I though you mike like to know that I found a solution to my problem.
This form/website:
http://forum.codecall.net/c-programm...hild-form.html
gave me the solution. In my program Form1 is the Parent and Colorlevelform2 is the child.
Sep 14 '11 #6
arie
64
There is easier solution.

in Form1 (main form) do:
Expand|Select|Wrap|Line Numbers
  1. Form2 frm = new Form2();
  2. frm.ShowDialog(this); 
  3. // or frm.Show(this); if you don't want it to be modal
  4.  
This code makes Form1 instance the Owner of Form2 instance.
Then in Form2 you just do:
Expand|Select|Wrap|Line Numbers
  1. this.Owner.chessGame.PlyMax = 2;
  2.  
Sep 15 '11 #7
Bye gosh your are right. Your Idea piggybacks on top of the first Idea. I needed to add public to:
Expand|Select|Wrap|Line Numbers
  1.  public ChessPlayer chessGame = new ChessPlayer();
in Form1
and in ColorLeverForm2:
Expand|Select|Wrap|Line Numbers
  1. this.cHandle.chessGame.PlyMax = 2;
line of code will work.

But

By doing it this way:
In ColorLevelForm2.cs I have:
Expand|Select|Wrap|Line Numbers
  1.  if (this.rBLevel3.Checked)
  2.             {
  3.                 cHandle.LevelPub = 3;
  4.                 cHandle.LinkLevel();
  5.                 return;
  6.             }
  7.  
and in Form1.cs
I have:
Expand|Select|Wrap|Line Numbers
  1.  public void LinkLevel()
  2.         {
  3.             string Leveltextstr = "Level: ";
  4.             chessGame.PlyMax = LevelPub;
  5.             LevelLabel.Text = Leveltextstr + (char)('0' + LevelPub);     
  6.         }
  7.  
This allows me to execute a few lines of code that are much easier to execute in Form1.cs

PS in some places I have used .cpp when I should have used .cs

Anyway, Thank you for your help
Dan Webb
Sep 15 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Cory Bosma | last post by:
Looked all over for the answer to this but can't find it. Maybe I'm searching for the wrong thing. I'm new to using classes in PHP. Whenever I try and reference the member's ($line) properties...
2
by: Wensheng | last post by:
I have a py program below: -------------------------------- class someclass: def __init__(self): self.var = "hell, world" def printitout(self): execfile("printit.py") #__import__("printit")...
1
by: mark4asp | last post by:
What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include that file on every page. This is the best way of...
6
by: ravi_shankar | last post by:
hi all. what's the advantage of prefixing a varible with "extern" storage class specifier other than its default initializion . you can mail to me : ravi_shankarprasad@rediffmail.com --...
1
by: Qingdong Z. | last post by:
Posted in asp.net newsgroup, no answer. Hope some experts can help me here 1. Does class shared variable share same feathers as ASP.NET application variable? 2. If it is, it may work better...
0
by: JackRazz | last post by:
Is it possible to serialize a static class containing shared variables like I can serialize an instance of a normal class (obj)? I had a static class, but I gave up and converted it into a normal...
12
by: jimfortune | last post by:
I have a question based somewhat on: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/ddde992b84f762bd/152bbc027bf00720?hl=en#152bbc027bf00720 A local table works well...
2
by: cdg | last post by:
Is there any way to directly access variables of one class from another class without passing any public member variables. Similar to how any function in a class can directly access (without...
5
by: Frank | last post by:
Hey Everyone, Lets say I have a variable declared as such: var newVariable="opener"; How can I then use this variable to access the opener of 'window' ? Meaning, I dont want to access it...
0
by: Joe Strout | last post by:
On Nov 10, 2008, at 2:44 PM, Zane Selvans wrote: How are you creating your list? You need to make sure that each instance creates its very own list object, something like: self.foo = ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.