Solved

Instr() Function in C#

Posted on 2010-11-18
5
944 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:jcampanali
5 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 34164877
C# equivalent would be:
if (txtStrike.Text.IndexOf('.') > 0 && txtStrike.Text.Length == 6)

Open in new window

0
 
LVL 3

Expert Comment

by:kraiven
ID: 34164993
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
 
LVL 4

Author Comment

by:jcampanali
ID: 34165711
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
 
LVL 3

Expert Comment

by:kraiven
ID: 34165783
using Microsoft.VisualBasic - good point. Sorry I hadn't thought of that.

In that case the issue is probably the single '&' sign.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34188530
@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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now