passing a params-array to another function with params-array

T

Tobias Olbort

Hello,

i've a outer function, which takes a params-array as a parameter. I
want to pass this array to inner function with a params-array (e. g.
string.format).

When i've passed an integer to the outer function, the string.format
sets the string-value "System.Object[]" at the position of {0}.

How can i get it work, so that all my values, passed to the outer
function, will inserted by string.format, like i've passed it to the
inner function directly?

public static object SQLImmediate(string format, params object[] arg)
{
SqlCommand cmd = new SqlCommand(String.Format(format, arg), _cn);
return cmd.ExecuteScalar();
}

Thanks for any help

Tobias
 
N

Niklas Borson [MSFT]

You should be able to simply pass the array, provided it has the same type
as the parameter array in the method declaration. Section 10.5.1, Method
Parameters, in the C# Language Specification contains the following
sentence:

"In a method invocation, a parameter array permits either a single argument
of the given array type to be specified, or it permits zero or more
arguments of the array element type to be specified."

For example, the following are equivalent:

string.Format("{0} {1}, {2}", "January", 13, 2004);
string.Format("{0} {1}, {2}", new object[] { "January", 13, 2004 } );

In fact, the first expression above is really just shorthand for the
second. (Section 10.5.1.4, Parameter Arrays: "...the invocation creates an
instance of the parameter array type with a length corresponding to the
number of arguments, initializes the elements of the array instance with
the given argument values, and uses the newly created array instance as the
actual argument.")

--Nick

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: (e-mail address removed) (Tobias Olbort)
Newsgroups: microsoft.public.dotnet.languages.csharp
Subject: passing a params-array to another function with params-array
Date: 13 Jan 2004 13:28:38 -0800
Organization: http://groups.google.com
Lines: 22
Message-ID: <[email protected]>
NNTP-Posting-Host: 217.236.120.83
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1074029319 5812 127.0.0.1 (13 Jan 2004 21:28:39 GMT)
X-Complaints-To: (e-mail address removed)
NNTP-Posting-Date: Tue, 13 Jan 2004 21:28:39 +0000 (UTC)
Path: cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.su
l.t-online.de!t-online.de!npeer.de.kpn-eurorings.net!news.tele.dk!news.tele.
dk!small.news.tele.dk!news.maxwell.syr.edu!postnews2.google.com!not-for-mail
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.languages.csharp:211777
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Hello,

i've a outer function, which takes a params-array as a parameter. I
want to pass this array to inner function with a params-array (e. g.
string.format).

When i've passed an integer to the outer function, the string.format
sets the string-value "System.Object[]" at the position of {0}.

How can i get it work, so that all my values, passed to the outer
function, will inserted by string.format, like i've passed it to the
inner function directly?

public static object SQLImmediate(string format, params object[] arg)
{
SqlCommand cmd = new SqlCommand(String.Format(format, arg), _cn);
return cmd.ExecuteScalar();
}

Thanks for any help

Tobias
 
J

Jon Skeet [C# MVP]

Tobias Olbort said:
i've a outer function, which takes a params-array as a parameter. I
want to pass this array to inner function with a params-array (e. g.
string.format).

When i've passed an integer to the outer function, the string.format
sets the string-value "System.Object[]" at the position of {0}.

How can i get it work, so that all my values, passed to the outer
function, will inserted by string.format, like i've passed it to the
inner function directly?

public static object SQLImmediate(string format, params object[] arg)
{
SqlCommand cmd = new SqlCommand(String.Format(format, arg), _cn);
return cmd.ExecuteScalar();
}

It should be fine. Here's a sample program:

using System;

public class Test
{
static void Main()
{
ConsoleWrite ("{0} {1}", 2, 3);
}

static void ConsoleWrite (string format, params object[] o)
{
Console.WriteLine (format, o);
}
}

Changing it to use String.Format explicitly works too.

Can you provide a complete example which *doesn't* work?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top