Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.NET to C Code - Can someone please help?

Status
Not open for further replies.

Kate32

MIS
Jan 30, 2005
6
US
I have been coding with .NET for about 2 years and wanted to refresh my skills in C (a very long time ~10 years) and wanted to know if someone could assist me. I have the following code that is rather simple: it gets a user's input string and takes out the spaces and pops out each word. I would greatly appreciate any assistence!
Code:
'String tokenizer class
Private s As String, i As Integer
Private sep As String   'token separator
Private stokens() As String 'array of tokens
Public Sub init(ByVal st As String)
 s = st
 setSeparator " "
End Sub
Private Sub Class_Initialize()
 sep = " "   'default is a space separator
End Sub
Public Sub setSeparator(ByVal sp As String)
  sep = sp
  stokens = Split(s, sp)
  i = -1
End Sub
Public Function nextToken() As String
 Dim tok As String
 If i < UBound(stokens) Then
   i = i + 1
   tok = stokens(i)
 Else
   tok = ""
 End If
 nextToken = tok    'return token
End Function
Code:
Private Sub Tokenize_Click()
 Dim tok As New Tokenizer
 Dim s As String

 tok.init txString.Text  'set the string from the input
 lsTokens.Clear          'clear the list box
 s = tok.nextToken       'get a token
 While Len(s) > 0        'as long as not of zero length
   lsTokens.AddItem s    'add into the list
   s = tok.nextToken     'and look for next token
 Wend
End Sub
 
Do you have a specific problem you need solved?


__________________________________________
Try forum1391 for lively discussions
 
:) Do you mean some C code similar to this?

#include <string.h>
#include <stdlib.h>

short int ix, iz;
char delms[20], str[200], *token;

strcpy(str,"11,12,13,51,501,68"); //String data line

strcpy(delms,","); //Use comma delm
token= strtok(str,delms);

while(token != NULL)
{
//Extract all the comma delimated tokens
ix = atoi(token);
token= strtok(NULL,delms); //Do next token
}

Regards,
Terry
(Prefer VB.NET to C now)
 
Excuse the heresy in the VB forum, but C# is the way to go. It's neat and clean, isn't as unwieldy as Java, and doesn't have the historical baggage that VB.NET brings to the party (like array indexes that begin at 1, and the awful 'is not Nothing', to name but two...).
 
stevexff said:
An array index begins at 0 by default in VB.Net. Also, I was unware that this post was a discussion of VB.Net vs. C#. It seemed to me to be a post asking for help with C.
 
(like array indexes that begin at 1, and the awful 'is not Nothing', to name but two...).

In addition to RG's correction, VB.Net 2k2 and 2k3 do not have "Is Not Nothing", they have "Not Is Nothing", 2k5 will have "Is Not Nothing".

You may be confussing VB.Net with VB, which had the IsNull funtion, and there were some specific objects that contained 1 based arrays, althought I think they were old RDO objects from VB 5.0.

As for the given situation, I would recommend converting it to C#. While C isn't object oriented, C++ is. But the differences between C# and C++ code whys would be minor.

Just remember Type before Name, and = <> == and <> = !=

;)

-Rick


----------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top