“并非所有代码路径都返回值"是什么意思?

编程入门 行业动态 更新时间:2024-10-27 10:32:18
本文介绍了“并非所有代码路径都返回值"是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

此代码在col1_button_click(int count)上给出错误,并非所有代码路径都返回一个值" 这是什么意思?如何删除此错误?请问有人可以帮助我吗?

this code gives an error on col1_button_click(int count), "not all code paths returns a value" what does it mean?how can i remove this error?pls can any one help me?

public int returnVal1; private void button1_Click(object sender, EventArgs e) { returnVal1= col1_button_click(clickCountC1); } private int col1_button_click(int count) { if (count == 0) { count = 1; return count; } else if (count == 1) { count = 2; return count; } else if (count ==2) { count = 0; return count; } }

推荐答案

计数为3时会发生什么? 在这种情况下,您的代码将不会返回任何内容-而这正是错误的含义. 使用return语句时,必须涵盖所有情况. What happens when count is, say, 3? In that case your code will not return anything - and that is exactly what the error means. Every case must be covered while using a return statement.

这意味着:函数中的 至少有一条路径没有return语句.现在,由于您已声明它返回了某些内容,因此您是个骗子,而我不会编译它" . It means: "in your function there is at least one path without a return statement. Now, since you have declared it returning something, you''re a liar and I won''t compile it".

这意味着您需要在最后一个花括号之前的return语句. 顺便说一句,我会用这种方法做同样的事情: It means you need a return statement just before the last curly brace. BTW, I''d do the same method this way: private int col1_button_click(int count) { count++; if (count >= 2) { count = 0; } return count; }

在一种方法中具有多个出口点是不好的做法.尽可能避免.

It''s bad practice to have more than one exit point in a method. Whenever possible, you should avoid it.

更多推荐

“并非所有代码路径都返回值"是什么意思?

本文发布于:2023-11-10 01:21:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   返回值   代码   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!