6 Replies - 595 Views - Last Post: 07 July 2011 - 01:46 PM Rate Topic: -----

#1 IYIaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-July 11

Need help with simple if statement.

Posted 07 July 2011 - 01:20 PM

Ok, I have a string now lets say that string can be 01234 or 12345. I'm trying to figure out a way so that I can test if the string doesn't equal one of those.

if (tsbmf != "01234" || tsbmf != "12345")
                {
do something...

                }
                else if (!Directory.Exists(getdir))
                {
                    errorch = 1;
                    MessageBox.Show("Directory: " + getdir + " doesn't exist!");
                }else if (search.Text == ""){
                    errorch = 1;
                    MessageBox.Show("Please set the search frequency!");
                }



Of course this didn't work because setting it up that way it always doesn't equal one of them. If it does equal one of them I just want it to move on.

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with simple if statement.

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15363
  • View blog
  • Posts: 61,613
  • Joined: 12-June 08

Re: Need help with simple if statement.

Posted 07 July 2011 - 01:22 PM

Convert the string (tsbmf ?) to an integer and don't worry about the leading zero seems to be pretty straight attack.
Was This Post Helpful? 0
  • +
  • -

#3 IYIaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-July 11

Re: Need help with simple if statement.

Posted 07 July 2011 - 01:24 PM

View Postmodi123_1, on 07 July 2011 - 01:22 PM, said:

Convert the string (tsbmf ?) to an integer and don't worry about the leading zero seems to be pretty straight attack.

The string isn't really numbers. It's words I just used that for an example.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Need help with simple if statement.

Posted 07 July 2011 - 01:27 PM

Use && instead of ||?
Was This Post Helpful? 0
  • +
  • -

#5 IYIaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-July 11

Re: Need help with simple if statement.

Posted 07 July 2011 - 01:29 PM

View PostJackOfAllTrades, on 07 July 2011 - 01:27 PM, said:

Use && instead of ||?

I tried that It still gives me the error even if the word equals one of them.

This post has been edited by IYIaster: 07 July 2011 - 01:29 PM

Was This Post Helpful? 0
  • +
  • -

#6 IYIaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-July 11

Re: Need help with simple if statement.

Posted 07 July 2011 - 01:35 PM

OK, I guess I'll give more detail. The string is a really long url. I'm stripping the url to show the first 21 chars. Doing this the url can be http://api.domain.com or https://api.domain.co If the url isn't one of those I want the error to pop up. Some reason even when the url is one of those it still pops up with the error. I'm printing the URL to the pop up box and it does equal one of them. If I use the || or the && it still gives me the error every time.


EDIT

Never mind figured out the problem.

This post has been edited by IYIaster: 07 July 2011 - 01:42 PM

Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Need help with simple if statement.

Posted 07 July 2011 - 01:46 PM

Ok, well, first of all, you have to understand boolean logic. This:

string test = "https://api.domain.co";
if (test != "http://api.domain.com" && test != "https://api.domain.co") {
    Console.WriteLine("not valid");
}
else {
    Console.WriteLine("Valid");
}



Will only output "not valid" if test is not one of the two values. Otherwise, it'll output "valid." So switching to && is a valid suggestion.

Second, though, you shouldn't bother doing this with a substring anyway. It's easy enough to do a .Contains, or at worst, a regex match.

This is a simple match:

string test = "http://api.domain.com";
if (!test.Contains("api.domain.com")) {
    Console.WriteLine("not valid");
}
else {
    Console.WriteLine("valid");
}
Console.ReadKey();



If you absolutely had to include the http(s), you could set up a simple regex match.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1