CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2008
    Posts
    154

    [RESOLVED] Need my Function available for two Items

    C# VS 2008

    I have a function that is doing some necessary stuff depending on the values set in a numericUpDown. The function is triggered by a combobox selection. example

    Code:
            private void ChangeLevels(object sender, EventArgs e)
            {
                decimal valueSet = numeric_Levels1.Value;
                if (newValue == 0)
                {
                    Do STUFF
                }
                else if (newValue > 0)
                {
                    Do STUFF
                }
    in Forms I understand that function can be assign to multiple tools, however in this case I want a 2nd set of numericUpDown and a combobox using the same function but be independant of the 1st set. As you can see in the code if I dropped this function in the 2nd set of comboxes it would still be using the 1st numericUpDown. Is there a way I can make this code 'universal' with out duplicating it?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Need my Function available for two Items

    It's a bit tough to follow your code - did you mean to the valueSet variable called newValue or is newValue a class field? (as a side issue always a good reason to preface class fields with an underscore).

    At any rate, if we assume decimal valueSet should have been named newValue. Just pull out the code that does stuff into another method.

    Code:
    private void ChangeLevels(object sender, EventArgs e)
    {
      SetStuff( numeric_Levels1.Value );
    }
    
    private void ChangeMoreLevels(object sender, EventArgs e)
    {
      SetStuff( numeric_Levels2.Value );
    }
    
    private void SetStuff( decimal value )
    {
      if ( value == 0)
      {
        // Do STUFF
      }
      else if (value > 0)
      {
        // Do other STUFF
      }
    }

  3. #3
    Join Date
    Jun 2008
    Posts
    154

    Re: Need my Function available for two Items

    That's perfect! (I prolly would have figured it out on my own but I am still waking up). need moar coffee!!

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: [RESOLVED] Need my Function available for two Items

    Code:
    private void ChangeLevels(object sender, EventArgs e)
            {
                decimal valueSet = ((NumericUpDown) sender).Value;
                if (newValue == 0)
                {
                    Do STUFF
                }
                else if (newValue > 0)
                {
                    Do STUFF
                }
    Or just use 'sender' the way it was meant to be used
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: [RESOLVED] Need my Function available for two Items

    I cant work out your problem but this may help:

    Suppose I had 10 buttons on a form. I wanted to use one block of code to handle them all. I Link them all to the same handler, then:

    Code:
    public void Generic_Click(object sender, ClickEventArgs e){
    
      MessageBox.Show((sender as Button).Text);
    
      MessageBox.Show("The object stored in this button's tag is: " + (sender as Button).Tag.ToString());
    
    }
    This is one block of code that will display a different text depending which button was clicked

    Perhaps this is what you mean?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  6. #6
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: [RESOLVED] Need my Function available for two Items

    One minute, darnit
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  7. #7
    Join Date
    May 2007
    Posts
    1,546

    Re: [RESOLVED] Need my Function available for two Items

    Purely a style point:

    Code:
    (sender as Button).Text
    Never use 'as' unless you follow it by a null check. If things are going to fail, it's *much* better to fail with an InvalidCastException than it is a NullReferenceException.

    One minute, darnit
    That's what she said!

    oh.. wait. That's not a good thing
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured