跳轉到內容

Visual Basic .NET/Visual Basic 6 到 .NET 函式等效

來自華夏公益教科書,開放的書籍,開放的世界

這裡提供的資訊旨在幫助正在從 Visual Basic 6 轉向 Visual Basic .NET 的開發者,並希望瞭解如何在不使用 Microsoft.VisualBasic 名稱空間(包含所有遺留 Visual Basic 6 函式)的情況下完成任務。

不要誤解;透過使用 Microsoft.VisualBasic 名稱空間中的函式,你並沒有使用“舊程式碼”,因為所有這些函式都對映到 .NET Framework 中的新的等效函式。避免使用這些函式的主要好處是,你可以熟悉 .NET Framework 中包含的名稱空間。

如果你想刪除 Microsoft.VisualBasic 名稱空間,你可以做以下兩件事之一

  • 在每次開始新專案時手動刪除匯入。
  • 建立一個不包含 Microsoft.VisualBasic 名稱空間的專案模板。

要建立模板,請檢視以下連結

  • 這裡列出的一些函式可能不是完全等效的,但它們是可用的最接近的函式。
  • 鼓勵你使用物件瀏覽器 (F2) 自己探索 .NET 名稱空間。
  • 點選任何 .NET 等效函式以訪問 Microsoft Developer Network,並瞭解更多關於該函式的資訊。
Visual Basic 6 .NET 等效
App.Path Application.StartupPath
CallByName Type Object.InvokeMember
Circle Graphics Object.DrawEllipse
Collection Collections.Hashtable, Collections.Dictionary
Command Environment.GetCommandLineArgs
DoEvents Application.DoEvents
Environ Environment.GetEnvironmentVariable
Err 已棄用 - 使用 Exception 物件和 Try ... Catch ... End Try 語句
Error Exception Object.Message
GetSetting 已棄用 - 檢視 Configuration.ConfigurationSettings
IIf 已棄用
InputBox 已棄用 - 自行製作表單
Len Runtime.InteropServices.Marshal.SizeOf
MsgBox MessageBox.Show
RGB Color Object.FromArgb
SavePicture Image Object.Save
SaveSetting 已棄用 - 檢視 Configuration.ConfigurationSettings
SendKeys SendKeys.Send
Shell Process.Start
Switch 已棄用
TypeName Any Object.GetType.ToString
Unload 已棄用
VarType Any Object.GetType
Visual Basic 6 .NET 等效
Asc Convert.ToInt32(Char)
Chr Convert.ToChar
Format Any Object.ToString(Format String)
FormatCurrency Any Numeric Object.ToString("c")
FormatDateTime Date Object.ToString(Format String), Date Object.ToLongDateString
FormatNumber Any Numeric Object.ToString(Format String)
FormatPercent Any Numeric Object.ToString("p")
Hex Integer Object.ToString("x"), Convert.ToString(Integer, 16)
InStr String Object.IndexOf
InStrRev String Object.LastIndexOf
Join String.Join
LCase String Object.ToLower
Left String Object.Substring
Len String Object.Length
LTrim String Object.TrimStart
Mid String Object.Substring
Oct Convert.ToString(Integer, 8)
Replace String Object.Replace
Right String Object.Substring
RTrim String Object.TrimEnd
Space String Object.PadLeft, String Object.PadRight
Split String Object.Split
String String Object = New String(Char, Count)
StrReverse Dim c() As Char = Array.Reverse(String Object.ToCharArray)
Trim String Object.Trim
UCase String Object.ToUpper

檔案和目錄

[編輯 | 編輯原始碼]
Visual Basic 6 .NET 等效
ChDir IO.Directory.SetCurrentDirectory
CurDir IO.Directory.GetCurrentDirectory
Dir IO.Directory.GetFiles, IO.Directory.GetDirectories, IO.Directory.Exists, IO.File.Exists
EOF IO.StreamReader.Peek = -1
FileAttr 已棄用 - 使用 StreamReaderStreamWriter
FileCopy IO.File.Copy
FileDateTime IO.File.GetLastAccessTime, IO.File.GetCreationTime
FileLen IO.FileStream Object.Length
GetAttr IO.File.GetAttributes
Loc IO.Stream Object.Position
LOF IO.Stream Object.Length
MkDir IO.Directory.CreateDirectory
RmDir IO.Directory.Delete
SetAttr IO.File.SetAttributes
Visual Basic 6 .NET 等效
Filter Array.BinarySearch
UBound Any Type of Array.GetUpperBound(0)

日期和時間

[編輯 | 編輯原始碼]
Visual Basic 6 .NET 等效
DateAdd Date Object.Add
DateDiff Date Object.Subtract
Now DateTime.Now
Visual Basic 6 .NET 等效
Abs Math.Abs
Atn Math.Atan
Cos Math.Cos
Exp Math.Exp
Fix Math.Truncate, Math.Floor (對於負數), Math.Ceiling (對於正數)
Int Math.Floor
Log Math.Log
Randomize 已棄用 - Random 物件在宣告時會自動隨機化
Rnd System.Random Object.Next
Round Math.Round
Sgn Math.Sign
Sin Math.Sin
Sqr Math.Sqrt
Tan Math.Tan
[編輯 | 編輯原始碼]
華夏公益教科書