473,666 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cannot pass array as a function parameter?

When I pass an array as a function parameter, it yields the following
compile error. Any ideas?? However, if I create a variable that holds an
array, and pass that variable to the function parameter, then it's working
fine. Any ideas?? Thanks!

public static int[] getFreq(string[] s)
{...
}

int[] res = getFreq({"eee", "ewsww"});
WordFreq.cs(7,2 3): error CS1525: Invalid expression term '{'
WordFreq.cs(7,2 4): error CS1026: ) expected
WordFreq.cs(7,2 9): error CS1002: ; expected
WordFreq.cs(7,2 9): error CS1525: Invalid expression term ','
WordFreq.cs(7,3 1): error CS1002: ; expected
WordFreq.cs(7,3 8): error CS1002: ; expected
WordFreq.cs(7,3 9): error CS1525: Invalid expression term ')'
Nov 15 '05 #1
2 4571
Matthew,

You need to declare the array. Arrays are managed objects in .NET, so
that needs to be reflected somewhere. If you change your code to this:

int[] res = getFreq(new string[]{"eee", "ewsww"});

Then it will work.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Matthew Louden" <jr********@hot mail.com> wrote in message
news:ef******** ******@tk2msftn gp13.phx.gbl...
When I pass an array as a function parameter, it yields the following
compile error. Any ideas?? However, if I create a variable that holds an
array, and pass that variable to the function parameter, then it's working
fine. Any ideas?? Thanks!

public static int[] getFreq(string[] s)
{...
}

int[] res = getFreq({"eee", "ewsww"});
WordFreq.cs(7,2 3): error CS1525: Invalid expression term '{'
WordFreq.cs(7,2 4): error CS1026: ) expected
WordFreq.cs(7,2 9): error CS1002: ; expected
WordFreq.cs(7,2 9): error CS1525: Invalid expression term ','
WordFreq.cs(7,3 1): error CS1002: ; expected
WordFreq.cs(7,3 8): error CS1002: ; expected
WordFreq.cs(7,3 9): error CS1525: Invalid expression term ')'

Nov 15 '05 #2

"Matthew Louden" wrote...
When I pass an array as a function parameter,
it yields the following
compile error. Any ideas?? public static int[] getFreq(string[] s)
{...
}

int[] res = getFreq({"eee", "ewsww"});


You're trying to send a "list of strings", while the method takes an
array-object as parameter. You have to initialize the array-object itself as
well:

int[] res = getFreq( new string[]{"eee", "ewsww"} );

// Bjorn A
Nov 15 '05 #3

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

Similar topics

7
25166
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort (Bubble, insertion, selection..). I have an array of int's and am passing them to each sort function.
5
3414
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? /******************************************************** Below is my code: ********************************************************/
10
9138
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
9
2311
by: Alan Silver | last post by:
Hello, I'm a bit surprised at the amount of boilerplate code required to do standard data access in .NET and was looking for a way to improve matters. In Classic ASP, I used to have a common function that was included in all pages that took an SQL query and returned a disconnected recordset. This meant that data access could be achieved in a single line. I would like to do something similar in ASP.NET. I know I could just duplicate...
4
150973
by: _Mario.lat | last post by:
Hallo, I have a little question: In the function session_set_save_handler I can pass the name of function which deal with session. In Xoops code I see the use of this function like that: session_set_save_handler(array(&$sess_handler, 'open'), array(&$sess_handler, 'close'), array(&$sess_handler, 'read'), array(&$sess_handler, 'write'), array(&$sess_handler, 'destroy'), array(&$sess_handler, 'gc'));
14
20389
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case the value if modified in the function will get reflected in the main function as well. I dont...
8
6258
by: =?Utf-8?B?V2hpdG5leSBLZXc=?= | last post by:
Hi there, I'm having a bit of trouble using an HRASCONN object as a class member variable inside my managed C++ class. When I call RasDial() and pass in the address of my HRASCONN object, I get the following compiler error from Visual C++ 2005: error C2664: 'RasDialW' : cannot convert parameter 6 from 'cli::interior_ptr<Type>' to 'HRASCONN *'
3
2497
by: QQ | last post by:
I have one integer array int A; I need to pass this array into a function and evaluate this array in this function how should I pass? Is it fine? void test(int *a)
2
44496
by: SM | last post by:
Hello, Such a simple question and i can't find the answer(see code below). Here it is: I have a function: ini() In that function, i call a function (load_CDThumbnail) that creates an array and returns the newly created array Then, another function is called (show) that needs to pass the newly created array as a parameter. That's the part that i don't know how to program
0
8440
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
8352
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,...
0
7378
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
6189
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
5661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4192
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...
0
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2005
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.