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.
  • M musefan

    yeah i wanted to double check it was nullable. Like i have said i dont see a use for it...

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

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

    A object reference is, by definition, nullable.

    musefan wrote:

    Like i have said i dont see a use for it...

    I see. :)

    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
    • M musefan

      shennwooi wrote:

      EventArgs e = null;

      This appears to work. but why would you want to do that? what is your requirement?

      shennwooi wrote:

      Can i assign some string or integer to EventArgs?

      No. You cant do that. Again why would you want to?

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

      B Offline
      B Offline
      BabyOreo
      wrote on last edited by
      #9

      i'm doing vsts2008 unit testing. How m i assign date to following test method? public void btnAddEntry_ClickTest() { frmMileage_Accessor target = new frmMileage_Accessor(); // TODO: Initialize to an appropriate value object sender = null; // TODO: Initialize to an appropriate value EventArgs e = null; // TODO: Initialize to an appropriate value target.btnAddEntry_Click(sender, e); //Assert.Inconclusive("A method that does not return a value cannot be verified."); }

      M 1 Reply Last reply
      0
      • B BabyOreo

        Can i assign some string or integer to EventArgs? For example, i wan assign value to the following: EventArgs e = null; Anyone can provide some examples? Thanks!

        1 Offline
        1 Offline
        12Code
        wrote on last edited by
        #10

        use this

        btnTest_Click(this, null);

        to test button click.

        ;)*12Code

        B 1 Reply Last reply
        0
        • 1 12Code

          use this

          btnTest_Click(this, null);

          to test button click.

          ;)*12Code

          B Offline
          B Offline
          BabyOreo
          wrote on last edited by
          #11

          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 1 Reply Last reply
          0
          • B BabyOreo

            Can i assign some string or integer to EventArgs? For example, i wan assign value to the following: EventArgs e = null; Anyone can provide some examples? Thanks!

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #12

            You've had some good answers. To add to them, you can't use your derived event args for existing events unless you create your own controls derived from the original. For example, maybe you want to assign the integer value 1 to the event args passed when a form is loaded. You need to create your own EventArgs and your own Form with a new Load event and override the OnLoad method. Something like this.

            using System;
            using System.Windows.Forms;

            namespace WindowsFormsApplication1
            {
            public partial class Form1 : IntForm
            {
            public Form1()
            {
            InitializeComponent();
            this.Load += new System.EventHandler<inteventargs>(Form1_Load);
            }

                private void Form1\_Load(object sender, IntEventArgs e)
                {
                    Console.WriteLine(e.Value);
                }
            }
            
            public class IntEventArgs : EventArgs
            {
                private int \_Value;
            
                public IntEventArgs(int value)
                {
                    \_Value = value;
                }
                public IntEventArgs() : this(0) { }
            
                public static implicit operator IntEventArgs(int value)
                {
                    return new IntEventArgs(value);
                }
            
                /// <summary>
                /// Gets the System.Int32 value held by this IntEventArgs instance.
                /// </summary>
                public int Value
                {
                    get { return \_Value; }
                }
            }
            
            public class IntForm : Form
            {
                /// <summary>
                /// Occurs before a form is displayed for the first time.
                /// </summary>
                public new event EventHandler<IntEventArgs> Load;
            
                protected override void OnLoad(EventArgs e)
                {
                    EventHandler<IntEventArgs> eh = Load;
                    if (eh != null)
                        eh(this, 1);
                }
            }
            

            }

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
            Why are you using VB6? Do you hate yourself? (Christian Graus)

            1 Reply Last reply
            0
            • CPalliniC CPallini

              A object reference is, by definition, nullable.

              musefan wrote:

              Like i have said i dont see a use for it...

              I see. :)

              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
              #13

              what would you use it for?

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

              CPalliniC 1 Reply Last reply
              0
              • B BabyOreo

                i'm doing vsts2008 unit testing. How m i assign date to following test method? public void btnAddEntry_ClickTest() { frmMileage_Accessor target = new frmMileage_Accessor(); // TODO: Initialize to an appropriate value object sender = null; // TODO: Initialize to an appropriate value EventArgs e = null; // TODO: Initialize to an appropriate value target.btnAddEntry_Click(sender, e); //Assert.Inconclusive("A method that does not return a value cannot be verified."); }

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

                as a parameter, no?

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

                B 1 Reply Last reply
                0
                • M musefan

                  what would you use it for?

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

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

                  Like for any other object reference: a null value simply states that the reference 'points' to no object. :)

                  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
                  • M musefan

                    as a parameter, no?

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

                    B Offline
                    B Offline
                    BabyOreo
                    wrote on last edited by
                    #16

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

                    M 1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      Like for any other object reference: a null value simply states that the reference 'points' to no object. :)

                      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
                      #17

                      yes, im not doubting the use of it for other object types. but why would anyone need to assign null to EventArgs instance?

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

                      CPalliniC 1 Reply Last reply
                      0
                      • M musefan

                        yes, im not doubting the use of it for other object types. but why would anyone need to assign null to EventArgs instance?

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

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

                        Probably noone. Neverthless, the wise developer receiving a EventArgs reference (in the event handler), should consider the possibility of a null value. :)

                        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

                          Probably noone. Neverthless, the wise developer receiving a EventArgs reference (in the event handler), should consider the possibility of a null value. :)

                          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
                          #19

                          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.

                          H CPalliniC 2 Replies 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.

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

                            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 1 Reply Last reply
                            0
                            • 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
                                          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