cannot pass array as a function parameter?

  • Thread starter Thread starter Matthew Louden
  • Start date Start date
M

Matthew Louden

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,23): error CS1525: Invalid expression term '{'
WordFreq.cs(7,24): error CS1026: ) expected
WordFreq.cs(7,29): error CS1002: ; expected
WordFreq.cs(7,29): error CS1525: Invalid expression term ','
WordFreq.cs(7,31): error CS1002: ; expected
WordFreq.cs(7,38): error CS1002: ; expected
WordFreq.cs(7,39): error CS1525: Invalid expression term ')'
 
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.
 
...
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
 
Back
Top