|
-
February 19th, 2009, 05:14 PM
#1
[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?
-
February 19th, 2009, 06:32 PM
#2
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
}
}
-
February 19th, 2009, 07:21 PM
#3
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!!
-
February 19th, 2009, 07:23 PM
#4
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.
-
February 19th, 2009, 07:24 PM
#5
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?
-
February 19th, 2009, 07:25 PM
#6
Re: [RESOLVED] Need my Function available for two Items
-
February 19th, 2009, 07:29 PM
#7
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|