相关疑难解决方法(0)

C#:并非所有代码路径都返回一个值

我正在编写一个简单的WinForms应用程序,我允许用户在TreeView控件中拖动TreeNodes.我强制执行的规则之一是不允许用户将TreeNode拖动到其自己的子节点之一.我以递归样式编写了以下函数来检查目标节点的父级.在编译时,我得到错误,并非所有代码路径都返回此函数的值.据我所知,我对这个逻辑的每个可能的分支都有一个回复声明......但我显然是错的.请有人指出我的错误.

    private bool IsDestinationNodeAChildOfDraggingNode(TreeNode draggingNode, TreeNode destinationNode) {
        if (draggingNode.Nodes.Count == 0) 
            return false;
        else {
            if (draggingNode.Nodes.Contains(destinationNode)) 
                return true;
            else {
                foreach (TreeNode node in draggingNode.Nodes) 
                    return IsDestinationNodeAChildOfDraggingNode(node, destinationNode);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

c# compilation

3
推荐指数
2
解决办法
1万
查看次数

编码样式 - 输入验证

哪个是验证传递给函数的输入的最佳方法,即在继续执行某些操作之前验证所有输入

class A;
void fun(A* p)
{
  if(! p)
  {
    return;
  }

 B* pB = p->getB();
  if(! pB)
  {
    return;
  }

.......

}
Run Code Online (Sandbox Code Playgroud)

或者你这样写:

void fun(A* p)
{
  if(p)
  {
    B* pB = p->getB();
    if(pB)
    {
      .....
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我问这个是因为,如果我使用第一种风格,那么我的代码中会有多个返回语句,很多人说这些语句很糟糕(不知道为什么),如果我使用第二种风格则会有太多级别嵌套在我的代码中.

c++ coding-style

2
推荐指数
1
解决办法
761
查看次数

为什么不在VB.NET中使用'Return'语句?

我已经获得了一些代码来查找可以改进和更改的问题和事情(这是一项家庭作业,但这个问题与任务本身无关),部分代码是:

Function CheckIfSameCell(ByVal FirstCellPosition As CellReference, ByVal SecondCellPosition As CellReference) As Boolean
    Dim InSameCell As Boolean
    InSameCell = False
    If FirstCellPosition.NoOfCellsSouth = SecondCellPosition.NoOfCellsSouth And FirstCellPosition.NoOfCellsEast = SecondCellPosition.NoOfCellsEast Then
        InSameCell = True
    End If
    CheckIfSameCell = InSameCell
End Function
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么InSameCell创建变量,只能将其赋值给函数名CheckIfSameCell

或者只使用如下所示的return语句?

Function CheckIfSameCell(ByVal FirstCellPosition As CellReference, ByVal SecondCellPosition As CellReference) As Boolean
    If FirstCellPosition.NoOfCellsSouth = SecondCellPosition.NoOfCellsSouth And FirstCellPosition.NoOfCellsEast = SecondCellPosition.NoOfCellsEast Then
        Return True
    End If
    Return False
End Function
Run Code Online (Sandbox Code Playgroud)

我可以理解不If直接在语句中返回表达式,以提高可读性.

我知道为函数名称分配返回值并不会退出函数,而Return会这样做,但它只是一个人的风格,还是第一个版本有任何优势(IMO,第二个版本更具可读性)?

vb.net return

1
推荐指数
1
解决办法
1万
查看次数

Inline If语句中的默认条件

我在我的c#代码中获取数据库行.Row包含3个不同的标志(3列具有true或false值).这些列中只有一列为真,这将决定该对象的类型.如何在一行代码中确定该对象的类型.如果所有三个标志都是假的,那么我需要一个默认类型.

var myObject = this.unitOfWork.myRepository.GetMeObject();

 var objectType = myObject .IsA == true
                              ? "A"
                              : myObject .IsB == true
                                    ? "B"
                                    : myObject .IsC == true
                                          ? "C"
                                          : "D";
Run Code Online (Sandbox Code Playgroud)

如果条件均无效,则ObjectType应为D.

任何建议将不胜感激.

谢谢

.net c# asp.net-mvc-3

1
推荐指数
1
解决办法
467
查看次数

为什么第二个IF语句是检测到的Unrachable代码?

TaskCompletionSource<bool> sy;
        public string SendResponse(HttpListenerRequest request)
        {
            string result = "";
            string key = request.QueryString.GetKey(0);
            if (key == "cmd")
            {
                if (request.QueryString[0] == "upload status")
                {
                    if (Youtube_Uploader.fileuploadstatus == "uploading file")
                    {
                        Youtube_Uploader.fileuploadstatus = "";
                        return "uploading";
                    }
                    else
                    {
                        return "upload unknown state";
                    }

                    if (Youtube_Uploader.fileuploadstatus == "file uploaded successfully")
                    {
                        Youtube_Uploader.fileuploadstatus = "";
                        return "upload completed";
                    }
                    else
                    {
                        return "upload unknown state";
                    }
                }
                if (request.QueryString[0] == "nothing")
                {
                    return "Connection Success";
                }
                if (request.QueryString[0] == "start") …
Run Code Online (Sandbox Code Playgroud)

.net c# winforms

1
推荐指数
2
解决办法
84
查看次数

为什么在此函数中返回false

在下面的程序中,我将变量设置thtrue第二个if语句中的变量.我很好奇为什么它后来又回来了false.

public boolean nodeExist(TreeNode Tree, T value){

    boolean th = false;

    if(Tree.getValue()!= null){

        if(value == Tree.getValue()){

            th = true;

        }else{

            if(value.compareTo((T) Tree.getValue()) < 0){

                nodeExist(Tree.getLeft(), value);

            }else{

                nodeExist(Tree.getRight(), value);
            }

        }

    }else{

        th = false;

    }

    return th;

}
Run Code Online (Sandbox Code Playgroud)

java

0
推荐指数
1
解决办法
149
查看次数

在 Linux 内核代码中使用 goto 没有意义

我正在浏览 Linux 源代码,在这里我偶然发现了这个功能:

static int check_free_space(struct bsd_acct_struct *acct)
{
    struct kstatfs sbuf;

    if (time_is_after_jiffies(acct->needcheck))
        goto out;

    /* May block */
    if (vfs_statfs(&acct->file->f_path, &sbuf))
        goto out;

    if (acct->active) {
        u64 suspend = sbuf.f_blocks * SUSPEND;
        do_div(suspend, 100);
        if (sbuf.f_bavail <= suspend) {
            acct->active = 0;
            pr_info("Process accounting paused\n");
        }
    } else {
        u64 resume = sbuf.f_blocks * RESUME;
        do_div(resume, 100);
        if (sbuf.f_bavail >= resume) {
            acct->active = 1;
            pr_info("Process accounting resumed\n");
        }
    }

    acct->needcheck = jiffies + ACCT_TIMEOUT*HZ;
out: …
Run Code Online (Sandbox Code Playgroud)

c goto linux-kernel

-1
推荐指数
1
解决办法
358
查看次数

以下哪种技术更好?

假设我有三个条件:Condition1,Condition2,Condition3.如果满足所有条件,则方法/函数返回true,否则返回false.

技术一:

     function check(){
        if(Condition1 is true AND Condition2 is true AND Condition3 is true){
           return true;
        }
        return false;
     }
Run Code Online (Sandbox Code Playgroud)

技巧二:

      function check(){
         if(Condition1 is false){
             return false;
         }
         if(Condition2 is false){
             return false;
         }
         if(Condition3 is false){
             return false;
        }

         return true;
      }
Run Code Online (Sandbox Code Playgroud)

哪种技术会更好?

javascript coding-style

-3
推荐指数
1
解决办法
187
查看次数

退货声明和缩进

我很难理解这个return陈述.

布尔测试下面if not test(a)是false(means if test(a)is true),else语句返回b.

但就在它返回之后a,重写有价值a,不是吗?

def proc4(a, b):
    if not test(a):
        b = 'udacity'
    else:
        return b
    return a
Run Code Online (Sandbox Code Playgroud)

python return

-3
推荐指数
1
解决办法
4410
查看次数