views:

666

answers:

11
+3  Q: 

VB to C# Functions

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()
+9  A: 
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!
Gavin Miller
It's CW so feel free to correct/modify/add
Gavin Miller
ChrisF
I suspect you reversed UBound and LBound, but I don't know VB.
jleedev
It's strange that you don't even get a warning when you stomp on someone else's edit...
Meta-Knight
@Meta-Knight - it's been reported over on meta but clearly nothing's been done so far
ChrisF
Did get a warning that other edits were in progress (3 of them)
Murph
@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.
Guffa
Changed text to "string".Length, as it's a property. For a method you could need to use "string".Count()
Chris Marisic
`?` 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.
MarkJ
@MarkJ - Thanks for pointing that out.
Gavin Miller
`IsNothing() = Object.ReferenceEquals(obj,null)` because `==` can be overloaded to behave incorrectly.
statenjason
+2  A: 

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)
Wim Hollebrandse
+2  A: 

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

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

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

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

Tim S. Van Haren
A: 

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

Meta-Knight
A: 

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

PoweRoy
+1  A: 

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.

Adam Neal
+2  A: 
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;
PMN
"array".Length is actually UBound + 1...
Thomas Levesque
+2  A: 
Joel Coehoorn
arrayVar.Length is actually UBound + 1...
Thomas Levesque
@Thomas: it depends on how you set Option Base in old vb.
Joel Coehoorn
A: 
  • 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);
Scott Anderson
A: 

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

Thomas Levesque
A: 

Another one...

VB - IsDBNull(value)

C# - yourdatarow.IsNull("columnName")

Cylon Cat
fell free to edit is CW
Angel Escobedo