VB to C# Functions
Solution 1
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)
vsyourArray.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 ofIIf
becauseIIf
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()
, andTrim()
, will treat an argument ofNothing
(Null
in c#) as being equivalent to a zero-length string. Running string methods onNothing
will, of course, throw an exception. - You can also pass
Nothing
to the classic VBMid()
andReplace()
functions. Instead of throwing an exception, these will returnNothing
.
Solution 2
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;
Solution 3
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
Solution 4
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)
Solution 5
IIf(test, trueval, falseval)
>> (test ? trueval : falseval);
IsNothing(obj)
>> (obj == null);
UCase(str)
>> str.ToUpper();
LCase(str)
>> str.ToLower();
Jonathan Escobedo
I started programming with Visual Basic 6.0 when I was 14 years old in high school. I am a full stack web developer living in Salt Lake City, Utah and currently working as a software engineer for Sorenson Communications.
Updated on July 09, 2022Comments
-
Jonathan Escobedo over 1 year
Which are the equivalent of the following operators from VB.Net to C#?
- UBound()
- LBound()
- IsNothing()
- Chr()
- Len()
- UCase()
- LCase()
- Left()
- Right()
- RTrim()
- LTrim()
- Trim()
- Mid()
- Replace()
- Split()
- Join()
- MsgBox()
- IIF()
-
MartW over 14 yearsThose are really holdovers from VB6 anyway - in VB.NET you should be using methods of the String or Array objects, eg String.TrimLeft, String.ToUpper and Array.GetUpperBound. And of course MessageBox.Show
-
Bijendra Singh over 14 yearsJust for information, they aren't really "VB.NET" operators, there's a compatibility library included in VB.NET (that you could choose to use in C# if you wanted, just add the reference and the using) that carry those functions over from VB.OLD - so if one wanted to be really pedantic...
-
Dan Tao over 14 yearsJust a small nit-pick: these are all functions. None of them is an operator.
-
Jonathan Escobedo over 14 years@erikkallen not dude, I was converting code from VB to C#. that for the first functions are in bold, also i found this vbconversions.net/vbtocsdetail.htm and I want to make it a CW for everyone that will need.
-
ChrisF over 14 yearsTake care though, & double check you haven't stomped on someone else's edits
-
MaddTheSane over 14 yearsI suspect you reversed UBound and LBound, but I don't know VB.
-
tsuo euoy over 14 yearsIt's strange that you don't even get a warning when you stomp on someone else's edit...
-
ChrisF over 14 years@Meta-Knight - it's been reported over on meta but clearly nothing's been done so far
-
Bijendra Singh over 14 yearsDid get a warning that other edits were in progress (3 of them)
-
Guffa over 14 years@Meta-Knight: It shows in the orange bar at the top of the screen, if the page has time to notice it before you save.
-
Joel Coehoorn over 14 years@Thomas: it depends on how you set Option Base in old vb.
-
Chris Marisic over 14 yearsChanged text to "string".Length, as it's a property. For a method you could need to use "string".Count()
-
MarkJ over 14 years
?
is not the exact equivalent ofIIf
becauseIIf
always evaluates both arguments, and?
only evaluates the one it needs. This could matter if there are side effects of the evaluation. I don't think there is a direct equivalent ofIIf
in C# - I think you'd need to write a custom function. -
statenjason over 14 years
IsNothing() = Object.ReferenceEquals(obj,null)
because==
can be overloaded to behave incorrectly. -
Maxim Gershkovich about 13 yearsMight be worth updating your answer to reflect the fact that VB.NET now supports a short circuited version of the ternary operator IF(bool, true, false) .
-
Govert about 9 yearsThe lower bound for arrays returned by COM calls might not be 0.