C# check if variable only contains given string - c#

I am trying to use a variable called game.PlayMode which contains a semicolon-separated list of Play Modes input by the user. The variable can literally contain anything.
An example value:
Single Player; 2-Player; 3-Player; Co-Op; Versus; Alternate
I want to check if game.PlayMode ONLY contains Single Player but I am not sure how to do that.
So far:
if (game.Playmode.Contains("Single Player"))
{
Players = "1";
}
But I know this will also return true with the example value above. How can I make sure my code returns true only if the string is an exact match?

If the variable Playmode only contains "Single Player" it is equal to that string, right?
if (game.Playmode.Trim().Replace(";", "") == "Single Player")
{
Players = "1";
}
.. and just in case if there is a semicolon, let's remove it.

In C#, you can simply use the == operator:
if (game.Playmode == "Single Player")
{
Players = "1";
}
It might be a better idea to remove any ; and any white spaces and only then do the comparison:
if (Playmode.Replace(";","").Trim()=="Single Player")
{
Players = "1";
}

Related

How do I compare the elements in the array with the elements entered in the textbox sequentially in c#?

My goal is to actually make a guessing game, so I created two arrays with Mysql data called answers and questions. And what I want to do is take the value from the user and if it is true, for example my first answer 'fashion' matches the guess the user entered in the textbox, I want the label to write correct and continue with the next answer and try to find the next answer
My code returns true when I enter my values ​​in the array into the textbox, but I want them to be in order. How do you think I can use the for loop. How do you think I can use the for loop to make an ordered comparison?
for (int i=0;i<cevaplar.Count;i++)
{
string tahmin = textBox1.Text;
if(cevaplar.Contains(tahmin))
{
label1.Text = "true";
continue;
}
else
{
label1.Text = "false";
break;
}
}
}
In your code you use "cevaplar.Contains(tahmin)". With contains you're checking if tahim can be found anywhere in your array, without taking any order in account.
The solution to your problem should be quite simple. Just don't use contains in this situation but use a simple indexer to compare the elements. Try the following:
Replace:
if(cevaplar.Contains(tahmin))
{
...
}
With
if(cevaplar[i] == tahim) //here you check only if the i'th element is matching.
{
...
}
Good luck!

Replacing Dictionary with function

I'm making a game with character attributes, like strength, agility, etc. I have them all in a <string, double> dictionary. I need to implement "mods" to attributes from equipment, buffs, etc. I'd like to replace all instances of "CurrentStats[Key]" with a function(string input) of some sort, via Find/Replace. (I have HUNDREDS of references to it, I can't do that all by hand)
So basically, I'm looking for a way to write a function where I can somehow write
Function("Strength") = 5; for assignment
while still being able to use
if(Function("Strength") == 5) for fetching.
Is this possible?
Visual Studio has inbuilt regular expressions in its search and replace. You just need to enable the option when doing your replace.
You can probably just perform a global search and replace of something like CurrentStats\[([^\]]+)\] to MyFunction($1).
Explanation: this searches for the literal string CurrentStats[], with the content between the two brackets being a group (referenced in the replace as $1), indicated by having it surrounded by ( and ), containing [^\]]+, or, "a character group of anything that's not the closing quote ], repeated one or more times".
Note that this won't work if the key itself could contains something like myArray[i] since then it'll obviously match the closing bracket of that. Regex isn't really good at doing matching brackets or tags.
The locigcal approach would be to have a class for your character. You can use properties. Functions are usually called with arguments if you want to assign something, but properties can be assigned directly.
public class Character {
private bool overpowered = false;
private int _strength = 0;
public int Strength
{
get { return this._strength; }
set {
if (value > 10) { overpowered = true; }
this._strength = value;
}
}
// [...]
}
Then to use the property simple access it via the object:
Character c = new Character();
c.Strength = 5;
if (c.Strength == 5) { /* ... */ }
Using a dictionary makes little sense here.
It looks like ref returns are what I was after. I don't know how to mark this as answered. First post here. Thanks everyone

How to evaluate single string in IF c#

is it possible to evaluate a single string in c#. The string itself will only be determined during run-time and therefore cannot be set before hand. please see example:
var a = "a == b";
if(a){
//do something
}
EDITED:
This is a actual example of what i would like computed:
var evaluationToBeDone = "MUST_CE_I = \"MUST_CE_I\"";
if(evaluationToBeDone){
// i will do something if the above is true
}
I see what you're trying to do, but the approach doesn't make sense. When you make a variable into an object, the program only reads it as letters, not any logic inside of the object. Try doing this:
var a = "MUST_CE_I"
var b = "\"MUST_CE_I\""
if (a == b)
{
do stuff
}
I assume you want your second string to have the " " quotes, so this should give you what you need. Even though the if statement will always return false since the two variables are not equal.

Using Contains Method to separate comma delimited string of numbers

I have an IF statement as follows:
if (snumber == "9999-999-9999" && cnumber == "999")
{
// 30 Day Trial Demo Key
return "Good";
}
There's a serial number linked to one or multiple cnumbers. In some cases I have a list of 5-20 cnumbers seperated by commas, but that method will not work for what I'm doing. I believe I need to use the Contains method to let the program know any one of those values will work for that serial number. Any insight or work around?
Thanks
C#
You could split your list of cnumbers by comma and iterate the array checking each cnumber against your value.
if(snumber == "9999-999-9999")
{
var cnumbers = listOfCnumbers.Split(',');
foreach(var cnumber in cnumbers)
{
if(cnumber == "999")
{
return "Good";
}
}
}
Might want to replace the hardcoded strings with variables though

How can I exactly match a string inside of a larger string?

I have the following enum declared:
public enum EtcMethod
{
ACCORD,
COROLLA,
COROLLA_S,
CAMRY,
CIVIC
}
On my form, I have a handful of controls with their Tag property set:
myControl1.Tag = "ACCORD";
myControl2.Tag = "COROLLA";
myControl3.Tag = "CIVIC COROLLA_S CAMRY";
Then I'm checking the controls' tags in a loop to see if any of the values are found:
private void HideControls(EtcMethod etcMethod, LayoutControlGroup lcg)
{
foreach (BaseLayoutItem ctl in lcg.Items)
{
if (ctl.GetType() == typeof (LayoutControlItem))
{
LayoutControlItem item = (LayoutControlItem)ctl;
if (item.Tag.ToString().IndexOf(etcMethod.ToString()) >= 0)
item.Visibility = LayoutVisibility.Always;
else
item.Visibility = LayoutVisibility.Never;
}
}
}
But the problem with this is, for example, if etcMethod is COROLLA and item.Tag.ToString() is "COROLLA_S" that'll erroneously pass the check.
How can I make sure that it'll find an exact match instead of a "partial" match? In other words, I would like it to behave as if you checked off the "Match whole word" option using Visual Studio's Find feature.
The only solution I could think of would be to check the value of the character at etcMethod.Lenght+1 and see if it's a space (indicating the beginning of another enum value) or if that position even exists (indicating the end of the tag), but that seems particularly sloppy.
Why don't you Split it and use Contains ?
if (item.Tag.ToString().Split().Contains(etcMethod.ToString()))
This will first split your Tag on space, if it hasn't space it just turn it into a string array, then using Contains on array will look for exact match.
This may be one solution:
if (item.Tag.ToString() + " ").IndexOf(etcMethod.ToString() + " ") >= 0)

Categories

Resources