473,748 Members | 3,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.Obj ect[]" 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(st ring format, params object[] arg)
{
SqlCommand cmd = new SqlCommand(Stri ng.Format(forma t, arg), _cn);
return cmd.ExecuteScal ar();
}

Thanks for any help

Tobias
Nov 15 '05 #1
2 15127
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.d e (Tobias Olbort)
Newsgroups: microsoft.publi c.dotnet.langua ges.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**********@go ogle.com
NNTP-Posting-Date: Tue, 13 Jan 2004 21:28:39 +0000 (UTC)
Path: cpmsftngxa07.ph x.gbl!cpmsftngx a06.phx.gbl!TK2 MSFTNGP08.phx.g bl!newsfeed00.s u
l.t-online.de!t-online.de!npeer .de.kpn-eurorings.net!n ews.tele.dk!new s.tele.
dk!small.news.t ele.dk!news.max well.syr.edu!po stnews2.google. com!not-for-mail Xref: cpmsftngxa07.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:2117 77
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.Obj ect[]" 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(st ring format, params object[] arg)
{
SqlCommand cmd = new SqlCommand(Stri ng.Format(forma t, arg), _cn);
return cmd.ExecuteScal ar();
}

Thanks for any help

Tobias


Nov 15 '05 #2
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.Obj ect[]" 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(st ring format, params object[] arg)
{
SqlCommand cmd = new SqlCommand(Stri ng.Format(forma t, arg), _cn);
return cmd.ExecuteScal ar();
}


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.WriteLi ne (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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
16845
by: Andy Read | last post by:
Dear all, I thought I understood passing parameters ByVal and ByRef but I clearly don't! If I define a simple class of: Public Class Person Public Name as String Public Age as Integer End Class
5
6698
by: Jack | last post by:
Hi, I need to pass multple variables in a link in order to go to a asp page with the two varables. The following are the values of the variables using response.write: <%'Response.Write Mypage & "<br>"%> Exp <%'Response.Write GrantID & "<br>"%>
7
2868
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID parameter to the XSLT stylesheet XsltArgumentList xsltArgList = new XsltArgumentList(); xsltArgList.AddParam("pmID", "", pmID); xmlItems.TransformArgumentList = xsltArgList;
11
3184
by: Arsen Vladimirskiy | last post by:
Hello, If I have a few simple classes to represent Entities such as Customers and Orders. What is the proper way to pass information to the Data Access Layer? 1) Pass the actual ENTITY to the Data Access Layer method -or- 2) Pass some kind of a unique id to the Data Access Layer method
9
2298
by: Just Me | last post by:
PARAFORMAT2 is a structure that SendMessage will return stuff in. Is the "ref" correct or since only a pointer is being passed should it be by value? Suppose I was passing data rather then receiving it, would that change the answer to the above?
8
2117
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects, each requiring a call to that method to fulfill their purpose. There could be 200, there could be more than 1000. That is a lot of references passed around. It feels heavy. Let us say i changed the signature of the interface method to:
22
25597
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to compile. <WebMethod()> _ Public Function VerifySku(ByVal skus As XmlDataDocument) As DataSet Test program : Dim cartSet As DataSet cartSet = ws.VerifySku(cartSet)
6
5996
by: MSDNAndi | last post by:
Hi, I get the following warning: "Possibly incorrect assignment to local 'oLockObject' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local." My code is: using System; using System.Collections.Generic;
61
3146
by: academic | last post by:
When I declare a reference variable I initialize it to Nothing. Now I'm wondering if that best for String variables - is "" better? With Nothing I assume no memory is set aside nor GC'ed But with "" it is - correct? The system seems to handle a null the same as "".
12
5400
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.