Instr() Function in C#

Hello,

Converting an app from VB.NET¿C# has caused a great many errors(mostly conversion). Some however, are stumpers: for instance why can't I use similar syntactical convention when using instr()

This is VB code...
If Microsoft.VisualBasic.InStr(txtStrike.Text, Chr(46)) > 0 And Len(txtStrike.Text) = 5 Then
This is C# converted...
if (Strings.InStr(txtStrike.Text, Strings.Chr(46), 1)) > 0 & Strings.Len(txtStrike.Text) == 6) {

to which I receive 'Invalid Expression Term' error under '>' operator when building

Many thanks
LVL 4
jcampanaliAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
C# equivalent would be:
if (txtStrike.Text.IndexOf('.') > 0 && txtStrike.Text.Length == 6)

Open in new window

0
 
kraivenCommented:
Surprised that is the only error as neither Strings nor InStr exists in C#; nor do Chr() and a single '&' is a bitwise AND where as you want a logical AND which is '&&'.

I'm sure the likes of Redgates Reflector would make a far better conversion job than this.

You need to use - from your example:
if (txtStrike.Text.Contains((char)46) && txtStrike.Text.Length == 6) ...

Open in new window


by the way, why is your VB code looking for length 5 and you other version looking for length 6?
0
 
jcampanaliAuthor Commented:
Well I did place using Microsoft.VisualBasic; so it would at least recognize the Instr() and related VB functions. I'll give a try to the above referenced.
0
 
kraivenCommented:
using Microsoft.VisualBasic - good point. Sorry I hadn't thought of that.

In that case the issue is probably the single '&' sign.
0
 
käµfm³d 👽Commented:
@carl_tawn

You would probably want to include 0 in the comparison since IndexOf is index-oriented whereas InStr is position-oriented.
txtStrike.Text.IndexOf('.') >= 0

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.