3

I need to convert the VB code to c#

Do Until MarkerPos = 0 Or i > UBound(Values)
    s = Replace(s, Token, Values(i),  , 1)
    i = i + 1;
    MarkerPos = Strings.InStr(s, Token);
Loop 

and I have converted it to do while

do
{
    s = Replace(s, Token, Values(i),  , 1)
    i = i + 1;
    MarkerPos = Strings.InStr(s, Token);
} while(MarkerPos = 0 || i > UBound(Values));

is it correct and is there any similar related to UBound in c#.???

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dah Sra
  • 4,107
  • 3
  • 30
  • 69

6 Answers6

2

You can simply use values.Length to return the number of items in a C# array:

do
{
    s = Replace(s, Token, Values(i),  , 1)
    i = i + 1;
    MarkerPos = Strings.InStr(s, Token);
} 
while(MarkerPos = 0 || i > Values.Length -1);

(You can also use .Count() for any other enumerable type)

EDIT:

Also - I think your condition may be the wrong way around:

i < Values.Length -1

EDIT2:

and your logic should probably be an AND:

while(MarkerPos = 0 && i < Values.Length-1);
Dave Bish
  • 19,263
  • 7
  • 46
  • 63
  • UBound = "upper bound", so yes – Dave Bish Aug 05 '14 at 10:53
  • @Arshad: UBound is equal to Length or Count *less one*. UBound is the highest index of the array, which is *not* the length. – Dave Doknjas Aug 05 '14 at 14:42
  • I don't think you can use `do ... while`. In that case, the code inside the loop will execute at least one time whereas in the original VB code, there is the possibility that the code won't execute at all. – Chris Dunaway Aug 06 '14 at 14:08
1

if you have an array of 10 items, Ubound will return 10 and Length will return 11.

you can use .GetUpperBound(0) or .Lenght-1

proof

 using System;
using Microsoft.VisualBasic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var ar = new string[11];

            Console.WriteLine(ar.GetUpperBound(0));
            Console.WriteLine(ar.Length);
            Console.WriteLine(Microsoft.VisualBasic.Information.UBound(ar));

            Console.ReadKey();
        }
    }
}

so with the help of this answer

this is what you would need, I think

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "123412341234";
            string Token = "2";
            var Values = new string[] {"a","b", "c" };
            int i = 0;
            int MarkerPos;

            do
            {
                s = ReplaceFirst(s, Token, Values[i]);
                MarkerPos = s.IndexOf(Token);
                i++;
            } while(MarkerPos != -1 && i <= Values.GetUpperBound(0));

            Console.WriteLine(s);

            Console.ReadKey();
        }

        static string ReplaceFirst(string text, string search, string replace)
        {
            int pos = text.IndexOf(search);
            if (pos < 0)
            {
                return text;
            }
            return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
        }
    }
}
Community
  • 1
  • 1
Fredou
  • 19,848
  • 10
  • 58
  • 113
0

As mentioned in a comment by 'the_lotus', the loop that is the most natural equivalent is a "while ( ! " loop:

while (!(MarkerPos == 0 || i > Values.GetUpperBound(0)))
{
    s = ...
    i = i + 1;
    MarkerPos = (s.IndexOf(Token) + 1);
}

Note that I've left out the 'Replace' equivalent - the version of the VB 'Replace' method that you are using has no direct equivalent, but you can likely verify whether the regular string 'Replace' method will be adequate or not.

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28
-1

No ubound in c#. You can use something like Values.Length where Values is your string array.

Shibasis Sengupta
  • 629
  • 1
  • 6
  • 21
-1

You can write:

do
{
    ...
} while(MarkerPos != 0 && i < Values.Length);
Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
-1

I used this page several time, it can convert vice versa: Convert vb.net to c# or c# to vb.net - developerfusion.com

It also supports python and ruby.

Zulu
  • 8,765
  • 9
  • 49
  • 56
user3567816
  • 106
  • 1
  • 1
  • 8