Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

c# - No overload for method, takes 0 arguments?

I have:

 public static int[] ArrayWorkings()

I can call it happily with MyClass.ArrayWorkings() from anywhere. But I want to build in some extra functionality by requiring a parameter such as:

 public static int[] ArrayWorkings(int variable)

I get the error No overload for method ArrayWorkings, takes 0 arguments. Why is this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You changed the function to require one parameter... so now all of your old function calls, which passed no parameters, are invalid.

Is this parameter absolutely necessary, or is it a default value? if it is a default then use a default parameter or an overload:

//`variable` will be 0 if called with no parameters
public static int[] ArrayWorkings(int variable=0)  

// pre-C# 4.0
public static int[] ArrayWorkings()
{
    ArrayWorkings(0);
}

public static int[] ArrayWorkings(int variable)
{
    // do stuff
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...