0

Im struggling trying to convert this code. As well as googling for an answer and reading MSDN documentation i still cant figure this out. Ive also looked at the examples

101 for Visual Basic .Net 101 for C# .Net

Hers some C# code im trying to convert:

var asciiCredentials = (from c in credentials
                select c <= 0x7f ? (byte)c : (byte)'?').ToArray();

My attempt so far:

Dim ascii = (From c In Credentials(Function(x) x= 0x7f .....)

But cant figure it out!! I think the Byte conversion is putting me off track.

Can anyone advise

3
  • I've been using C# recently but need to make that transition soon hopefully. It depends on the other guys too as i cant have my projects with C# and rest with VB .Net ;-) Commented Dec 18, 2013 at 20:54
  • @Computer Well, you can ;) Commented Dec 18, 2013 at 20:54
  • Hmm wonder why someone decided to remove the 1 up vote i had for putting in research etc Commented Dec 18, 2013 at 21:11

4 Answers 4

3

Remember that Visual Basic has the IIf command that, in some respects, acts like the ternary operator.

    Dim ascii = (From ch In s
                Select IIf(Char.GetNumericValue(ch) < 127, Convert.ToByte(ch), Convert.ToByte("?"c))).ToArray()

There was a comment saying this didn't compile

Sign up to request clarification or add additional context in comments.

5 Comments

To create this LINQ query, I loaded VS2013, assigned a value to the string "s", compiled it, and debugged it to validate the results. Which version of Visual Studio/Visual Basic are you targeting? (Image attached to show it in the debugger.)
Sorry - I was testing with an 's' similar to what the op used (List<int> probably).
Not a problem. I should have posted my entire code block, not just the snippet.
IIf is not a best solution, since it evaluates both arguments, better use If() instead
Rustram, very true. I haven't used VB.NET since 2006, so until this thread I wasn't aware that the short-circuit ternary operator was added in .NET 3.0 to VB.NET.
1

You can use If in place of the conditional operator, making the code:

Dim asciiCredentials = credentials.Select(Function(x) _
        If(x <= 127, Convert.ToByte(c), Convert.ToByte("?"C)))_
    .ToArray();

3 Comments

Boy, it was funny that as soon as you post, the other answers changed to use your IF.
mine had it from start, just missing the space when I copied for some reason
This doesn't compile.
0
Dim asciiCredentials = (
    From c In credentials
    Select If(c <= &H7f, CByte(c), AscW("?"c))).ToArray()

Comments

0
Dim asciiCredentials = (From c In credentials Select If(c <= &H7f, CByte(c), CByte(AscW("?"C)))).ToArray()

Taken from here:http://www.developerfusion.com/tools/convert/csharp-to-vb

They usually do pretty well converting.

2 Comments

This doesn't compile.
You're correct, it was missing the select. I've put that in to make the answer correct, and I'm contacting the conversion site. Thanks for the info.

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.