Home > Net >  Shows that not all of the code path has a return value, beg god for help, thank you very much
Shows that not all of the code path has a return value, beg god for help, thank you very much

Time:10-04

 public static int process exists (string I) 
{
Process [] ps=Process. GetProcesses ();
Foreach (Process p in ps)
{
String info="";
Info=p.P rocessName;

If (I==info)
{
return 1;
}
The else
{
return 0;
}

}

}

Shows that not all of the code path has a return value, beg god for help, thank you very much

CodePudding user response:

 
If there is a public static int process (string I)
{
Process [] ps=Process. GetProcesses ();
Foreach (Process p in ps)
{
String info="";
Info=p.P rocessName;

If (I==info)
{
return 1;
}
The else
{
return 0;
}

}
Return 0

}


CodePudding user response:

Methods may appear may not return a value, 1/f, positive solutions

CodePudding user response:

Could not perform foreach operation, there would be no return value, advice and methods to define a temporary variable in the body, if the assignment, and then return to come out,

CodePudding user response:

To this, otherwise the first is not matching, it returns 0
 public static int process exists (string I) 
{
Process [] ps=Process. GetProcesses ();
int result=0;
Foreach (Process p in ps)
{
String info="";
Info=p.P rocessName;
If (I==info)
{
Result=1;
break;
}
}
return result;
}

CodePudding user response:

Or like this:
 public static int process exists (string I) 
{
Process [] ps=Process. GetProcesses ();
If (ps. Any (p=& gt; P.P rocessName==I))
{
return 1;
}
The else
{
return 0;
}
}

CodePudding user response:

1 # positive solution

CodePudding user response:

The rule is simple,
Because you can direct the Foreach,
The compiler cannot determine the ps have value,
So, on the outside of the foreach need a return, to represent if not into the traversal, so too does return a value

CodePudding user response:

 
Public static bool ProcessIsExisted (string processName)
{
Process [] ps=Process. GetProcesses ();
Foreach (Process p in ps)
{
If (p.P rocessName==processName)
{
return true;
}

}
return false;
}


Rewrite and compare yourself,

CodePudding user response:

Do you want to ensure that any case the return value, ps. Not a Length=0

CodePudding user response:

You finally add a return is not good, you should consider to whether have returned may be good

CodePudding user response:

# 4, # 5, # 8 is the truth, no problem with the other compilation, logic has a problem,

CodePudding user response:

references on 7th floor is wrath of god reply:
is simple,
Because you can direct the Foreach,
The compiler cannot determine the ps have value,
So outside the foreach, need a return, to represent if not into the traversal, then will return a value


refer to the eighth floor Amon Amon response:
 
Public static bool ProcessIsExisted (string processName)
{
Process [] ps=Process. GetProcesses ();
Foreach (Process p in ps)
{
If (p.P rocessName==processName)
{
return true;
}

}
return false;
}


Rewrite and compare yourself,

The same way,

CodePudding user response:

Your Process. GetProcesses is likely to return to the set of is zero, so the following foreach into not to go, and then you return are foreach, answer to upstairs, suggest you change to Bool return values, defined in advance a Boolean result=false, and then return to the reuslt

CodePudding user response:

 
If there is a public static bool process (string I)
{
The return Process. GetProcesses (). The Exists (d=& gt; D.P rocessName==I);
}

CodePudding user response:

If you don't want to do this you need to make sure that your server can certainly connected to the Internet, and then through the CMD ping or nslookup get returned by IP
  •  Tags:  
  • C#
  • Related