473,326 Members | 2,013 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,326 software developers and data experts.

Is there a way to use a variable from a function in the whole form ?

I'm making the game four on a row (with oop)
On the form the player has to indicate if he is playing against a Person or to the computer.
(this is done bij indicating the right radiobutton)

Then in the code it will controle this with an if(...)
Expand|Select|Wrap|Line Numbers
  1. if (rdbSpeler.Checked == true)
  2.   { SpelTegenSpeler(); 
  3.   rdbCpu.Visible = false;}
  4. else
  5. { SpelTegenSpeler(); rdbSpeler.Visible = false;  }
If the second player had to be a person the form calls the function --> MakeHumanPlayer

Expand|Select|Wrap|Line Numbers
  1. public void SpelTegenSpeler()
  2.         {
  3.             Speler Speler2 = new Speler(2, Color.Yellow, false);
  4.             //Naam laten invoeren van de speler
  5.             frmInputbox InputboxNaam2 = new frmInputbox();
  6.             InputboxNaam2.ShowDialog();
  7.             //Naam gelijk stellen aan het ingestelde
  8.             Speler2.Naam = InputboxNaam2.naam;
  9.             InputboxNaam2.Dispose();
  10.  }
(Speler means Player)

And to play against a cpuPlayer i call this function

Expand|Select|Wrap|Line Numbers
  1.  public void SpelTegenComputer()
  2.         { ComputerSpeler Speler2 = new ComputerSpeler(2,Color.Yellow,false);
  3.             //Naam toewijzen
  4.             Speler2.Naam = "[BOT]Speler2";}
But i cant use the made player in the rest of the form ...

If anyone can help to solve this, i would be really glad.
i'm kind of new to the language .
Mar 15 '11 #1
2 1829
GaryTexmo
1,501 Expert 1GB
If I'm reading this right, you want to define a computer or human player in response to an input, then be able to use that elsewhere in your class?

I think what you need to read up on here is a Class Member. Define your player at the class level, then any method in that class can see it.

The next part is just me suggesting something that you might find helpful... perhaps consider using a base class or even an interface to define your player, then use that to define new classes. In this way you can reduce the complexity of your program, avoiding checking whether or not the player is human or a computer. Here's an example...

Expand|Select|Wrap|Line Numbers
  1. public interface IPlayer
  2. {
  3.   string PlayerName;
  4.   void TakeTurn();
  5. }
  6.  
  7. public class HumanPlayer : IPlayer
  8. {
  9.   // whatever logic you need, implement TakeTurn in such a way that it waits for input and responds to that input
  10. }
  11.  
  12. public class ComputerPlayer : IPlayer
  13. {
  14.   // whatever logic you need, implement TakeTurn in such a way that it plays automatically 
  15. }
Now in your main game class, you can do something like this...

Expand|Select|Wrap|Line Numbers
  1. public class MainGame
  2. {
  3.   private List<IPlayer> m_players = new List<IPlayer>();
  4.   private int m_activePlayer = 0;
  5.  
  6.   ...
  7.  
  8.   public CreatePlayer(bool isComputer, ...)
  9.   {
  10.     IPlayer newPlayer;
  11.     if (isComputer)
  12.       newPlayer = new ComputerPlayer(...);
  13.     else
  14.       newPlayer = new HumanPlayer(...);
  15.   }
  16.  
  17.   ...
  18.  
  19.   public void LetPlayerTakeTurn()
  20.   {
  21.     m_players[m_activePlayer].TakeTurn();
  22.   }
  23. }
Or something to that effect... that way you don't have if statements everywhere to see what kind of player you're looking at. Additionally, you can code the logic entirely within each type of player.

I hope that helps!
Mar 15 '11 #2
Thank you for your respond !
I found the answer.

