将新参数添加到已在使用的方法中 [英] adding new parameter to a method which is already in use

查看:108
本文介绍了将新参数添加到已在使用的方法中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个名为public int sum(int a,int b)的方法。



我在项目的所有其他页面中都使用了sum()方法。



但是现在我想添加一个名为int c的新参数即sum(int a,int b,int c)。



如果我添加它们我会收到错误



Hello,

I have a method called public int sum(int a,int b).

And i had made use of the sum() method in all other pages in my project.

But now i wanted to add one new parameter called int c i.e., sum(int a,int b,int c).

If i add it them i am getting error that

"

Error   2   No overload for method 'sum' takes 4 arguments    C:\Windows Data\gv_info\contactus.aspx.cs   43  28  C:\Windows Data\gfo\







那么有什么方法可以解决这个问题。



因为我在php函数上工作,我曾经把函数称为和(int a,int b,int c = 0)。



在ASP.NET中我想知道...



Plz帮助我

.


So is there any way to solve this problem.

As i worked on php function, there i used to call a function as sum(int a,int b,int c=0).

In ASP.NET i wanted to know...

Plz help me

推荐答案

在C#中(如果它是V4或更高版本)你可以提供可选参数:

In C# (provided it's V4 or above) you can provide optional arguments:
public int Sum(int a, int b, int c = 0)
   {
   ...

它会正常工作。



或者,你可以使用 params 关键字:

And it'll work fine.

Or, you could use the params keyword:

public int Sum (params int[] vars)
   {
   int sum = 0;
   foreach (int i in vars)
      {
      sum += i;
   ...

然后你可以用一个或多个值来调用它:

You can then call it with one or more values:

sum = Sum(7);
sum = Sum(1, 2, 3, 4, 5, 6, 7, 8, 9);


Quote:

方法'sum'没有重载需要4个参数

No overload for method 'sum' takes 4 arguments

错误信息非常清楚。您没有 sum 方法或重载 sum 方法来处理4个参数。



更改现有的 sum 方法签名或定义一个 sum的重载接受4个参数的方法。

The error message is quite clear. You don't have a sum method or a overload sum method to deal with 4 parameters.

Either change the existing sum method signature or define one overload of sum method accepting 4 parameters.


这篇关于将新参数添加到已在使用的方法中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