将C#转换为VB

时间:2017-03-16 22:56:29

标签: c# vb.net

问题:

我目前在C#中有以下代码行:

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

它会正确显示我想要的内容。

然而,当我尝试在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

没有显示任何结果。这意味着当我运行我的C#版本的代码时,它会显示一个图像但是对于VB版本,没有任何反应。

我做错了什么?

更新

大家好。我想感谢大家的帮助,并帮助我更好地理解VB。但是,它仍然没有显示任何图像,我不知道为什么。

以下是我正在做的事情。出于某种原因,当我尝试分配变量时,它也没有显示任何结果。任何帮助将不胜感激。

<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>

1 个答案:

答案 0 :(得分:7)

在这种情况下,一个好的调试策略是在if语句之外进行评估,并使用调试器逐步完成并确保正确计算值。

<强> 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

很明显你的问题是什么:你在VB代码中有一组额外的括号,它们是C#代码中缺少的。操作顺序规定and运算符优先于or运算符,因此在C#代码中,如果containsAlpha为真,则条件总是通过,而在VB代码中,只有{{}} {1}}也是如此。

此外,letterToName = fullName的区分大小写可能是问题所在。

正如Visual Vincent指出的那样,"Omega"And运算符实际上并不对应于C#的Or&&运算符的行为 - 而是等同于VB的运算符将是||AndAlso。除非OrElseGetInfo有一些副作用,否则在这种情况下,这不太可能成为问题的根源。