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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Help needed with a complicated application

Help needed with a complicated application

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

    Is there a way of calling a method but not calling the first line of the method? I have a generic List called PlayHand. I add to this list in the method PlayGame(). When the button is clicked I want to add a Card type to this list. But after the method PlayGame() has been called and the method has completed, the generic list PlayHand clears. Can anyone help? Im really stuck on this! Lucy

    D C 2 Replies Last reply
    0
    • L Lucy

      Is there a way of calling a method but not calling the first line of the method? I have a generic List called PlayHand. I add to this list in the method PlayGame(). When the button is clicked I want to add a Card type to this list. But after the method PlayGame() has been called and the method has completed, the generic list PlayHand clears. Can anyone help? Im really stuck on this! Lucy

      D Offline
      D Offline
      DavidNohejl
      wrote on last edited by
      #2

      Lucy_H85 wrote:

      Is there a way of calling a method but not calling the first line of the method?

      I don't think so. What you can do is add boolean parameter to that method, and have condition on first line, something like

      void MyMethod(bool shouldExecuteFirstLine)
      {
        if(shouldExecuteFirstLine)
        {
         ...
        }
         /* rest of method body */
      }
      

      Lucy_H85 wrote:

      I have a generic List called PlayHand. I add to this list in the method PlayGame(). When the button is clicked I want to add a Card type to this list. But after the method PlayGame() has been called and the method has completed, the generic list PlayHand clears.

      Uh, I don't follow :( Can you show your code?


      [My Blog]
      "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
      "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

      L 1 Reply Last reply
      0
      • L Lucy

        Is there a way of calling a method but not calling the first line of the method? I have a generic List called PlayHand. I add to this list in the method PlayGame(). When the button is clicked I want to add a Card type to this list. But after the method PlayGame() has been called and the method has completed, the generic list PlayHand clears. Can anyone help? Im really stuck on this! Lucy

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        Sounds like your problem is tha the list is created inside the method and so has no state. Is it a member ? If it is, then it's Clear method is being called somewhere. What is this first line you're trying to avoid ( it can't be done without the approach someone else offered ) ?

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        1 Reply Last reply
        0
        • D DavidNohejl

          Lucy_H85 wrote:

          Is there a way of calling a method but not calling the first line of the method?

          I don't think so. What you can do is add boolean parameter to that method, and have condition on first line, something like

          void MyMethod(bool shouldExecuteFirstLine)
          {
            if(shouldExecuteFirstLine)
            {
             ...
            }
             /* rest of method body */
          }
          

          Lucy_H85 wrote:

          I have a generic List called PlayHand. I add to this list in the method PlayGame(). When the button is clicked I want to add a Card type to this list. But after the method PlayGame() has been called and the method has completed, the generic list PlayHand clears.

          Uh, I don't follow :( Can you show your code?


          [My Blog]
          "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
          "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

          L Offline
          L Offline
          Lucy
          wrote on last edited by
          #4

          Sorry I am very new to C# and I am trying alter a console application example from a book to a windows form to test my skills and how much I have learnt. I think I haven't learnt to much and I have taken on a huge task. I have this method PlayGame() public void PlayGame() { Player[] players = new Player[1]; for (int p = 0; p < players.Length; p++) { string playerName = listBox1.SelectedItem.ToString(); players[p] = new Player(playerName); } if (players == null) return; for (int p = 0; p < players.Length; p++) { for (int c = 0; c < 7; c++) { players[p].PlayHand.Add(playDeck.GetCard(currentCard++)); } } //int currentPlayer; Card playCard = playDeck.GetCard(currentCard++); discardedCards.Add(playCard); for (currentPlayer = 0; currentPlayer < players.Length; currentPlayer++) { label1.Text = players[currentPlayer].Name + "'s turn."; labelHiden.Text = Convert.ToString(currentPlayer); label2hiden.Text = Convert.ToString(players[currentPlayer]); foreach (Card card in players[currentPlayer].PlayHand) { listBox2.Items.Add(Convert.ToString(card)); //richTextBox1.Text = Convert.ToString(card); } label3.Text = "Card in play: " + playCard; if (discardedCards.Contains(playCard)) { discardedCards.Remove(playCard); } players[currentPlayer].PlayHand.Add(playCard); label1.Text = "Drawn: " + playCard; label1.Text = "New Hand:"; for (int i = 0; i < players[currentPlayer].PlayHand.Count; i++) { listBox2.Items.Add(i + 1 + ": " + players[currentPlayer].PlayHand[i]); //richTextBox1.Text = i + 1 + ": " + players[currentPlayer].PlayHand[i]; } label3.Text = "Choose card to discard:"; } current = playCard; //return; } I only want this section of code to execute if a button is clicked: if (discardedCards.Contains(playCard))

          C 1 Reply Last reply
          0
          • L Lucy

            Sorry I am very new to C# and I am trying alter a console application example from a book to a windows form to test my skills and how much I have learnt. I think I haven't learnt to much and I have taken on a huge task. I have this method PlayGame() public void PlayGame() { Player[] players = new Player[1]; for (int p = 0; p < players.Length; p++) { string playerName = listBox1.SelectedItem.ToString(); players[p] = new Player(playerName); } if (players == null) return; for (int p = 0; p < players.Length; p++) { for (int c = 0; c < 7; c++) { players[p].PlayHand.Add(playDeck.GetCard(currentCard++)); } } //int currentPlayer; Card playCard = playDeck.GetCard(currentCard++); discardedCards.Add(playCard); for (currentPlayer = 0; currentPlayer < players.Length; currentPlayer++) { label1.Text = players[currentPlayer].Name + "'s turn."; labelHiden.Text = Convert.ToString(currentPlayer); label2hiden.Text = Convert.ToString(players[currentPlayer]); foreach (Card card in players[currentPlayer].PlayHand) { listBox2.Items.Add(Convert.ToString(card)); //richTextBox1.Text = Convert.ToString(card); } label3.Text = "Card in play: " + playCard; if (discardedCards.Contains(playCard)) { discardedCards.Remove(playCard); } players[currentPlayer].PlayHand.Add(playCard); label1.Text = "Drawn: " + playCard; label1.Text = "New Hand:"; for (int i = 0; i < players[currentPlayer].PlayHand.Count; i++) { listBox2.Items.Add(i + 1 + ": " + players[currentPlayer].PlayHand[i]); //richTextBox1.Text = i + 1 + ": " + players[currentPlayer].PlayHand[i]; } label3.Text = "Choose card to discard:"; } current = playCard; //return; } I only want this section of code to execute if a button is clicked: if (discardedCards.Contains(playCard))

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            Lucy_H85 wrote:

            Player[] players = new Player[1];

            As I thought, this creates a new local variable. Turn it into a member if you want to reuse it.

            Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            L 1 Reply Last reply
            0
            • C Christian Graus

              Lucy_H85 wrote:

              Player[] players = new Player[1];

              As I thought, this creates a new local variable. Turn it into a member if you want to reuse it.

              Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              L Offline
              L Offline
              Lucy
              wrote on last edited by
              #6

              Thank you! I will try that now. Lucy

              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