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. Argument

Argument

Scheduled Pinned Locked Moved C#
tutorialquestion
28 Posts 10 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.
  • B BabyOreo

    yes.wan to know how to assign a value to test method.

    M Offline
    M Offline
    musefan
    wrote on last edited by
    #21

    perhaps try creating your own eventargs class that inherits from EventArgs then you can put any properties you want to use in it and pass that to the button click handler

    Life goes very fast. Tomorrow, today is already yesterday.

    1 Reply Last reply
    0
    • H Henry Minute

      Sometimes, for example (admittedly a bad one) if you want to call a button click handler, you can use myButton_Click(this, null), rather than myButton.PerformClick(). Although, as you have said, why would you want to?

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #22

      yep, that possible. But if you were testing with null then your button click handler would surely never make use of EventArgs and you would never need to check it. anyway if you want the functionality called manually then you should have it as a separate function that the button click handler also calls.

      Life goes very fast. Tomorrow, today is already yesterday.

      H 1 Reply Last reply
      0
      • M musefan

        yep, that possible. But if you were testing with null then your button click handler would surely never make use of EventArgs and you would never need to check it. anyway if you want the functionality called manually then you should have it as a separate function that the button click handler also calls.

        Life goes very fast. Tomorrow, today is already yesterday.

        H Offline
        H Offline
        Henry Minute
        wrote on last edited by
        #23

        Agreed, but since EventArgs is essentially empty, nothing that I can think of actually makes any use of it.

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        1 Reply Last reply
        0
        • B BabyOreo

          what if i wan to test the following function? private void btnAddEntry_Click(object sender, EventArgs e) { double gas, miles; int i; string d; char c; bool validDate; // Validate date format (actual values not checked) // strip out any spaces d = ""; for (i = 0; i < txtDate.Text.Length; i++) { if (txtDate.Text.Substring(i, 1) != " ") { d += txtDate.Text.Substring(i, 1); } } txtDate.Text = d; validDate = true; // make sure date has eight characters (two are slashes remainder are numbers) if (txtDate.Text.Length != 8) { validDate = false; } else { for (i = 0; i < 8; i++) { c = Convert.ToChar(txtDate.Text.Substring(i, 1)); if (i == 2 || i == 5) { if (c != '/') { validDate = false; } } else { if (c < '0' || c > '9') { validDate = false; } } } } if (!validDate) { MessageBox.Show("Date must be in form mm/dd/yy", "Date Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtDate.Focus(); return; } // Make sure miles is greater than previous value miles = Convert.ToDouble(txtMiles.Text); if (numValues > 0) { if (miles <= odometer[numValues - 1]) { MessageBox.Show("Odometer reading less than previous value.", "Odometer Reading Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtMiles.Focus(); return; } } // No zero gallons allowed gas = Convert.ToDouble(txtGallons.Text); if (gas <= 0) { MessageBox.Show("Gallons pumped must be greater than zero", "Gallons Pumped Error", MessageBoxButtons.OK, MessageBoxI

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #24

          Hi, split your code into two methods: 1. one contains the "bunsiness logic" and no GUI stuff (No MessageBox); it does not take sender or eventargs as parameters, and it does return a calculated value and outputs (through out parameter) a string which should be shown to the user. 2. the buttonClickHandler which mainly calls the other method and shows the MessageBox when there is a need to.

          private void btnAddEntry_Click(object sender, EventArgs e) {
          string s;
          double result=calculate(out s);
          if (s==null) textBox.Text="The result is "+result;
          else MessageBox.Show(s);
          }

          private double calculate(out string message) {
          message=null;
          double result=...;
          if (...) message="Something went wrong";
          ...
          return result;
          }

          That way you can easily perform unit tests on the business logic, and there probably won't be a need to test the actual buttonClickHandler anymore. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


          1 Reply Last reply
          0
          • M musefan

            well EventArgs does not have any useful information so you wouldn't need to check if its null or not as you would never use the reference. Unless of course you pass it another EventArgs derived object but then you will need a cast so you would validate it first then.

            Life goes very fast. Tomorrow, today is already yesterday.

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #25

            musefan wrote:

            well EventArgs does not have any useful information so you wouldn't need to check if its null or not as you would never use the reference

            One useful information, maybe, for instance, if EventArgs reference is null or not. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            M 1 Reply Last reply
            0
            • CPalliniC CPallini

              musefan wrote:

              well EventArgs does not have any useful information so you wouldn't need to check if its null or not as you would never use the reference

              One useful information, maybe, for instance, if EventArgs reference is null or not. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              M Offline
              M Offline
              musefan
              wrote on last edited by
              #26

              do you mean you could use it to represent a boolean value for something else such as 'isUserClick'?

              Life goes very fast. Tomorrow, today is already yesterday.

              CPalliniC 1 Reply Last reply
              0
              • M musefan

                do you mean you could use it to represent a boolean value for something else such as 'isUserClick'?

                Life goes very fast. Tomorrow, today is already yesterday.

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #27

                Yes, it may represent a boolean value, ugly, I should admit. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                In testa che avete, signor di Ceprano?

                M 1 Reply Last reply
                0
                • CPalliniC CPallini

                  Yes, it may represent a boolean value, ugly, I should admit. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  M Offline
                  M Offline
                  musefan
                  wrote on last edited by
                  #28

                  yes very ugly, but i guess that is an answer to my question of what it could be used for so good answer.

                  Life goes very fast. Tomorrow, today is already yesterday.

                  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