-
December 30th, 2013, 03:33 AM
#1
if then statement and switch statement
Hi all,
see the following:
Code:
public static bool IsInstallerRunning(string arg)
{
Process[] myProcesses = null;
myProcesses = Process.GetProcesses();
if (arg == "Install")
{
foreach (Process SingleProcess in myProcesses)
{
if (SingleProcess.ProcessName.Equals("setup") == true)
return true;
}
return false;
}
if (arg == "Uninstall")
{
foreach (Process SingleProcess in myProcesses)
{
if (SingleProcess.ProcessName.Equals("uninstall") == true)
return true;
}
return false;
}
}
this gives me an error, "not all code paths return a value"
the arg can have only two values either "Install" or "Uninstall", so all paths return a value.
Where do I go wrong?
If I do this with the switch statement I get a more or less the same error/warning.
regards,
Ger
-
December 30th, 2013, 04:06 AM
#2
Re: if then statement and switch statement
What if "arg" value is neither "install" nor "Uninstall"? This is precisely what your code is complaining about.
-
December 30th, 2013, 07:21 AM
#3
Re: if then statement and switch statement
Hi,
It is ALWAYS one of the two !!
-
December 30th, 2013, 08:22 AM
#4
Re: if then statement and switch statement
Well, that may be true, but the compiler does not know about that. You need to let the compiler know that it will always be 2 values (like using an Enum).
-
December 30th, 2013, 01:32 PM
#5
Re: if then statement and switch statement
Originally Posted by TBBW
Hi,
It is ALWAYS one of the two !!
Is it? Consider:
Code:
var result = IsInstallerRunning("something random");
The compiler has to know the possibilities at COMPILE time, not at runtime, so even though you know the string can be only one of two values, the compiler doesn't know.
At any rate, you can simply return false; at the end of the method (or true).
Or... better yet, don't use strings, use an enum as Shuja mentioned. In fact, if you have a fixed set of string values like you do in this case, then create an enum and use Enum.Parse to convert the string into the enum. Then only use the enum in methods.
When I see folks comparing and switching on strings in code, it makes me gag. Not only is it error prone (because comparisons are runtime), but it's also slower than most other comparisons like ints or enums.
Structure your code to allow the compiler to catch as many compile time errors. A good way to do this is to get rid of string comparisons as much as possible.
-
December 30th, 2013, 01:39 PM
#6
Re: if then statement and switch statement
can you put the enum in my code to see how to use it?
-
December 30th, 2013, 01:50 PM
#7
Re: if then statement and switch statement
Originally Posted by TBBW
can you put the enum in my code to see how to use it?
Really? Okay, I'll do part of it, but you'll need to look up how to use Enum.Parse() yourself (and note the case insensitive method overload).
Code:
public enum InstallerMode { Install = 0, Uninstall = 1 }
public static bool IsInstallerRunning(InstallerMode mode)
{
foreach (var process in Process.GetProcesses())
{
if (process.ProcessName.Equals( InstallerMode.Install == mode ? "setup" : "uninstall" ))
{
return true;
}
}
return false;
}
-
December 31st, 2013, 04:59 PM
#8
Re: if then statement and switch statement
Hi All,
Thanks for all the info.
I use the strings to keep my code readable. (and is some parts flow control)
Can I put it like this;
in stead of using strings for readability and flow control, it is better to use the enum.
While using the enum constants, you can still use words iso of numbers to keep the readability.
and the "constant strings" will be replaced by integer (or other type) to speed up code.
and using enum will decrease the change of making mistakes.
Correct?
Anyway will have a go using enum.
for now "happy new year"
Ger
-
January 1st, 2014, 01:31 AM
#9
Re: if then statement and switch statement
Hi there,
New user here still in the process of learning C#.
How did you go about using the first code to see what the issue was?
If I copy the code and stick it in a c# windows form I get all sorts of issues.
Only asking because I believe this type of thing will help with my development.
Thanks,
Cess
-
January 1st, 2014, 01:11 PM
#10
Re: if then statement and switch statement
If you are learning C#, then you don't need to worry about runonning the installer or how to eliminate it. The concept works, but only in HIS code
-
January 1st, 2014, 03:59 PM
#11
Re: if then statement and switch statement
Originally Posted by TBBW
Hi All,
Thanks for all the info.
I use the strings to keep my code readable. (and is some parts flow control)
Can I put it like this;
in stead of using strings for readability and flow control, it is better to use the enum.
While using the enum constants, you can still use words iso of numbers to keep the readability.
and the "constant strings" will be replaced by integer (or other type) to speed up code.
and using enum will decrease the change of making mistakes.
Correct?
Anyway will have a go using enum.
for now "happy new year"
Ger
Yes, pretty much. I believe in general, you want to leverage the compiler as much as possible and structure your code so that you get compile time errors instead of runtime errors. For example, some folks use an array of objects and put different types of objects into the array and then check the type of object at runtime to decide what to do with it. Unfortunately, if you put an unexpected object into the array, it becomes a runtime error. Instead, if you use a generic collection, then it's type safe and, you'll get a compile time error, if you try to insert an object of a different type. The strategy of striving for compile time errors is just one way of making your program more robust. Another area is to try not to compare strings in program flow as I've mentioned previously. Strings can make your program readable as you say, but you can achieve readability with well named variables and using an enum. Magic numbers are to be avoided as well. Consider:
Code:
switch(mode)
{
case 0:
// do something
break;
case 1:
// do something else
}
versus
Code:
switch(mode)
{
case DisplayMode.Active:
// do something
break;
case DisplayMode.Inactive:
// do something else
}
The second approach is definitely more readable. It's also type safe so you do need to check the range of mode. If you use a number, then you have to clutter the code up with checks to make sure that the value of mode is only 0 or 1. You found this out when you used the string "Install" and "Uninstall". Also, with strings you have to consider case sensitivity. "Install", "install", and "INSTALL" all mean different things. If the data is coming from an external source, you need to consider the casing. If you use an enum, you don't need to worry about it.
Another area to consider for readability and maintainability is follow C# naming conventions. Use _camelCase or camelCase for class fields, camelCase for method parameters, and PascalCase for class names, class properties and method names.
You are in charge of your code, so you can follow any naming convention you want or invent your own, but if you expect others to maintain your code, following the recommended convention will allow others to ramp up quickly because clear what your intentions are. Btw, if I'm writing code in other languages like javascript or jquery, I follow that convention for naming and brace placement. Whether you follow the typical convention or not is up to you, but by all means whatever you do, be consistent. Nothing is harder to follow that someone writing code with mixed convention throughout their code.
-
January 2nd, 2014, 02:12 PM
#12
Re: if then statement and switch statement
Originally Posted by dglienna
If you are learning C#, then you don't need to worry about runonning the installer or how to eliminate it. The concept works, but only in HIS code
Thanks for the reply dglienna however I'm not so much concerned about the installer as much as being able to know how others troubleshoot.
Once again if someone could chime in as to how to used the first code themselves I'd appreciate it.
Thank you,
Cess
-
January 2nd, 2014, 04:59 PM
#13
Re: if then statement and switch statement
Originally Posted by cessation
Thanks for the reply dglienna however I'm not so much concerned about the installer as much as being able to know how others troubleshoot.
Once again if someone could chime in as to how to used the first code themselves I'd appreciate it.
Thank you,
Cess
Based OP's method naming, he is interested in finding out if an installer is running during operation of his code. Why he needs this isn't clear.
If you are looking for tips how to troubleshoot, then the best advice I can give is to learn how to use a debugger to debug your code. You might have heard this term (or already know how to do it), but debugging in a nutshell is having the ability to see program flow and look at code variables while your program runs. In Visual Studio, you do this by putting break points on the line of code you are interested in (click on the line and press F9) and then start debugging the program by pressing F5. When you program runs, you do whatever you need to do to get it to hit the breakpoint (I say this because GUI apps might need the user to do something like click on a button). When you hit the breakpoint, Visual studio will stop on the line of code and let you inspect the current variables and single step through the code (F10, F11).
Learning this approach is the #1 Tip I have for learning how to troubleshoot.
-
January 3rd, 2014, 03:42 PM
#14
Re: if then statement and switch statement
-
January 4th, 2014, 04:20 PM
#15
Re: if then statement and switch statement
But note that, depending on how you construct your code, even using enums can give you the same error. Enums can be type-casted from integers, and so can end up having values you didn't specify in the enum declaration. The compiler still can't be sure that the caller will only use a limited set of values.
So, in both cases, you have to have additional code to handle the exceptional situation. You can, say, throw an exception (it is, after all, an exceptional situation), or maybe use a default value, or simply do nothing (except for potentially making sure that your app is in a valid state when the method exits). This depends on what you want to do, and on what the requirements for the program are.
If you're using two separate if statements such as in the code at your first post (or, better, an if-else-if construct), then you can just append a throw statement at the bottom, or, if you don't want to throw an exception, remove the "return false;" part out from both if-blocks, and put it at the bottom of the method instead, outside of both if-blocks.
If you are using the switch statement, you can do the same thing, after the switch-block, or you can use the "default:" case as your last case label, and handle the anomalous entry there.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|