By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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: To****@Olbort.de (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: <bc**************************@posting.google.com >
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: gr**********@google.com
NNTP-Posting-Date: Tue, 13 Jan 2004 21:28:39 +0000 (UTC)
Path: cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTN GP08.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!postnew s2.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


Nov 15 '05 #2

P: n/a
Tobias Olbort <To****@Olbort.de> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.