Can you translate this code for me in VB please?

Joined
Oct 19, 2010
Messages
5
Programming Experience
Beginner
dim content as string
content = e.row.cells(3).text

content = content.Substring(0, (content.IndexOf(" ", 150) < 0 ? content.Length : content.IndexOf(" ", 150))) & "..."


it doesn't allow the ? and : in vb. Can you please write a similar code for this but in VB please?

I'm want the string to show until a space on the 150th character but if there is no space on the 150th character, the string continues until next space after the 150th character.
 
Previously, VB used the IIf function to approximate the behaviour of the C# ternary '? :' operator. From VB 2008, the new If operator provides the exact same functionality as the C# ternary operator. As such, this C# code:
VB.NET:
Expand Collapse Copy
a = x ? y : z;
is equivalent to this VB code:
VB.NET:
Expand Collapse Copy
a = If(x, y, z)
That VB code is equivalent to the long-hand:
VB.NET:
Expand Collapse Copy
If x Then
    a = y
Else
    a = z
End If
In related news, both languages also added a similar operator specifically for replacing null values. In C#:
VB.NET:
Expand Collapse Copy
a = x ?? y;
and in VB:
VB.NET:
Expand Collapse Copy
a = If(x, y)
Using the previous If operator, that VB code is equivalent to this:
VB.NET:
Expand Collapse Copy
a = If(x Is Nothing, y, x)
 
Also place the result of content.IndexOf(" ", 150) in a variable and use that in the expression, there's no need to search for same result twice.
 
Back
Top