2

Please Help!

What am I doing wrong?

 static void f1(Color color, params float[] f) { System.Console.WriteLine("Here is f1 for float"); } static void f1(Color color, params int[] f) { System.Console.WriteLine("Here is f1 for int"); } static void Main() { f1(null,0); } 

I can't invoke f1(null,0); I get compile time error.

How this staff can be overcome assuming I indeed need those method signatures?

EDIT: As for Compile-tme error - ReSharper complains:

Cannot resolve method f1(null,int), candidates are:

void f1(Syste.Drawing.Color, params[] float)

void f1(Syste.Drawing.Color, params[] int)

3
  • You could try explicitly casting the 0 to either an int or a float, but I've just tried your code in a test program and it compiles fine. This is because the 0 is assumed to be a 32-bit integer, so the second of the two methods is called. Can you post the compilation error you get so we have more information? EDIT: Refer to @meziantou's answer - it might be what your looking for. Commented Dec 7, 2011 at 9:01
  • @Yochai Timmer: It's a shame you can't downvote a comment. Firstly, that wouldn't answer the question. Secondly, params is syntactic sugar which implicitly converts a comma-seperated list of values (which may be 0...n in number) into an array, so Michael Z's code is completely valid in that respect. Understand the details of the language before you answer. Commented Dec 12, 2011 at 20:08
  • @TomW You're right. I've looked into it after this post. Forgot to remove the comment. Commented Dec 12, 2011 at 22:36

5 Answers 5

4

I think the problem is you are passing null for Color which might be upsetting the function, either change that to Color? (since it is a struct) or pass a valid Color value

static void f1(Color? color, params float[] f) static void f1(Color? color, params int[] f) 
Sign up to request clarification or add additional context in comments.

Comments

3

You cannot pass null in place of a struct parameter, that is Color in your example. The problem is not about the params parameters. Some valid calls would be like;

f1(Color.Black, 0); // prints "Here is f1 for int" f1(Color.Black, 0f); //prints "Here is f1 for float" f1(Color.Black, 0, 5, 6, 7); // prints "Here is f1 for int" f1(Color.Black, 0, 5.4f, 6, 7); //prints "Here is f1 for float" 

Comments

1

If Color is System.Drawing.Color then parameter color can't be null. If you want color to be nullable use Color? type

f1(Color.Black, 0) // works f1(null, 0) // Doesn't work 

Comments

0

You need to pass System.Drawing.Color Object not null in Color parameter.

Comments

0

First of all, you cant use null for the Color parameter. To use one of both codes use this for ints:

f1(SystemColors.ActiveBorder, new int[]{0}); 

or this for floats:

f1(SystemColors.ActiveBorder, new float[]{0}); 

the way you use it it will always call the int version.

1 Comment

Since the f1() method takes a second argument of params, you do not need to define the input as an array. Also, 0 as it stands is taken as a 32-bit integer, so the integer overload of the method will be taken. Specifying 0f would explicitly select the float overload of the method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.