Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Endless method arguments of the same type

I remember that I have red somewhere that you can create a method that takes endless arguments. The problem is that I don't remember how to do it. I remember that it was something like this:

private void method(int arguments...)
{
//method body
}

I'm sure that there was "...". And I remember that when you call method you could call it like this: method(3232); or method(123,23,12); If anyone understands of what I'm talking about please tell me how to do it.

like image 596
Bosak Avatar asked Jan 03 '12 20:01

Bosak


People also ask

How do you pass an infinite argument in Java?

It's called varargs. It allows a method to take any number of arguments. They are accessible as an array in the method: public void foo(String...

What is a Ruby argument?

In Ruby, all arguments are required when you invoke the method. You can't define a method to accept a parameter and call the method without an argument. Additionally, a method defined to accept one parameter will raise an error if called with more than one argument.

What's the difference between a parameter and an argument Ruby?

Parameter is the variable in the declaration of the function. Argument is the actual value of this variable that gets passed to the function.


3 Answers

You would use the params keyword:

private void method(params int[] arguments)  {      //method body  } 

You could invoke your method like so: method(1,2,3,4,5,6,7,8,9); and the array would contain those numbers. The params keyword must be on an array, and if it is not the only parameter in the method, it must be the last. Only one parameter can have have the param declaration.

like image 109
vcsjones Avatar answered Sep 21 '22 15:09

vcsjones


you mean ParamArray ? (for vb.net)

for c# it seem it's params

like image 35
Fredou Avatar answered Sep 21 '22 15:09

Fredou


You are looking for c/c++ definition of infinite number of arguments to a function. You can see here - http://www.cplusplus.com/reference/cstdarg/va_start/

An easy way to implement such a function is like that:

1- define your function for example

void logging(const char *_string, int numArgs, ...)

First argument is the string you want to use.

Second argument is the number of the infinite arguments, which you want to give. You dont have to use this parameter, if you want to count the placeholders in a switch (like %d, %f in printf) -Hint: in a loop get every char and look if it is your placeholder-.

I want to give first an example how you could call such a function :

logging("Hello %0. %1 %2 %3", "world", "nice", "to", "meet you"); // infinite arguments are "world", "nice", ... you can give as much as you want

As you see my placeholders are numbers. You can use anything you want.

2- There are macros, which initializes the list variable and gets the value of an argument :

va_list arguments; // define the list
va_start(arguments, numArgs); // initialize it, Note: second argument is the last parameter in function, here numArgs

for (int x = 0; x < numArgs; x++) // in a loop
{ 
      // Note : va_arg(..) gets an element from the stack once, dont call it twice, or else you will get the next argument-value from the stack
      char *msg = va_arg(arguments, char *); // get "infinite argument"-value Note: Second parameter is the type of the "infinite argument".
      ... // Now you can do whatever you want - for example : search "%0" in the string and replace with msg
}
va_end ( arguments ); // we must end the listing

If you replace each placeholder with the infinite argument values and print the new string, you should see this:

Hello world. nice to meet you

I hope that helps...

like image 24
zakarrr Avatar answered Sep 21 '22 15:09

zakarrr