Home All Groups Group Topic Archive Search About

Does anybody know how to translate this C# code to VB6?

Author
8 Mar 2009 4:46 PM
Ulf Christenson
I have this code, but I don't know how to translate these IEnumerable
and other tricky code to VB6.
If anybody could help me it would really make my day. I cannot do it yet.
Ulf

         private class StringReplacementPermuter
         {
             private char[] equiChar;
             private string inputString;
             private int[] indexIntoString;
             private int[] indexIntoChar;
             public StringReplacementPermuter(string inputString, char
e, char a)
             {
                 equiChar = new char[] { e, a };
                 this.inputString = inputString;
             }

             public IEnumerable<string> Permute()
             {
                 Prepare();

                 return GetReplacementStrings(0);
             }

             private void Prepare()
             {
                 List<int> occurenceList = new List<int>();
                 int occurrenceAt = -1;
                 while ((occurrenceAt = inputString.IndexOfAny(equiChar,
occurrenceAt + 1)) != -1)
                 {
                     occurenceList.Add(occurrenceAt);
                 }

                 indexIntoString = occurenceList.ToArray();
                 indexIntoChar = new int[indexIntoString.Length];
             }

             private IEnumerable<string> GetReplacementStrings(int loop)
             {
                 if (loop == indexIntoString.Length)
                 {
                     yield return ReplaceByMap();
                 }
                 else
                 {
                     for (int i = 0; i < equiChar.Length; i++)
                     {
                         indexIntoChar[loop] = i;

                         foreach (string os in
GetReplacementStrings(loop + 1))
                         {
                             yield return os;
                         }
                     }

                     indexIntoChar[loop] = 0;
                 }
             }


             private string ReplaceByMap()
             {
                 StringBuilder s = new StringBuilder(inputString);

                 for (int j = 0; j < indexIntoChar.Length; j++)
                 {
                     s[indexIntoString[j]] = equiChar[indexIntoChar[j]];
                 }

                 return s.ToString();
             }

         }

         static void Main(string[] args)
         {
             foreach (string s in new StringReplacementPermuter("abef",
'a', 'e').Permute())
             {
                 Console.WriteLine(s);
             }

             Console.Read();
         }
     }

Author
8 Mar 2009 5:33 PM
Abhishek
First convert it to VB.net, then simplify it and then convert it to VB6.
that would be much easier.
Author
9 Mar 2009 5:39 PM
Nobody
"Abhishek" <u***@server.com> wrote in message
news:%23FUy2PBoJHA.4872@TK2MSFTNGP04.phx.gbl...
> First convert it to VB.net, then simplify it and then convert it to VB6.
> that would be much easier.

Here is one C# to VB.Net translator like Abhishek suggested.

http://authors.aspalliance.com/aldotnet/examples/translate.aspx
Author
9 Mar 2009 6:43 PM
Tom Shelton
On 2009-03-09, Nobody <nob***@nobody.com> wrote:
> "Abhishek" <u***@server.com> wrote in message
> news:%23FUy2PBoJHA.4872@TK2MSFTNGP04.phx.gbl...
>> First convert it to VB.net, then simplify it and then convert it to VB6.
>> that would be much easier.
>
> Here is one C# to VB.Net translator like Abhishek suggested.
>
> http://authors.aspalliance.com/aldotnet/examples/translate.aspx
>
>

I just tried it, just to see what it would do.  This code, is going to be
difficult even to translate to VB.NET because it uses a feature of C# (yield
return) that is not supported by the VB.NET language.  It's not impossible,
but would require more work...

--
Tom Shelton
Author
12 Mar 2009 12:25 AM
Bill McCarthy
"Tom Shelton" <tom_shel***@comcastXXXXXXX.net> wrote in message
news:%23gex2bOoJHA.1168@TK2MSFTNGP05.phx.gbl...
>
> I just tried it, just to see what it would do.  This code, is going to be
> difficult even to translate to VB.NET because it uses a feature of C#
> (yield
> return) that is not supported by the VB.NET language.  It's not
> impossible,
> but would require more work...
>

Yeh there's no simple translation into VB6.  You could try making enumerable
classes, creating a new one for each of the iterations so as to store the
state, but it's going to be messy.  I think I'd approach this using nested
For Each loops. With strings that shouldn't make a difference.
Author
8 Mar 2009 6:38 PM
Galen Somerville
Show quote Hide quote
"Ulf Christenson" <u.christen***@googlemail.com> wrote in message
news:ujFlR2AoJHA.996@TK2MSFTNGP03.phx.gbl...
>I have this code, but I don't know how to translate these IEnumerable and other tricky
>code to VB6.
> If anybody could help me it would really make my day. I cannot do it yet.
> Ulf
>
>         private class StringReplacementPermuter
>         {
>             private char[] equiChar;
>             private string inputString;
>             private int[] indexIntoString;
>             private int[] indexIntoChar;
>             public StringReplacementPermuter(string inputString, char e, char a)
>             {
>                 equiChar = new char[] { e, a };
>                 this.inputString = inputString;
>             }
>
>             public IEnumerable<string> Permute()
>             {
>                 Prepare();
>
>                 return GetReplacementStrings(0);
>             }
>
>             private void Prepare()
>             {
>                 List<int> occurenceList = new List<int>();
>                 int occurrenceAt = -1;
>                 while ((occurrenceAt = inputString.IndexOfAny(equiChar, occurrenceAt +
> 1)) != -1)
>                 {
>                     occurenceList.Add(occurrenceAt);
>                 }
>
>                 indexIntoString = occurenceList.ToArray();
>                 indexIntoChar = new int[indexIntoString.Length];
>             }
>
>             private IEnumerable<string> GetReplacementStrings(int loop)
>             {
>                 if (loop == indexIntoString.Length)
>                 {
>                     yield return ReplaceByMap();
>                 }
>                 else
>                 {
>                     for (int i = 0; i < equiChar.Length; i++)
>                     {
>                         indexIntoChar[loop] = i;
>
>                         foreach (string os in GetReplacementStrings(loop + 1))
>                         {
>                             yield return os;
>                         }
>                     }
>
>                     indexIntoChar[loop] = 0;
>                 }
>             }
>
>
>             private string ReplaceByMap()
>             {
>                 StringBuilder s = new StringBuilder(inputString);
>
>                 for (int j = 0; j < indexIntoChar.Length; j++)
>                 {
>                     s[indexIntoString[j]] = equiChar[indexIntoChar[j]];
>                 }
>
>                 return s.ToString();
>             }
>
>         }
>
>         static void Main(string[] args)
>         {
>             foreach (string s in new StringReplacementPermuter("abef", 'a',
> 'e').Permute())
>             {
>                 Console.WriteLine(s);
>             }
>
>             Console.Read();
>         }
>     }

I've used a C to VB converter with pretty good results.

If you want to try it http://home.surewest.net/galen/download/CtoVB.zip

Galen
Author
8 Mar 2009 6:16 PM
Ulf Christenson
The code I posted is C#, but the C to VB6 converter is amazing anyway!!
But it only accepts header files. Is there a version out there that
converts .c files to VB6 as well?
The only converters I found were C to VB.NET, but not to VB6.

Thanks a lot!
Ulf