rmmarsh
asked on
best way to pass multiple parameters to a method?
I have several classes that refer to textboxes, checkboxes, etc on the main form; I want to be able to refer to those exact textboxes, etc from within methods in other classs. The only way I know of doing this is passing those textboxes, etc to the methods in those other classes.
Is there another better way of doing this?
Is there another better way of doing this?
If you wanted to You could pass them in to the classes constructor, or use a "setReferencedTextBox" Method.
I find that it is badly object oriented design if you are passing your entire controls everywhere though... What I do is simply pass in delegates that I need.
Example of using delegates:
public delegate String myDelegateMethod();
public class A{
TextBox text = new TextBox();
public A(){
text.Text = "Test";
}
public String getText(){
return text.Text;
}
}
public class B{
myDelegateMethod getText;
public B(myDelegateMethod getTextMethod){
getText = getTextMethod;
}
}
Now in class B, all you need to do is getText(), and it refers to class A's getText method
public delegate String myDelegateMethod();
public class A{
TextBox text = new TextBox();
public A(){
text.Text = "Test";
}
public String getText(){
return text.Text;
}
}
public class B{
myDelegateMethod getText;
public B(myDelegateMethod getTextMethod){
getText = getTextMethod;
}
}
Now in class B, all you need to do is getText(), and it refers to class A's getText method
ASKER
I don't see how delegates are going to help me... you still have to pass parameters of the objects from the main form...
ASKER
Also, I am not passing in every object; only a few, maybe 6-10 of the textboxes, and controls that have the information I need to access.
I can do it by passing them as parameters.. I was thinking of something along the line of an array of parameters?
I can do it by passing them as parameters.. I was thinking of something along the line of an array of parameters?
Why do you need to be passing many objects ?
ASKER
because the user sets certain switches (checkboxes, radio buttons) that I need to conrol the process... also, some of the information is in textboxes which I need...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why not just Handle all that in 1 class... and then pass that class around?
What I do, is this, in C# all my text boxes.... or what not when I am designing are put in User Controls.
That user control handles everything of the buttons or what not. And then when I need to, I pass that user control around.
That User control has methods to access *what is needed*
What I do, is this, in C# all my text boxes.... or what not when I am designing are put in User Controls.
That user control handles everything of the buttons or what not. And then when I need to, I pass that user control around.
That User control has methods to access *what is needed*
ASKER
I think the ArrayList would be more efficient... some of the objects need to be modified from the other classes, so that would solve that problem...
I really don't suggest that though... It is not an object oriented approach. I highly suggest wrapping all the controls in a class, and passing that class around, with accesors to those controls. Make that class handle all of those controls.
ASKER
Thanks for your help... you gave me some good ideas...
I find that if all classes reference all other object classes, it gets very confusing to code... It would be much better if your class only has the a delegate in which you need.