3

Problem:

I currently have the following line of code in C#:

if ((oAEAuthInfo.GetInfo("LetterTo_name") == (oAEAuthInfo.GetInfo("firstname") + " " + oAEAuthInfo.GetInfo("lastname"))) && (oAEAuthInfo.GetInfo("Name")).Contains("OMEGA") || (oAEAuthInfo.GetInfo("Name")).Contains("Alpha"))

and it displays what I want correctly.

However, when I try to do the same thing but in VB:

IF ((oAEAuthInfo.GetInfo("LetterTo_name") = (oAEAuthInfo.GetInfo("firstname") + " " + oAEAuthInfo.GetInfo("lastname"))) And ((oAEAuthInfo.GetInfo("Name")).Contains("Omega") Or (oAEAuthInfo.GetInfo("Name")).Contains("Alpha"))) THEN

It displays no results. Meaning when I run my C# version of the code, it displays an image but for the VB version, nothing occurs.

What am I doing wrong?

UPDATE

Hello Everyone. I would like to thank you all for your help and helping me get a better understanding with VB. However, it is still not displaying any images and I am not sure why.

The following is what I am doing. For some reason, when I try to assigned variables, it did not display any results as well. Any help would be appreciated.

<table width="100%" CELLSPACING="0" CELLPADDING="0">
<tr>
 <td align="center">
 <%
 IF ((oAEAuthInfo.GetInfo("LetterTo_name") = (oAEAuthInfo.GetInfo("firstname") & " " & oAEAuthInfo.GetInfo("lastname"))) AndAlso ((oAEAuthInfo.GetInfo("Name")).Contains("OMEGA") OrElse (oAEAuthInfo.GetInfo("Name")).Contains("ALPHA"))) THEN
  select (oAEAuthInfo.GetInfo("Site")) 
   case "R107":
   case "R0712":
   case "R108":
   case "R10812":
   case "R113":
   case "R11312":
   case "R115":
   case "R11512":
   case "R10702":
   case "R10802":
   case "R11302":
   case "R11502":
   case "RG112":
    Response.Write("<img src='/images/logos/COLA-m.jpg'>")
   case "RG109":
   case "RG10912":
   case "RG110":
   case "RG11012":
   case "RG10902":
   case "RG11002":
    Response.Write("<img src='/images/logos/regalcodow-m.jpg'>")
      End select
ELSEIF (oAEAuthInfo.GetInfo("Name") = (oAEAuthInfo.GetInfo("firstname") & " " & oAEAuthInfo.GetInfo("lastname"))) THEN
   case "RG112":
   case "RG11212":
   case "RG11202":
    Response.Write("<img src='/images/logos/RegalBG-m.jpg'>")    
   case "RMG117":
   case "RMG11712":
   case "RMG11702":
    Response.Write("<img src='/images/logos/RegalGle-m.jpg'>")    
   case "MG101":
   case "MG10112":
   case "MG102":
   case "G10212":
   case "G116":
   case "G11612":
   case "118":
   case "11812":
   case "10102":
   case "G10202":
   case "G11602":
   case "G11802":
    Response.Write("<img src='/images/logos/RegalSFV-m.jpg'>")
     End select
Else
  IF((oAEAuthInfo.GetInfo("Facility") <> (oAEAuthInfo.GetInfo("firstname") & " " & oAEAuthInfo.GetInfo("lastname"))) OrElse (oAEAuthInfo.GetInfo("_ReferredTo") <> (oAEAuthInfo.GetInfo("firstname") & " " & oAEAuthInfo.GetInfo("lastname"))) OrElse (oAEAuthInfo.GetInfo("ReferredFrom") <> (oAEAuthInfo.GetInfo("firstname") & " " & oAEAuthInfo.GetInfo("lastname"))) OrElse (oAEAuthInfo.GetInfo("pcpname") <> (oAEAuthInfo.GetInfo("firstname") & " " & oAEAuthInfo.GetInfo("lastname")))) THEN
select (oAEAuthInfo.GetInfo("PatientSite")) 
    case "MG112":
    case "MG11212":
    case "MG11202":
     Response.Write("<img src='/images/logos/RegalBG.bmp'>")    
    case "MG117":
    case "MG11712":
    case "MG11702":
     Response.Write("<img src='/images/logos/RegalGle.bmp'>")     
    case "MG101":
    case "MG10112":
    case "MG102":
    case "MG10212":
    case "RG116":
    case "MG11612":
    case "MG118":
    case "MG11812":
    case "MG10102":
    case "MG10202":
    case "MG11602":
    case "MG11802":
     Response.Write("<img src='/images/logos/RegalSFV.bmp'>")
     End select
  End If
