Computer
Computer

Reputation: 2227

Linq C# to Vb .Net

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

Upvotes: 0

Views: 171

Answers (4)

BlackICE
BlackICE

Reputation: 8926

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.

Upvotes: 0

RomSteady
RomSteady

Reputation: 398

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

Upvotes: 3

Servy
Servy

Reputation: 203821

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();

Upvotes: 1

Dave Doknjas
Dave Doknjas

Reputation: 6542

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

Upvotes: 0

Related Questions