传递参数

发布于 2024-09-19 09:42:22 字数 274 浏览 19 评论 0原文

我将参数作为 var 类型传递给函数。它不接受,我如何传递给该函数?

示例

var Input = ................

listview1.itemsource = getinput(Input);

public List<answers>getinput(var inp)
{
................
..................
}

这里函数不接受 var。我能做些什么?

I am passing the parameter to the function as a var type. It is not accepting, how do I pass to the function?

Example

var Input = ................

listview1.itemsource = getinput(Input);

public List<answers>getinput(var inp)
{
................
..................
}

Here the function is not accepting the var. What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

单挑你×的.吻 2024-09-26 09:42:22

var只能在同一个语句中声明和初始化局部变量时使用;该变量不能初始化为 null、方法组或匿名函数。

MSDN:隐式类型局部变量

var can only be used when a local variable is declared and initialized in the same statement; the variable cannot be initialized to null, or to a method group or an anonymous function.

MSDN : Implicitly Typed Local Variables

幽梦紫曦~ 2024-09-26 09:42:22

var 用于类型推断,而不是声明动态变量。使用实际输入类型作为 inp 的类型。

var is used for type inference, not to declare a dynamic variable. Use the actual input type as the type for inp.

撧情箌佬 2024-09-26 09:42:22

它不接受第三行,因为您的函数属于 void 类型,并且您尝试将该函数的结果分配给 listview1.itemsource

It's not accepting the third line because your function is of the type void and you try to assign the result of that function to listview1.itemsource.

芸娘子的小脾气 2024-09-26 09:42:22

正如其他人所说,您正在混合隐式类型变量(类型推断)和显式类型函数签名。

您应该拥有的是:

var Input = txtQuestion.text; // Implicitly typed variable of type string
listview1.itemsource = getinput(Input); 

// Strongly typed method taking string, returning List<answers> 
public List<answers>getinput(string question) 
{ 
    var result = new List<answers>();
    result.Add(answer);
    return result; 
} 

抱歉,如果这与您的代码不完全匹配,但它应该演示您所追求的内容。

var 关键字用于从赋值运算符的右侧推断变量的类型。在方法的签名中,没有赋值运算符,因此无法进行推理。此外,您始终可以传递任意数量的从基类派生的类型,这将使​​编译器难以确定参数的正确类型。 (您是指 DbReader、SqlDbReader 还是 IDbReader?)

可以推断变量。参数不能。

As others have said, you're mixing implicitly typed variables (type inference), and an explictly typed function signature.

What you should have is:

var Input = txtQuestion.text; // Implicitly typed variable of type string
listview1.itemsource = getinput(Input); 

// Strongly typed method taking string, returning List<answers> 
public List<answers>getinput(string question) 
{ 
    var result = new List<answers>();
    result.Add(answer);
    return result; 
} 

Sorry if this doesn't exactly match your code, but it should demonstrate what you're after.

The var keyword is used to infer the type of a variable from the right-hand side of the assignment operator. In a method's signature, there's no assignment operator, so inference can't take place. Further, you could always pass any number of types derived from a base class, which would make it difficult for the compiler to determine the correct type of the argument. (Did you mean DbReader, SqlDbReader, or IDbReader?)

Variables can be inferred. Parameters cannot.

情绪失控 2024-09-26 09:42:22

var 只是在 JavaScript 代码中用作变体。如果您使用 var 那么您可以使用字符串或使用对象。

public void getinput(object inp) 
{ 
    ................ 
    .................. 
} 


public void getinput(string inp) 
{ 
    ................ 
    .................. 
} 

var is just used in the JavaScript code as a variant. If you are using var then you can use string or use object.

public void getinput(object inp) 
{ 
    ................ 
    .................. 
} 


public void getinput(string inp) 
{ 
    ................ 
    .................. 
} 
爺獨霸怡葒院 2024-09-26 09:42:22
public void getinput(object inp)
{
................
..................
}

只要 C# 是强类型语言,编译器就始终知道您的变量属于什么实际类型:

var Input = ....

.... 的类型始终是已知的。这就是为什么你不能声明

var a;

,而这正是你想要做的

public void getinput(var inp)
{
    ................
    ..................
}
public void getinput(object inp)
{
................
..................
}

As soon as C# is strongly-typed language, the compiler always knows, what real type your variable belongs to:

var Input = ....

Type of .... is always known. That's why you can't declare

var a;

and this is EXACLTLY what you are trying to do in

public void getinput(var inp)
{
    ................
    ..................
}
飘逸的'云 2024-09-26 09:42:22

在函数中使用 object 而不是 var。然后将其转换为函数内适当的类型。

Use object in the function instead of var. Then cast it to the appropriate type within the function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文