End If
 %>
 </td>
</tr>
</table>
Roberto Flores
  • 775
  • 2
  • 12
  • 46
  • 4
    Just for your sanity, consider breaking that into multiple statements (that prepare the strings, call some of the methods, etc) – BradleyDotNET Mar 16 '17 at 23:01
  • @BradleyDotNET: K Thanks – Roberto Flores Mar 16 '17 at 23:03
  • 2
    Erik has isolated the main problem, but you should be aware that "And" is not the equivalent of C#'s "&&" operator and "Or" is not the equivalent of C#'s "||" operator - you want the VB logical operators "AndAlso" and "OrElse". – Dave Doknjas Mar 16 '17 at 23:12
  • @DaveDoknjas: I have tried everyones recommendation and yet I still displaying a blank page. I will shortly post what I did – Roberto Flores Mar 17 '17 at 15:28
  • If you expect a match in Case "R107" through Case "RG112" to execute the code for "RG112", then write it as `Case "R107", "R0712", ... all other matches..., "RG112"` on a single line and not as separate cases. – TnTinMn Mar 17 '17 at 17:18
  • @TnTinMn: I have tried that and it still does not render any results – Roberto Flores Mar 17 '17 at 17:43

1 Answers1

7

A good debugging strategy in a situation like this is to take your evaluations outside your if statement and use the debugger to step through and make sure the values are getting calculated correctly.

C#

var letterToName = oAEAuthInfo.GetInfo("LetterTo_name");
var fullName = oAEAuthInfo.GetInfo("firstname") + " " + oAEAuthInfo.GetInfo("lastname");
var containsOmega = oAEAuthInfo.GetInfo("Name").Contains("OMEGA");
var containsAlpha = oAEAuthInfo.GetInfo("Name").Contains("Alpha");

if ((letterToName == fullName) && containsOmega || containsAlpha)

VB

dim letterToName = oAEAuthInfo.GetInfo("LetterTo_name")
dim fullName = oAEAuthInfo.GetInfo("firstname") + " " + oAEAuthInfo.GetInfo("lastname")
dim containsOmega = oAEAuthInfo.GetInfo("Name").Contains("Omega")
dim containsAlpha = oAEAuthInfo.GetInfo("Name").Contains("Alpha")

IF ((letterToName = fullName) And (containsOmega Or containsAlpha)) THEN

And it becomes clear what your problem is: you have an extra set of parenthesis in the VB code that are missing from the C# code. Order of operations dictate that and operators take precedence over or operators, so in the C# code if containsAlpha is true the condition always passes, whereas in the VB code that will only happen if letterToName = fullName is also true.

Also, case sensitivity of "Omega" may be the problem.

As Visual Vincent pointed out, the And and Or operators do not actually correspond to the behavior of C#'s && and || operators - instead VB's operators that are equivalent would be AndAlso and OrElse. That is unlikely to be the source of the problem in this situation unless GetInfo or Contains have some side-effects.

Erik
  • 5,355
  • 25
  • 39
  • beat me only by seconds :) – caesay Mar 16 '17 at 23:06
  • 2
    Also why its a good idea to have multiple statements instead of one giant `if` :) – BradleyDotNET Mar 16 '17 at 23:06
  • 2
    Instead of `Or` and `And` you should use [**`OrElse` and `AndAlso`**](http://stackoverflow.com/a/8409488/3740093), and [**`&` is preferred over `+` for concatenations**](http://stackoverflow.com/a/734631/3740093). – Visual Vincent Mar 16 '17 at 23:14
  • @VisualVincent Good call out - OrElse and AndAlso are a better match for the behavior of C#'s operators. – Erik Mar 16 '17 at 23:18
  • I do not know if it is applicable to this instance, but in general one should be careful using VB's equality operator for strings to yield a boolean value. The backward compatibility requirement that VB.Net adheres to can lead to some confusing issues. First you have `Option Compare = Text/Binary`. Secondly, the equality operator for strings treats Nothing (null) as String.Empty; so the result of: `Nothing=String.Empty` will be True. It is safer to use one of the various equality functions available on the String type. i.e: `string1.Equals(string2)` or `String.Equals(string1, string2)` – TnTinMn Mar 17 '17 at 00:15