Results 1 to 30 of 30

Thread: string manipulation error

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: string manipulation error

    Yes I added it thanks, Yet it still would not loop. It is not getting inside the loop
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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!

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: string manipulation error

    characterarray.length evaluates to "0"
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  6. #6
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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?

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  9. #9
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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!

  10. #10
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  11. #11
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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!
    Last edited by keystone_paul; Apr 15th, 2009 at 08:29 AM.

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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)
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  13. #13
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  15. #15
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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()

  16. #16

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: string manipulation error

    On Form Load
    GetRandomString()
    Dim RandomStr As String = ReturnString ====gives an error here
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  17. #17
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: string manipulation error

    Sorry I'm lost - what is that?

  18. #18

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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()
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  19. #19
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  20. #20

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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
    Last edited by angelica; Apr 15th, 2009 at 02:06 PM.
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  21. #21
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.

  22. #22

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  23. #23
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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!)

  24. #24

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: string manipulation error

    Guess it's not the IUseGap, cos Ive changed the name in all its occurences.
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  25. #25
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: string manipulation error

    OK can you post what the code looks like now please so we can see exactly what you are running?

  26. #26

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  27. #27
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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.
    Last edited by keystone_paul; Apr 16th, 2009 at 07:37 AM.

  28. #28

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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!
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  29. #29
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: string manipulation error

    Quote Originally Posted by keystone_paul View Post
    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.

  30. #30

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: string manipulation error

    Yep have to correct that
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width