2009-11-12 98 views
35

下面哪些運算符等同於VB.Net到C#?VB到C#的功能

  • UBound函數()
  • LBOUND()
  • IsNothing()
  • CHR()
  • LEN()
  • 用Ucase()
  • LCASE()
  • 左()
  • 右()
  • RTRIM()
  • LTRIM()
  • TRIM()
  • MID()
  • 更換()
  • 斯普利特()
  • 加入()
  • MsgBox()
  • IIF()
+6

這些都是真正從VB6遺留下來的呢 - 在VB.NET你應該使用String或Array對象的方法,例如String.TrimLeft,String.ToUpper和Array.GetUpperBound。當然MessageBox.Show – MartW 2009-11-12 15:20:29

+4

僅供參考,它們不是真正的「VB.NET」運算符,VB.NET中包含兼容性庫(如果需要,可以選擇在C#中使用,只需添加引用和使用)從VB.OLD進行這些功能 - 所以如果一個人想要真正迂腐... – Murph 2009-11-12 15:22:28

+3

只是一個小挑剔:這些都是功能。 **沒有**他們是運營商。 – 2009-11-12 15:25:00

回答

66
VB    C# 

UBound()  = yourArray.GetUpperBound(0) or yourArray.Length for one-dimesional arrays 
LBound()  = yourArray.GetLowerBound(0) 
IsNothing() = Object.ReferenceEquals(obj,null) 
Chr()  = Convert.ToChar() 
Len()  = "string".Length 
UCase()  = "string".ToUpper() 
LCase()  = "string".ToLower() 
Left()  = "string".Substring(0, length) 
Right()  = "string".Substring("string".Length - desiredLength) 
RTrim()  = "string".TrimEnd() 
LTrim()  = "string".TrimStart() 
Trim()  = "string".Trim() 
Mid()  = "string".Substring(start, length) 
Replace() = "string".Replace() 
Split()  = "string".Split() 
Join()  = String.Join() 
MsgBox()  = MessageBox.Show() 
IIF()  = (boolean_condition ? "true" : "false") 

  • yourArray.GetUpperBound(0) VS yourArray.Length:如果數組是零長度的,GetUpperBound將返回-1,而長度將在VB.NET返回0 UBound()將返回-1爲零長度數組。
  • VB字符串函數使用基於一個索引,而.NET方法使用基於零的索引。即Mid("asdf",2,2)對應於"asdf".SubString(1,2)
  • ?不是IIf完全等效,因爲IIf總是計算參數,?只評估它需要的一個。如果有評估〜不寒而慄的副作用,這可能很重要!
  • 許多經典VB字符串的功能,包括Len()UCase()LCase()Right()RTrim(),和Trim(),將治療的NothingNull在c#)的自變量爲等同於一個零長度的字符串。當然,在Nothing上運行字符串方法會引發異常。
  • 您還可以將Nothing傳遞給經典的VB Mid()Replace()函數。這些將不會拋出異常,而會返回Nothing
+0

它是CW所以隨時糾正/修改/添加 – 2009-11-12 15:16:38

+1

保重,雖然,並仔細檢查你沒有踩別人的編輯 – ChrisF 2009-11-12 15:21:55

+0

我懷疑你扭轉UBound和LBound,但我不知道VB。 – 2009-11-12 15:23:15

2

其中大部分將是返回修改字符串的字符串對象上的實例方法。

MsgBox vs. MessageBox.Show(..)
IIF vs. (expression?returnValueIfTrue:returnValueElse)
2

IIf(test, trueval, falseval) >>(test ? trueval : falseval);

IsNothing(obj) >>(obj == null);

UCase(str) >>str.ToUpper();

LCase(str) >>str.ToLower();

0

如果你看看你的時間也有兩種語言的示例代碼的MSDN最見。

1

相信喜歡Mid()其中的一些仍然在.NET Framework提供的Microsoft.VisualBasic命名空間,你仍然可以從C#代碼的參考。

所有的
3
UBound() "array".Length 
LBound() 
IsNothing(): "object" == null 
Chr()  (char)"N" 
Len()  "string".Length 
UCase() "string".ToUpper() 
LCase() "string".ToLower() 
Left() "string".Substring(from, to) 
Right() "string".Substring(from, to) 
RTrim() "string".TrimEnd() 
LTrim() "string".TrimStart() 
Trim() "string".Trim() 
Mid()  "string".Substring(from, to) 
Replace() "string".Replace() 
Split() "string".Split() 
Join() String.Join() 
MsgBox() MessageBox.Show() 
IIF()  validate ? iftrue : iffalse; 
+0

「array」.Length實際上是UBound + 1 ... – 2009-11-12 15:32:12

2

首先,那些最沒有運營商。他們是功能,和功能只包括在VB.Net出於兼容性原因。這意味着你不應該在VB.net或者使用它們,等同而是使用所提供的新的API。

  • UBound函數()arrayVar.Length
  • LBOUND() —過時,下界總是 0在正常的.Net陣列
  • IsNothing() —過時。在VB中使用Is Nothing。在C#
  • CHR()Convert.ToChar()(char)someVar
  • 萊恩()在VBstringVar.Length使用這種在VB太
  • 用Ucase()stringVar.ToUpper()使用本太
  • 網和== nullLCase() —​​在VB中也使用這個
  • 左()stringVar.Substring(0, n)在VB在VB中使用這種在VB太
  • 右()stringVar.Substring(stringVar.Length - n)使用本太
  • RTRIM()stringVar.TrimEnd()使用本太
  • LTRIM( )stringVar.TrimStart()在VB中也使用這個
  • TRIM()stringVar.Trim()使用這種在VB太
  • MID()stringVar.Substring(n, m)使用這種在VB太
  • 更換()stringVar.Replace()使用這種在VB太
  • 斯普利特()stringVar.Split()在VB中也使用它
  • 加入()String.Join()在VB中使用過
  • MSGBOX()MessageBox.Show()
  • IIF()(condition) ? truepart : falsepart - 「?」 注意,有一些差異,因爲是一個操作符,而不是一個功能
+1

arrayVar.Length實際上是UBound + 1 ... – 2009-11-12 15:31:24

+0

@Thomas:它取決於如何在舊的vb中設置Option Base。 – 2009-11-12 15:37:52

+0

COM調用返回的數組下限可能不是0。 – Govert 2015-03-23 22:58:32

0
  • UBound函數() - >如果x是一個字符串數組[]例如:x.GetUpperBound(); LBound() - >如果x是一個字符串數組[],例如:x.GetLowerBound(); () - >如果(x == null)
  • Chr() - > char x =(char)65;
  • Len() - > x.Length(); ();}假設x是一個字符串:x.ToUpper();
  • LCase() - >假定x是一個字符串:x.ToLower();
  • Left() - >假定x是一個字符串:x.Substring(0,10); //前10個字符
  • Right() - >假定x是一個字符串:x.Substring(x。長度 - 10); //最後10個字符
  • RTrim() - > x.TrimEnd();
  • LTrim() - > x.TrimStart();
  • Trim() - > x.Trim();
  • Mid() - >假設x是一個字符串:x.Substring()
  • Replace() - >假定x是一個字符串:x.Replace();
  • Split() - >假定x是一個字符串:x.Split();
  • Join() - > String.Join();
  • MsgBox() - > MessageBox.Show();
  • IIF() - >三元運算符(x == true?true-value:false-value);
