44

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()
4
  • 6
    Those 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 Commented Nov 12, 2009 at 15:20
  • 4
    Just 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... Commented Nov 12, 2009 at 15:22
  • 3
    Just a small nit-pick: these are all functions. None of them is an operator. Commented Nov 12, 2009 at 15:25
  • 1
    @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. Commented Nov 12, 2009 at 16:08

14 Answers 14

92
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.
Sign up to request clarification or add additional context in comments.

10 Comments

Take care though, & double check you haven't stomped on someone else's edits
I suspect you reversed UBound and LBound, but I don't know VB.
It's strange that you don't even get a warning when you stomp on someone else's edit...
@Meta-Knight - it's been reported over on meta but clearly nothing's been done so far
? 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. I don't think there is a direct equivalent of IIf in C# - I think you'd need to write a custom function.
|
5
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;

Comments

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

Comments

2

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)

Comments

2

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

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

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

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

Comments

2

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

2 Comments

@Thomas: it depends on how you set Option Base in old vb.
The lower bound for arrays returned by COM calls might not be 0.
1

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

Comments

1

I believe some of these like Mid() are still available in the .NET Framework in the Microsoft.VisualBasic namespace which you can still reference from C# code.

Comments

1

Another one...

VB - IsDBNull(value)

C# - yourdatarow.IsNull("columnName")

Comments

1

In addition to the answers above. Be carefull with replacing Len() -> x.Length. VB Len() allows you to pass null, but in c# you will get an exception. Sometimes it would be better to use String.IsNullrEmpty() (If the situation allows)

Comments

0

If you look on MSDN you see that most of the time there are sample code for both languages.

Comments

0
  • UBound() -> if x is an array of string[] for example: x.GetUpperBound();
  • LBound() -> if x is an array of string[] for example: x.GetLowerBound();
  • IsNothing() -> if (x == null)
  • Chr() -> char x = (char)65;
  • Len() -> x.Length();
  • UCase() -> assume x is a string: x.ToUpper();
  • LCase() -> assume x is a string: x.ToLower();
  • Left() -> assume x is a string: x.Substring(0, 10); // first 10 characters
  • Right() -> assume x is a string: x.Substring(x.Length - 10); // last 10 characters
  • RTrim() -> x.TrimEnd();
  • LTrim() -> x.TrimStart();
  • Trim() -> x.Trim();
  • Mid() -> assume x is a string: x.Substring()
  • Replace() -> assume x is a string: x.Replace();
  • Split() -> assume x is a string: x.Split();
  • Join() -> String.Join();
  • MsgBox() -> MessageBox.Show();
  • IIF() -> ternary operator (x == true ? true-value : false-value);

Comments

0

One more addition to this could be IndexOf() function to Find String within string

An example below...

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

Comments

0

The space function is missing from everyone else's list:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.