-
string manipulation error
hey, can someone please tell me why IUse Gap is always returning a "0" please???
Code:
Public Sub Main()
Dim characterarray() As Char
characterarray = RandomGen1.ToCharArray() 'this is a functions that gives a random generator of 200 characters
Dim lUseGap As Long, lUse5th As Long
lUseGap = FindGap(characterarray)
End Sub
Private Function FindGap(ByVal characterarray() As Char) As Long
Dim gap As Long
Dim vowelcounter, i As Integer
Dim vowelstring As String = "AEIOU"
For i = characterarray.Length - 1 To 0
If vowelstring.Contains(characterarray(i).ToString.ToUpper()) Then
vowelcounter += 1
If vowelcounter = 3 Then
'letter5vowel = characterarray(i)
Dim indexLast3 As Integer = RandomGen2.IndexOf(characterarray(i))
gap = (characterarray.Length - 1) - CInt(Val(RandomGen2(indexLast3)))
'Chr(AscW(chargap)) = Asc(gap)
MessageBox.Show(characterarray(i).ToString())
End If
Exit For
End If
Next
FindGap = gap
Return FindGap
End Function
-
Re: string manipulation error
You may need to change this line to this:
Code:
For i = characterarray.Length - 1 To 0 Step -1
So it counts backwards.
-
Re: string manipulation error
Yes I added it thanks, Yet it still would not loop. It is not getting inside the loop
-
Re: string manipulation error
Daft question - when you step through the code what does characterarray.length evaluate to at the start of the loop.
NB - you may also want to rejig the line "Dim vowelcounter, i As Integer" as you havent specified a type for vowelcounter. Not that that would cause the problem - just good practice!
-
Re: string manipulation error
characterarray.length evaluates to "0"
-
Re: string manipulation error
So theres your answer - the array is empty so of course it will never enter the loop!
I think you need to investigate RandomGen1 - what exactly is this and how does it work?
-
Re: string manipulation error
An array with the length zero means that it contains no element. So your next step is to find out why the characterarray doesn't have any elements in it, and that points to RandomGen1 string.... Can you show the code that creates this string?
-
Re: string manipulation error
Code:
Public Class Form1
Public RandomGen1, RandomGen2, strmessage, characterarray As String
Public letter5vowel As String
Dim lUseGap As Long, lUse5th As Long
Dim lStartAt As Long
==============================================
Public Sub Main()
Dim characterarray() As Char
characterarray = RandomGen1.ToCharArray()
Dim encodestring As String = txtmessage.Text
lUseGap = FindGap(characterarray)
lUse5th = Find5thvowel(characterarray)
Code:
Private Sub RandomGenerator1()
Dim AscChr As String, ChrAsc As String
Dim counter As Integer = 0
txtRandom1.Text = ""
For counter = 0 To 199
Randomize()
AscChr = Abs(Int(64 - 122) * Rnd() - 64)
ChrAsc = Chr(AscChr)
txtRandom1.Text += String.Format(ChrAsc)
RandomGen1 = txtRandom1.Text
Next
End Sub
-
Re: string manipulation error
I'm really confused as how this even runs, given the errors on the other thread. Your VB environment seems to be allowing strange things to happen.
RandomGenerator1 is a sub not a function, so it is never going to return anything.
Do you have Option Explicit set in your application settings?
A quick fix for this is to change from Private Sub RandomGenerator1() to read
Code:
Private Function RandomNumberGenerator1() as String
But I think you need to check out your settings because VB really ought to be complaining like crazy about some of this code!
-
Re: string manipulation error
You declare the public RandomGen1 variable but never initialize or assign any value to it.
You need to call the sub RandomGenerator1 before you use the RandonGen1 variable.
-
Re: string manipulation error
EDIT - sorry I misread something -
RandomGen1 = txtRandom1.Text
Presumably this should actually read
RandomNumberGenerator1 = txtRandom1.Text
That would work if you change the routine to a function from a sub.
EDIT again - seeing stanav's reply now I understand what you are trying to do - as he said you need to call the subroutine, although in my view it would make more sense to just have a function!
-
Re: string manipulation error
It is used like this . It is being generated within a Function take takes a char and returns an integer.
Code:
Public Sub Main()
Dim characterarray() As Char
characterarray = RandomGen1.ToCharArray()
Dim encodestring As String = txtmessage.Text
lUseGap = FindGap(characterarray)
lUse5th = Find5thvowel(characterarray)
-
Re: string manipulation error
Yes but the point stanav is making is you haven't actually called the procedure RandomNumberGenerator1 which populates RandomGen1.
Try inserting the call like below:
Code:
Dim characterarray() As Char
RandomNumberGenerator1
characterarray = RandomGen1.ToCharArray()
Dim encodestring As String = txtmessage.Text
lUseGap = FindGap(characterarray)
lUse5th = Find5thvowel(characterarray)
If you put a break point within that procedure (RandomNumberGenerator1) at the moment and run your code you'll see that it is never actually running and hence not setting RandomGen1, and hence you get an emptry string.
-
Re: string manipulation error
Ok I see your point. True however I have called it on Form Load which should be initialised from there (forgot to point it out sorry). Even though the characterarray is still 0 and I am really at a loss
-
Re: string manipulation error
OK... how about this. Add this function :
Code:
Private Function GetRandomString() As String
Dim ReturnString As New System.Text.StringBuilder
Dim AscChr As String, ChrAsc As String
For Counter As Integer = 0 To 199
Randomize()
AscChr = Abs(Int(64 - 122) * Rnd() - 64)
ChrAsc = Chr(AscChr)
ReturnString.Append(ChrAsc)
Next
Return ReturnString.ToString
ReturnString = Nothing
End Function
And in your existing code change the line
Code:
characterarray = RandomGen1.ToCharArray()
to
Code:
characterarray = GetRandomString.ToCharArray()
-
Re: string manipulation error
On Form Load
GetRandomString()
Dim RandomStr As String = ReturnString ====gives an error here
-
Re: string manipulation error
Sorry I'm lost - what is that?
-
Re: string manipulation error
Sorry I had to go for a while. Now what I meant was that in the Form Load I have to assign the return parameter to a string and I did it like this and gives no error now. But still I get the same "0" return in the FindGap() of my first tops in this thread ???
Dim RandomStr As String = GetRandomString()
-
Re: string manipulation error
OK so can you put a breakpoint in FindGap on the "For i =" line, run the code and check characterarray.length again to check whether the array is empty or has a random string in it.
-
Re: string manipulation error
Think I got somewhere. Thanks for all your help Keystone Paul and the other guys
Code:
Private Function FindGap2(ByVal characterarray As String) As Long
Dim RandomStr As String = GetRandomString()
Dim gap As Integer
Dim Vowels As String = "AEIOU"
Dim VowelNum, Index As Integer
For Index = RandomStr.Length - 1 To 0 Step -1
If Vowels.Contains(RandomStr.Substring(Index, 1).ToUpper) Then
VowelNum += 1
End If
If VowelNum = 3 Then
gap = ((RandomStr.Length - 1) - (Index)
MessageBox.Show(gap.ToString())
Exit Function
End If
Next
Return gap
End Function
-
Re: string manipulation error
No problem. If it is working now please remember to mark the thread resolved using the Thread Tools menu toward the top of the page.
-
Re: string manipulation error
Hey ,
Just this last bit with this one please. When I access the UseGap from outside the Function it still returns a "0", even though UseGap is global. How do I go about calling the Function to return the integer please?
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btngetgap.Click
MessageBox.Show(UseGap.ToString())
End Sub
-
Re: string manipulation error
Is it UseGap, or lUseGap?
Your initial post suggests you should be using lUseGap unless I'm misreading it:
Code:
Dim lUseGap As Long, lUse5th As Long
lUseGap = FindGap(characterarray)
If that is the problem it suggests you don't have "Option Explicit" set on. That is vitally important otherwise you can have all kinds of errors that you haven't spotted (I'm really don't know why Microsoft give the option of not having it set, it is that bad!)
-
Re: string manipulation error
Guess it's not the IUseGap, cos Ive changed the name in all its occurences.
-
Re: string manipulation error
OK can you post what the code looks like now please so we can see exactly what you are running?
-
Re: string manipulation error
Code:
Public RandomGen1, RandomGen2, strmessage, characterarray As String
Public letter5vowel, Smessage, SecretMessage As String
Dim UseGap As Long, lUse5th As Long
Dim lStartAt As Long
Dim gap As Long
Public Sub Main()
Dim characterarray() As Char
characterarray = GetRandomString().ToCharArray()
Dim encodestring As String = txtmessage.Text
UseGap = FindGap2(characterarray)
lUse5th = Find5thvowel(characterarray)
SecretMessage = ComputeMessage(characterarray)
End Sub
Private Function GetRandomString() As String
Dim ReturnString As New System.Text.StringBuilder
Dim AscChr As String, ChrAsc As String
For Counter As Integer = 0 To 199
Randomize()
AscChr = Abs(Int(64 - 122) * Rnd() - 64)
ChrAsc = Chr(AscChr)
ReturnString.Append(ChrAsc)
Next
Return ReturnString.ToString
ReturnString = Nothing
End Function
Private Function FindGap2(ByVal characterarray As String) As Long
Dim RandomStr As String = GetRandomString()
Dim Vowels As String = "AEIOU"
Dim VowelNum, Index As Integer
For Index = RandomStr.Length - 1 To 0 Step -1
If Vowels.Contains(RandomStr.Substring(Index, 1).ToUpper) Then
VowelNum += 1
End If
If VowelNum = 3 Then
gap = ((RandomStr.Length - 1) - Index)
MessageBox.Show(gap.ToString())
Exit Function
End If
Next
UseGap = gap
Return UseGap
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btngetgap.Click
'Dim lStartAt As Long
'lStartAt = 200 + lUse5th
'' MessageBox.Show(IStartAt)
FindGap2(characterarray)
MessageBox.Show(UseGap.ToString())
End Sub
-
Re: string manipulation error
Hmm... you have three variables all called characterarray - one defined as a string at module level, and a second defined locally in "sub Main" as an array, and a third as a parameter to FindGap2 as a string.
You are passing in the locally defined one (the array) to FindGap2 which is expecting a string, and then you're not actually referencing any of the three variables called characterarray in the function.
Also in FindGap2 you are returning the value of a module-level variable "gap" which you are only setting from within the routine if VowelNum = 3 at some point in your loop, so if vowelnum never reaches three you are not setting gap so the function will return whatever gap was when you entered the function (0 on the first run).
I think you may need to go back to the drawing board and start again.
To avoid confusion, it is best if you give your module-level variables a prefix (ie m_characterarray) so you know their scope, and to differentiate them from local variables. It is usually best to minimise global and module level variables and instead pass parameters between functions, because to be honest your code is very hard to understand, for example :
You have a public variable "usegap" which you reference in this line in sub Main :
Code:
UseGap = FindGap2(characterarray)
So the intention is that UseGap will be set to the number returned by FindGap2 - right?
But then within the function FindGap2 you have these line :
Code:
UseGap = gap
Return UseGap
Which means that you are going to set UseGap = gap (another module level variable) - so you are changing the UseGap from within the function, and then you are returning UseGap as the output of the function, so UseGap is being set to = UseGap which you've already set!
It is totally convoluted - basically FindGap2 should not be touching any variables outside of its own block and returning a number which you assign to UseGap.
-
Re: string manipulation error
That function is sorted out Keystone_Paul. Thanks for pointing out the dump errors.
Still more to work on to get this application going though!
Many thanks . Reputation well deserved!
-
Re: string manipulation error
Quote:
Originally Posted by
keystone_paul
NB - you may also want to rejig the line "Dim vowelcounter, i As Integer" as you havent specified a type for vowelcounter. Not that that would cause the problem - just good practice!
A little OT, but:
This is .NET. Unlike in VB6 and earlier, that line of code will actually create two integers, and not an object and integer.
-
Re: string manipulation error