VB conversion - whats going on?
OK so this is really doing my head in since I am using VB to convert to C# and the variable names don't help in addition to the whole zero counts as 1 in C#
Code:
Public Function Parsec(stmt As Variant, delim As String, stmtinx() As Long) As Long
Dim i As Long
Dim j As Long
Dim k As Long
Dim l As Long
Dim m As Long
k = UBound(stmtinx) - 2
m = Len(delim)
Erase stmtinx
If (m < 1) Then Exit Function
i = 1
For l = 1 To k
stmtinx(l) = i
j = InStr(i, stmt, delim)
If (j < 1) Then Exit For
i = j + m
Next
stmtinx(l + 1) = Len(stmt) + 1
stmtinx(0) = l
Parsec = l
End Function
how do I convert this to C#? I mean...I think I got it but the problem is, when comparing the output between the C'# and VB, it always is out by 1 number (C# is 1 number ahead)
whether or not this is correct when it comes to C# land - im not sure. I think it is.
here is the data I use to put through the function:
Quote:
stmt = " | |08/16/2012 21:46:01|0799555065|*205505555555558|001029604658953|038800C8080801140010296046589533106507995550650 2C050000|310650799555065|08/08/2012 08:01:20||||xxx||||||||||||||||||||||||||||||||||||||||||||||"
delim = "|"
stmtinx is an array length of 96 in VB:
Dim stmtinx(96) as long
I hope someone can help me :) my brain is...frazzled
Re: VB conversion - whats going on?
I'm not even sure I can tell what it does.... it looks like it's looking for the delimiters... but it's not extracting the data... is it simply storing the position of each delimiter? Well that can't be right... the calculation of i doesn't look right.... perhaps rather than converting that code (which is VB6 anyways, and should be upgraded to VB.NET before converting to C#) ... you can explain what it's supposed to do... there maybe a more efficient way of going about it... right now, it looks a little jacked.... like I said, that calculation of i looks wonky.
-tg
Re: VB conversion - whats going on?
yeh but it works. what is it doing? No idea. I can't wait to find out what it does but just have to DO IT you know?
it works and has done for years..... literally. better way of doing it? Sure. but not enough time to test the new method either, so since this works - just "port it over"
Re: VB conversion - whats going on?
VB's string handling methods are 1-based, while other languages are 0-based.
Try the following:
Code:
public long Parsec(string stmt, string delim, long[] stmtinx)
{
long i = 0;
long j = 0;
long k = 0;
long l = 0;
long m = 0;
k = stmtinx.GetUpperBound(0) - 2;
m = delim.Length;
stmtinx = null;
if (m < 1)
{
return 0;
}
i = 1;
for (l = 1; l <= k; l++)
{
stmtinx[l] = i;
j = stmt.IndexOf(delim, i - 1) + 1;
if (j < 1)
{
break;
}
i = j + m;
}
stmtinx[l + 1] = stmt.Length + 1;
stmtinx[0] = l;
return l;
}
Re: VB conversion - whats going on?
Quote:
Originally Posted by
David Anton
VB's string handling methods are 1-based, while other languages are 0-based.
vb.net's arrays are zero based. vb6 had optional lowerbounds
Re: VB conversion - whats going on?
Right - I was referring to the legacy string methods, such as "InStr", which is in the posted code.
Re: VB conversion - whats going on?
Quote:
Originally Posted by
Techno
MVP 2007-2010 any chance of a regain?
not a hope in hell:D
Re: VB conversion - whats going on?
thanks. almost there. I had to new up the array buffer when you assigned it to null instead.
we have 1 extra element in there.... (the last one probably)
Re: VB conversion - whats going on?
Quote:
Originally Posted by
.paul.
not a hope in hell:D
yeh I know.... but hey I work alot and too much for my brain to keep things in as im getting older. LOL
but my day will come again. you just wait ;)
Re: VB conversion - whats going on?
all those long variables... i,j,k,l,m... should be integers?
Re: VB conversion - whats going on?
Quote:
Originally Posted by
Techno
yeh I know.... but hey I work alot and too much for my brain to keep things in as im getting older. LOL
but my day will come again. you just wait ;)
ok. i've set my timer... i'm watching you:D
Re: VB conversion - whats going on?
Quote:
Originally Posted by
Techno
yeh I know.... but hey I work alot and too much for my brain to keep things in as im getting older. LOL
but my day will come again. you just wait ;)
ok. i've set my timer... i'm watching you:D
Re: VB conversion - whats going on?
p.s. i didn't post twice for emphasis
Re: VB conversion - whats going on?
ok so trying to get my brain to almost work. Regardless - the contents of the array must be the same in both VB and C# and using the same "input" data. yes... thats right.
so thanks to david, almost there. But there appears to be 1 extra element in the array
Re: VB conversion - whats going on?