I had to declare the 2 players at the top like this
Expand|Select|Wrap|Line Numbers
  1.  public partial class frmVieropeenrij : Form
  2.     {
  3.         byte NieuwSpel;
  4.         Spel GestartSpel = new Spel();
  5.         Graphics spelbord;
  6.  
  7.         private Speler Speler1;
  8.         private Speler Speler2;
  9.  
  10. // rest of code
and then i made my functions like this

Expand|Select|Wrap|Line Numbers
  1. private ComputerSpeler SpelTegenComputer()
  2.         {
  3.             ComputerSpeler computerSpeler = new ComputerSpeler(2, Color.Yellow, false);
  4.             //Naam toewijzen
  5.             computerSpeler.Naam = "[BOT]Speler2";
  6.             lblSpeler2Naam.Text = computerSpeler.Naam;
  7.             //Kleur bepalen
  8.             lblSpeler2Naam.ForeColor = computerSpeler.KleurSpeler;
  9.             //Moeilijkheid bepalen
  10.             if (rdbMakkelijk.Checked == true)
  11.             {
  12.                 computerSpeler.Moeilijkheidsgraad = "Makkelijk";
  13.             }
  14.             else if (rdbMoeilijk.Checked == true)
  15.             { computerSpeler.Moeilijkheidsgraad = "Moeilijk"; }
  16.             return computerSpeler;
  17.         }
(makkelijk = easy & moeilijk = hard)

And for a personplayer

Expand|Select|Wrap|Line Numbers
  1.  private PersoonSpeler SpelerAanmaken(int Spelernummer)
  2.         {
  3.             Color color = Color.Red;
  4.             if (Spelernummer != 1) { color = Color.Yellow; }
  5.  
  6.             PersoonSpeler PersoonSpeler = new PersoonSpeler(Spelernummer, color, false);
  7.             //Naam laten invoeren van de speler
  8.             frmInputbox InputboxNaam = new frmInputbox();
  9.             InputboxNaam.ShowDialog();
  10.             //Naam gelijk stellen aan het ingestelde
  11.             PersoonSpeler.Naam = InputboxNaam.naam;
  12.             InputboxNaam.Dispose();
  13.             return PersoonSpeler;
  14.  
  15.         }
(i hope you get it because the names of the variables are in Dutch)

The problem now is that i have to call a method from computerspeler to let the computer play. But it wont.
i get this error:

Error 1 'vier_op_een_rij__2_.Speler' does not contain a definition for 'BepaalZetComputer' and no extension method 'BepaalZetComputer' accepting a first argument of type 'vier_op_een_rij__2_.Speler' could be found (are you missing a using directive or an assembly reference?) L:\vier op een rij\vier op een rij\vier op een rij (2)\vier op een rij (2)\frm4op1rij.cs 226 37 vier op een rij (2)


So i dont know how to fix this :s
ps: (ComputerSpeler inherits Speler ...)
Mar 19 '11 #3

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

Similar topics

2
by: Bob Quintal | last post by:
Using Access 97, with the runtime parameter. I have a an application that needs a filter by form replacement, because that is unimplemented in runtime mode. I call the filter parameters form...
7
by: Access2003Guru | last post by:
Instead of sending an entire form to a client that includes code changes, I'm trying to update just one section of code. If I know the object, function name & property, is there a way to search...
0
by: Eric | last post by:
When I use the scroll up and down button in my form it shows an empty form and some times it save blank record. Is it possible i will lock my form if any one use the scroll button of mouse my form...
1
by: menyki | last post by:
How do i declare a variable in a form if i want to reference to the variable from another form. Below is a code on a form called updatemortalityForm, some variable on this form, i want to make use of...
1
by: avinashdimsmrt | last post by:
Dear sir, Can anyone pls tell me that how can i set a variable in a form of MS Access so that I can get it in a MS Access Report which is based on this variable value.
6
by: gggram2000 | last post by:
I'd like to know how I go about passing two variables from one form to the next. For example, I can pass a variable like this: This demonstrates how to pass a variable to a form when the form is...
7
by: thesti | last post by:
hi, how to access a variable in a form from a different form? i have a DataSet variable in form1 which needs to be accessed by form2. when i type the exact name as the DataSet variable in...
4
by: kkshansid | last post by:
i want to pass 2 variable from one form to another form one is section name which is the column value which is working problem is that i also want another variable culumn name so both variable should...
1
by: arsha123 | last post by:
How can I change the value of empty session variable with another variable value defined in form. I get value of session value of id from a logon form , that is "". I have defined a variable in form...
4
by: Jamen98 | last post by:
I'm trying to pass the "ADA_Member_Factor" variable from on form to another. The first form that puts a value in the variable is below: Option Compare Database Dim ADA_Member_Factor As...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.