Solved

Instr() Function in C#

Posted on 2010-11-18
5
1,004 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 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 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

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 …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

707 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