Link to home
Start Free TrialLog in
Avatar of jcampanali
jcampanali

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kraiven
kraiven

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?
Avatar of jcampanali

ASKER

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.
using Microsoft.VisualBasic - good point. Sorry I hadn't thought of that.

In that case the issue is probably the single '&' sign.
@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