C# - Simpler/Shorter way to make an 'if-or' statement - if-statement

Is there any way to shorten this statement:
if(string.Equals("Hello") || string.Equals("Hi") || string.Equals("Hey")) { }
To something like:
if(string.Equals("Hello" || "Hi" || "Hey")) { }
It's not necessary, but can be handy.

Thanks to #thelaws who suggested using an array of the possible values and flipping the statement, which I got to work with:
if(new string[]{"Hello", "Hi", "Hey"}.Contains(value)) { }

if ((new List<string> { "Hello", "Hi", "Hey" }).Contains(yourValue))
{
//your code here
}
Here I created a list of strings with values Hello, Hi and Hey. Then I am just searching whether the value of variable yourValue is present in the created list.

Related

How to determine if JSON is object or array in cpp, and if object convert it to array?

I was searching for that, but found answers regarding java.
For a long time, I was receiving JSON responses as array, even when I had one response only.
Example:
{"intervention":
[
{
"id":"3",
"subject":"dddd",
"details":"dddd",
"beginDate":"2012-03-08T00:00:00+01:00",
"endDate":"2012-03-18T00:00:00+01:00",
"campus":
{
"id":"2",
"name":"paris"
}
}
]
}
now I can recive it either as an array, or as an object. meaning these [ , ] are no longer appear.
As you may guess, my code crushes as I'm using it as array..
I want to do something like that:
if (parsedJson["intervention"] == jsonObject])
covertObjectToArray
I've tried (pseudo) :
std::string tmp = parsedJson["intervention"].asString()
if (firstChar is "{")
{
concat : "[ + tmp + ] ";
parseStringBackToJSon
}
but it crashed!
can you please help?
If I get it right from your initial question and the comments you would like to transform an element in the JSon tree if it is a simple object into an array containing that object. I'm also not familiar with that lib but for me it would be something like.
Value & v = parseJSon["intervention"];
if(v.isObject()) {
Value vcopy = v;
v.clear();
v.append(vcopy);
}
Just fantasy code based on the API doc.

Opposite of 'is' in if statement?

I want to see if this statement is false:
if twData is Array {
}
isnt and isnot don't seem to exist.
This doesn't work:
if (twData is Array) == false {
}
So I'm not sure exactly how to do this, other than the less clean:
if twData is Array {
} else {
//Code goes here
}
If you know the generic type stored in the array, then you should make it explicit:
if !(twData is Array<Int>) {
// Do something
}
If instead you just want to know if it's an array regardless of the generic type, then you have to use NSArray:
if !(twData is NSArray) {
}
Documentation says:
let isarray = twData is Array
if !isarray {
do something
}

How can I tell that I'm at the last object in a foreach, in Qt

I was wondering if anyone knows of a library method or function within Qt that will tell you when you've hit the last object in a foreach.
Below I'm rolling on a list of strings and I've made up a fictional method below called "isLast()":
foreach( QString a_string, string_list )
{
if ( a_string.isLast() ) // does something like this exist?
{
...
}
}
Does anyone know if anything like "isLast()" exists?
Thanks,
Wes
I've not seen an isLast()-style function around QT. Your best bet is probably to mix in a little old-school counter logic:
int str_count = 0;
int str_list_last_elem = string_list.size()-1;
foreach(QString a_string, string_list) {
str_count++;
if (str_count == str_list_last_elem) {
...
}
}
If the strings in string_list all have unique values you could do:
foreach(QString a_string, string_list) {
if(a_string == string_list.last()){
// it's the last string
}
...
}
Otherwise you would have to use some sort of counter as #ascentury suggested.

Check for structure key in array element using cfscript

I am trying to loop over an array called meta.
I am having issues with checking if an element exists. In this array sometimes the length is present and sometimes it is not. I am trying to get something like this to work:
for (i=1;i LTE ArrayLen(meta);i=i+1) {
if (meta[i].length==undefined) {
maxLen = '1';
}
else
{
maxLen = meta[i].length;
}
}
I cannot seem to get the syntax right.
I think you want a structkeyexists.
if (structkeyexists(meta[i],"length") ....

linux c/c++ - weird if/else issue

I'm querying a mysql table which then loops through the results.
One of the fields has a value of "0" in it, so when I try the following it doesn't work!
while ((row2 = mysql_fetch_row(resultset2)) != NULL) {
if (row2[2] != "0") {
// the field has a value of 0, but it's still executing the code here!
} else {
// should be executing this code
}
}
I know C/C++ is very strict when it comes variables (unlink php), but I can't figure this one out. Anyone have any ideas why?
You're comparing row2[2], a pointer to char, with a pointer to the constant char array "0".
Use strcmp(row2[2], "0") != 0 (C solution), std::string(row2[2]) != "0" (C++ solution), or atoi(row2[2]) != 0 if you know row2[2] is always the string representation of an integer (and cannot be a SQL NULL value).
You cannot compare string literal like this :
if (row2[2] != "0") //wrong
Either you do this :
if (strcmp(row2[2], "0")) //correct
Or this:
if (std::string(row2[2]) != "0") //correct
For this particular case, when there is only one character you can also do this:
if (row2[2][0] != '0') //correct - not the single quote around 0!