Solved

best way to pass multiple parameters to a method?

Posted on 2008-06-25
13
163 Views
Last Modified: 2013-12-16
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?
0
Comment
Question by:rmmarsh
  • 8
  • 5
13 Comments
 
LVL 6

Expert Comment

by:RishadanPort
ID: 21869575
Why pass in the entire object? I suggest passing in delegates for the specific method you might need.

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.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 21869583
If you wanted to You could pass them in to the classes constructor, or use a "setReferencedTextBox" Method.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 21869592
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.
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 6

Expert Comment

by:RishadanPort
ID: 21869632
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
0
 

Author Comment

by:rmmarsh
ID: 21869651
I don't see how delegates are going to help me... you still have to pass parameters of the objects from the main form...
0
 

Author Comment

by:rmmarsh
ID: 21869662
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?
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 21869667
Why do you need to be passing many objects ?
0
 

Author Comment

by:rmmarsh
ID: 21869679
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...
0
 
LVL 6

Accepted Solution

by:
RishadanPort earned 500 total points
ID: 21869687
If you wanted to, you could pass them in an ArrayList,

Like:

ArrayList list = new ArrayList();
list.add(panel);
list.add(textBox);
...

and then just pass the list around.

But I don't recommend this approach. Note that if you do this approach, you would need to know in what order to added the controls in
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 21869701
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*
0
 

Author Comment

by:rmmarsh
ID: 21869736
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...
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 21869752
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.
0
 

Author Comment

by:rmmarsh
ID: 21869764
Thanks for your help... you gave me some good ideas...
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question