If / Else statement, how to say "DoNothing" or "Continue" [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have an IF / ELSE statement, although I would like to know how to tell the "else" part to do nothing if it is true. e.g.:
if(x == x)
//run calc.exe
else
//DoNothing
Or I am write in saying that if I just remove the else statement, it will continue anyway if the if condition is not matched?

just omit the else
if(condition)
{
do_something();
}
//go on with your program

Yes. An non existant else statement is the same as an empty one.
This is common to all C-like languages.

If there's nothing to do then just omit the else:
if (some condition)
{
do stuff
}
continue is used in loops to short-circuit the rest of the this iteration of the loop and break is used to terminate the loop early.

If you don't put an else part and the if condition is not met the program will continue executing its flow:
if (SomeCondition)
{
// Do something if the condition is not met
}
// continue executing

Related

Error of List<T>.RemoveAt() [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
lock("data"){
if(_requestList.Count>1 && _requestList[1]==null){
Debug.Log("why0");
}
_requestList.RemoveAt(0);
if(_requestList.Count > 0 && _requestList[0] == null){
Debug.Log("why1");
}
doSomething ();
}
_requestList is a List of string
Sometimes it logs "why0", sometimes "why1", and sometimes both, and sometimes nothing.
The elements added is never null.
So why?
First of all you need to use a
private readonly object lockObject = new object();
lock(lockObject)
{
}
You also need to put a lock(lockObject) around anywhere where you are adding to the list.
Hope that helps in some regard.
You could also look into a ConcurrentBag if you don't want to worry about threading.

What should I use in this case? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I need to go through two arraylists (for the ones answering my previous question, I've solved it), one ("dir") has text saved on it which will be used as an Instruction, and the other one ("time") has numbers in it wich will be used for the Interval of a Timer ("Timer.Interval"). What I need to do is to make each position of "Dir" be done continiously until the timer ticks (Timer.Tick), then go to the next position.
This is as far as I got:
int i = 0;
for ( i = 0; i > -1; i++)
{
Console.WriteLine(cls.dir[i]);
Console.WriteLine(cls.time[i]);
if (cls.dir[i] == "one")
{
timerSeconds.Interval = Convert.ToInt32(cls.time[i]) * 1000;
Label1.Text = "Hello StackOverflow";
}
}
In this case to make it more easy to understand I made and example, what it's supposed to do is to show a different text as a sign in the Label1 as long as the time Arraylist says for each block. I know this is incomplete, how would you end it?
It sounds like you want to "wait" a certain number of milliseconds.
If so, the best way to do this is with a "Sleep":
if (cls.dir[i] == "one")
{
int interval = Convert.ToInt32(cls.time[i]) * 1000;
Label1.Text = "Hello StackOverflow";
System.Threading.Thread.Sleep(interval);
}
Label1.Text = "Time to wake up";

c# string confusion [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am fairly new to C# and am confused about something.....
Let me show you whats happening and hopefully you guys can tell me what im doing wrong here.
string incomming = Encoding.UTF8.GetString(bytes);
//MessageBox.Show(incomming); shows me the string "stop", No problem
executeCommand(incomming);
public void executeCommand(string action)
{
MessageBox.Show(action + " was recieved"); // shows the string "stop", No problem here... that works
switch (action)
{
case "start":
MessageBox.Show("start was recieved"); //shows nothing
break;
case "stop":
MessageBox.Show("stop was recieved"); //shows nothing
break;
}
}
With out knowing what the contents of the Byte array that is being converted to a String it is very hard to give you anything help you. But here are a few things to try.
You can put a breakpoint on executeCommand(incomming) and in your watch window type incomming.ToCharArray(), you will need to click on the green circle in the value column before you can see the characters that are apart of the string. This should let you know what you are dealing with.
You can search incomming for a match of the string you are looking for by using the String.Contains Method.
if (action.Contains("stop"))
MessageBox.Show("stop was recieved");
else if (action.Contains("start"))
MessageBox.Show("start was recieved");
The only reasonable explanation for this is that one of your "stop" strings has a letter in a diferent language from english but looks the same. In this case:
"stop" != "stоp"
is true;

How to deal with errors in function nesting and calling functions within other functions [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Scenario:
I have to check for user input (a string) that shouldn't contain <.%?/ symbols and if it does I remove them. I have 20 different places where I've to check it, actually, 20 different pages with each 20 different controls.
So I wrote a function like the following shortened example:
public string MyFunction (string userinput)
{
return userinput.replace("<"," ");
}
Now if I want to call this function from within another function and there's an error in this function's try catch block I want it to write an error to a Label on the main page, without interrupting the second function.
Also i am thinking of implementing conditional statement function calls too.
Usually it's best to let the calling function catch your error and to act upon it. Something like this (assuming C#, but your question wasn't clear about that and didn't have a working example):
try {
string resplacedString = yourReplaceFunction(userInput);
} catch (MyException e) {
Label1.Text = "An error occurred." + e.Description;
}

How many times is DOSTUFF() called? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
try
{
for (int i = 0; i <10; i++)
{
DoStuff();
if (i>3 && 1== i% 2)
{
throw new Exception();
}
}
}
catch (Exception ex)
{
DoOtherStuff();
}
DOSTUFF() is called 0 times because C# method names are case-sensitive.
If you really meant DoStuff(), then assuming it is not an unimplemented partial method and the method itself is not marked with a conditional attribute specifying an undefined symbol, then it will be called 6 times as in #MikeCito's answer.
6 times. When i hits 5 the remainder will be 1 and it will be greater than 3.

Categories