2

所有這些功能都是Microsoft.VisualBasic.Information類的成員方法,在Microsoft.VisualBasic程序集中,因此您可以直接使用它們。然而,大多有C#當量,或非語言特定當量在覈心.NET框架類:

  • UBound函數():Array.GetUpperBound
  • LBOUND():Array.GetLowerBound
  • IsNothing():== null
  • CHR():(char)intValue(澆鑄)
  • 萊恩():String.Length
  • 用Ucase():String.ToUpper
  • LCASE():String.ToLower
  • 左(),右()和MID():String.Substring(具有不同參數)
  • RTRIM():String.TrimEnd
  • LTRIM():String.TrimStart
  • TRIM() :String.Trim
  • 替換():String.Replace
  • 斯普利特():String.Split
  • 加入():String.Join
  • MSGBOX():MessageBox.Show
  • IIF():condition ? valueIfTrue : valueIfFalse(條件運算符)

鏈接

1

另外一...

VB - IsDBNull以便(值)

C# - yourdatarow。ISNULL( 「列名」)

0

還有一個除了這可能是的IndexOf()函數的字符串中查找字符串

下面的例子...

string MainString = "String Manipulation"; 
string SearchString = "pul"; 
int FirstChr = MainString.IndexOf(SearchString); 
//SHOWS START POSITION OF STRING 
MessageBox.Show("Found at : " + FirstChr); 
0

除了答案以上。 請注意替換Len() - > x.Length。 VB Len()允許你傳遞null,但在C#中你會得到一個異常。 有時它會更好地使用String.IsNullrEmpty()(如果情況允許的話)

0

空間功能是從其他人的名單丟失:

Space(16) -> new String(" ", 16)