Migration from VisualBasic

I am trying to migrate a program from VisualBasic .Net to XOJO and I am having trouble with a VB function that I use to count the specific occurrences of a character in a string. In VisualBasic, the code is implemented as follows:

Public Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer
Dim cnt As Integer = 0
For Each c As Char In value
If c = ch Then cnt += 1
Next
Return cnt
End Function

I used the VB migration tool to import my VB project into XOJO and I have modified this function to:

ccount(value as string, ch as string) as integer
Dim cnt As Integer = 0

For Each c As string In value
If c = ch Then cnt = cnt+1
Next

Return cnt

However, when I try to compile my program, I get a syntax error saying that the variable called ‘value’ is “not an array, but you are using it as one”.

Does anyone know how I should alter my code so that this function will work as expected?

[code]Function CCount(value as String, ch as String) As Integer
Dim cnt As Integer = 0

dim letters() as string = Split(Value,"") //create an array of letters from the word
For Each c As string In letters //loop through the array
If c = ch Then cnt = cnt+1
Next

Return cnt
End Function[/code]

It is better to click the code icon before you paste or type code, to make it more legible.

You may want to use the split function to obtain the number of matching characters :

Function ccount(value as string, ch as string) As integer dim anArray(-1) as string anArray = split(value,ch) return UBound(anArray) End Function

Hi Michel and Jason,

Thanks for your help. The modified function works as expected.