Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Instr() Function in C#

Posted on 2010-11-18
5
Medium Priority
?
1,068 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1000 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 75

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

597 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