Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB to C# Functions

People also ask

Can you convert VB to C?

The VB to C# code converter from the SharpDevelop team is now a standalone extension to Visual Studio. Once installed, you can convert an entire VB.NET project to C# by opening the solution, right clicking the solution node in the Solution Explorer and selecting Convert to C#.

Should I migrate from VB.NET to C#?

NET Core, you need to convert VB.NET to C#. If desktop apps are enough for you and your users, VB.NET works with NET 5, but only to target Windows Forms--not WPF. But if you want to write web apps running on ASP.NET Core, you'll need C#.

Can you convert C# to C?

If you mean "Is it possible to convert C# to readable and maintainable C-code?", then sorry, the answer is no — C# features don't directly map to C.


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

Notes

  • yourArray.GetUpperBound(0) vs yourArray.Length: if the array is zero-length, GetUpperBound will return -1, while Length will return 0. UBound() in VB.NET will return -1 for zero-length arrays.
  • The VB string functions uses a one based index, while the .NET method uses a zero based index. I.e. Mid("asdf",2,2) corresponds to "asdf".SubString(1,2).
  • ? is not the exact equivalent of IIf because IIf always evaluates both arguments, and ? only evaluates the one it needs. This could matter if there are side effects of the evaluation ~ shudder!
  • The Many classic VB String functions, including Len(), UCase(), LCase(), Right(), RTrim(), and Trim(), will treat an argument of Nothing (Null in c#) as being equivalent to a zero-length string. Running string methods on Nothing will, of course, throw an exception.
  • You can also pass Nothing to the classic VB Mid() and Replace() functions. Instead of throwing an exception, these will return Nothing.

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;

All these functions are member methods of the Microsoft.VisualBasic.Information class, in the Microsoft.VisualBasic assembly, so you can use them directly. However, most of them have C# equivalents, or non language specific equivalents in core .NET framework classes :

  • UBound() : Array.GetUpperBound
  • LBound() : Array.GetLowerBound
  • IsNothing() : == null
  • Chr() : (char)intValue (cast)
  • Len() : String.Length
  • UCase() : String.ToUpper
  • LCase() : String.ToLower
  • Left(), Right() and Mid() : String.Substring (with different arguments)
  • RTrim() : String.TrimEnd
  • LTrim() : String.TrimStart
  • Trim() : String.Trim
  • Replace() : String.Replace
  • Split() : String.Split
  • Join() : String.Join
  • MsgBox() : MessageBox.Show
  • IIF() : condition ? valueIfTrue : valueIfFalse (conditional operator)

Links

  • Array members
  • String members
  • MessageBox.Show
  • conditional operator

Most of these would be instance methods on the string object that return the modified string.

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

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

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

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

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


First of all, most of those are NOT operators. They are functions, and the functions are only included in VB.Net for compatibility reasons. That means you shouldn't use them in VB.net either, and instead use the equivalents provided by the new API.

  • UBound()arrayVar.Length
  • LBound() — obsolete, lower bound is always 0 in a normal .Net array
  • IsNothing() — obsolete. Use Is Nothing in VB.Net and == null in C#
  • Chr()Convert.ToChar() or (char)someVar
  • Len()stringVar.Length use this in VB too
  • UCase()stringVar.ToUpper() use this in VB too
  • LCase()stringVar.ToLower() use this in VB too
  • Left()stringVar.Substring(0, n) use this in VB too
  • Right()stringVar.Substring(stringVar.Length - n) use this in VB too
  • RTrim()stringVar.TrimEnd() use this in VB too
  • LTrim()stringVar.TrimStart() use this in VB too
  • Trim()stringVar.Trim() use this in VB too
  • Mid()stringVar.Substring(n, m) use this in VB too
  • Replace()stringVar.Replace() use this in VB too
  • Split()stringVar.Split() use this in VB too
  • Join()String.Join() use this in VB too
  • MsgBox()MessageBox.Show()
  • IIF()(condition) ? truepart : falsepart - note that there are some differences, because "?" is an operator and not a function

You'll find the conversion for many of these functions on this wikipedia page.