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;
Related
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.
I am making a app to generate a password
Now I have a character list which use the following characters when all options for generate a password are null:
chars = "$%##!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
Next I have a else with some options like Punctuation and Symbols, so then he is not using the standard characters:
var listOfCharacters = string.Empty;
foreach (var currentOption in options)
{
switch (currentOption)
{
case GenOptions.Punctuation:
listOfCharacters += "&#.#%!*?;:,";
break;
case GenOptions.Symbols:
listOfCharacters += "^$";
break;
default:
break;
}
}
chars = listOfCharacters.ToCharArray();
listOfCharacters.Remove('i'); listOfCharacters.Remove('!');
listOfCharacters.Remove('l'); listOfCharacters.Remove('1');
listOfCharacters.Remove('O'); listOfCharacters.Remove('o');
listOfCharacters.Remove('0'); listOfCharacters.Remove('q');
listOfCharacters.Remove('p'); listOfCharacters.Remove('g');
listOfCharacters.Remove('I');
Now I want to build in a option to remove Similar characters so characters who are almost a copy from each other like o and 0. I have want to do this with listofcharacter.remove but when I use it like above it gives me a error because its null/empty. Now how can I fix this?
Not sure about null/empty error messages.
You are trying to use String.Remove, which takes character index as an argument. So, when you pass 'i' it is first converted to int and then String.Remove tries to delete character with that index. As far as I understand it's not what you want it to do.
You can use String.Replace instead:
String.Replace('!',String.Empty);
Try this:
listOfCharacters.Replace('i',' ');
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 coding program, and stacked. Please can give me a code which search text in file from one specific symbol to another using C# visual Windows Forms , not console application. Like this text in textfile c:\id.txt
The entry was successfully copied to {ea4c4653-cc65-11e1-a2fc-001e101f4e71}.
search string from { to } , and result with { and }, without . at the end. And send found text in a message box.Code to search text in a file an send whole line in message box. But i need part of line.
Regex can be useful:
MessageBox.Show(
Regex.Match(inputString, "\{(?<path>[^}]*)\}").Groups["path"].Value);
explain:
{ '{'
[^}]* any character except: '}'
(0 or more times, matching the most amount possible)
} '}'
Try by using regular expressions:
var line = " The entry was successfully copied to {ea4c4653-cc65-11e1-a2fc-001e101f4e71}.";
var foo = Regex.Match(line, #"to\s*\{([^}]+)\}");
if(foo.Success) {
MessageBox.Show(foo.Groups[1].Value); //ea4c4653-cc65-11e1-a2fc-001e101f4e71
} else {
//not found value
}
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 have php code that puts some values into an Array as follows:
$hunter=addslashes($MessageArray[1]);
$time=addslashes($MessageArray[2]);
I wrote the same code in C# and wanted to know if it was correct.
string Hunter = Messagearray[1].tostring();
string time = Messagearray[2].tostring();
As James mentioned, use Pascal casing:
string hunter = messageArray[1].ToString();
string time = messageArray[2].ToString();
Also, C# arrays are indexed starting at 0. You can change the starting index of arrays in PHP, but you can't in C#. Perhaps you do wish to take the 2nd and 3rd items, but keep it in mind. You might want:
string hunter = messageArray[0].ToString();
string time = messageArray[1].ToString();
As far as addslashes() goes, it will depend on your usage of hunter and time. If you're using them in a SQL statement, there are other ways of accomplishing the functionality of PHP's addslashes().
Snipped from Here
public static string AddSlashes(string input)
{
return System.Text.RegularExpressions.Regex.Replace(input, #"(\\)([\000\010\011\012\015\032\042\047\134\140])", "$2");
}
Usage:
//
var Messagearray = new object[] { "item 0", 1 };
var hunter = AddSlashes(Messagearray[0].ToString());
var time = AddSlashes(Messagearray[1].ToString());
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;
}
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