2

I am new to OOP as a whole let alone VB. I'm trying to turn the following 4 lines into just 2 lines by combining the conditions:

ElseIf GA_URI.StartsWith("/home/index") Then
    gaCategory = cat_splash

ElseIf GA_URI = "/games" Then
    gaCategory = cat_splash

I have tried this:

ElseIf GA_URI.StartsWith("/home/index") And GA_URI = "/games" Then
    gaCategory = cat_splash

But not working. Any help is hugely appreciated

4

6 回答 6

4

I'd suggest OrElse so you can be more efficient with short circuiting (don't have to eval both expresssions):

ElseIf GA_URI.StartsWith("/home/index") OrElse GA_URI = "/games" Then
   gaCategory = cat_splash

Update:

Q: Would this work for more than 2 expressions? as I have cases where I may have 3 or more

Sure:

ElseIf GA_URI.StartsWith("/home/index") OrElse 
    (GA_URI = "/games" OrElse GA_URI = "/foo")
Then 
     gaCategory = cat_splash
...

You can keep going (parenthetical expressions) but at some point Select...Case would be the way to go.

于 2013-05-15T12:21:42.870 回答
3

replace And with Or and can write it as

         ElseIf GA_URI.StartsWith("/home/index") Or GA_URI = "/games" Then
       gaCategory = cat_splash

Because in you want if the GA_URI starts with /home/index or GA_URI is /games then in any of the condition you want the gaCategory = cat_splash so we need to use OR instead of And. to validate any of the condition is true then assign the gaCategory = cat_splash

于 2013-05-15T12:02:07.137 回答
3

You need to use OR, it can't be both locations

ElseIf GA_URI.StartsWith("/home/index") Or GA_URI = "/games" Then
   gaCategory = cat_splash

By using AND, you're saying it must start with "/home/index" AND must be "/games". Well, this will never be true.

By changing to OR, you're saying it can either start with "/home/index" OR be "/games".

于 2013-05-15T12:02:09.227 回答
2

How about this:

ElseIf GA_URI.StartsWith("/home/index") Or GA_URI = "/games" Then
    gaCategory = cat_splash
于 2013-05-15T12:02:16.607 回答
2
ElseIf GA_URI.StartsWith("/home/index") OR GA_URI = "/games" Then
    gaCategory = cat_splash

would surely do the trick, because the path can only be one at a time.

于 2013-05-15T12:02:41.370 回答
2

Try Or instead of And

ElseIf GA_URI.StartsWith("/home/index") Or GA_URI = "/games" Then
    gaCategory = cat_splash
于 2013-05-15T12:03:22.560 回答