pass in numbers?

Reply

Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

pass in numbers?

 
0
  #1
Feb 20th, 2009
is there a way to pass in numbers(int) like this?

somename([0,0],[2,4],3);

thanks for any help
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,078
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 142
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: pass in numbers?

 
2
  #2
Feb 20th, 2009
You mean pass in arrays of numbers? Yes, use arrays.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2
Reputation: poorvi_tiwari is an unknown quantity at this point 
Solved Threads: 0
poorvi_tiwari poorvi_tiwari is offline Offline
Newbie Poster

Re: pass in numbers?

 
0
  #3
Feb 21st, 2009
Originally Posted by JackDurden View Post
is there a way to pass in numbers(int) like this?

somename([0,0],[2,4],3);

thanks for any help

solution-yes we can pass it like this:
int a[0,0],b[2,4];
int c=3;
we can declare these variables seperately like this ...but collectively we can't do this as u asked...
i think the method i told u ..it will definitely help u....try it
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 442
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 73
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: pass in numbers?

 
0
  #4
Feb 21st, 2009
Passing parameters to a method requires the correct parameter list declaration in the Method construct.

If you plan on passing a strictly typed list then for your example:
  1. private void something(int[] a, int[] b, int c)

If you do not know how many parameters you are going to send, or the order then you can use a different approach:
  1. private void something(params object[] data )

To Call either of these methods you can format the call as such:
  1. something(new int[] { 0, 1 }, new int[] { 2, 3 }, 3);

Note that if you have both of these method constructs, the more exact version is what will be used:
IOW the one with the something(int[] a, int[] b, c) is prime because it exactly meets the parameters being called.
If you rearrange the parameters so that the int is in position 0 or one, then it uses the something( params object[] data) version.

To use the "params" version you could do something like this:
  1. private void something(params object[] data )
  2. {
  3. int sum = 0;
  4. int[] somearray;
  5. foreach (object obj in data)
  6. {
  7. if(obj.GetType() == typeof(int[]))
  8. {
  9. somearray = (int[])obj;
  10. foreach (int n in somearray)
  11. sum += n;
  12. }
  13. else if (obj.GetType() == typeof(int))
  14. {
  15. sum += (int)obj;
  16. }
  17. }
  18. MessageBox.Show(sum.ToString());
  19. }

Happy Coding // Jerry
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 413 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC