Results 1 to 12 of 12

Thread: Syntax error in string manipulation

  1. #1

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

    Syntax error in string manipulation

    Hey,

    Some help please!

    I am trying an encrypting exercise which takes the txtmessage and encrypts it in characterarray.
    IStartAt is the starting point and by taking one character at a time from txtmessage I need to insert it in characterarr\y allowing a gap in between which is IUseGap. KeyGap and Keyfirst will be the next two characters after txtmessage and when insert have no gap in between.

    I think the logic I done is good however, I get syntax errors on the red lines.

    Please Help me get this right. Many thanks

    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsecrettMessage.Click
            'Splitletters()
            Dim KeyFirst, KeyGap, secretmessage As String
            Dim encodestring As String = txtmessage.Text
            lStartAt = 200 + lUse5th
            Dim lPos As Long
            KeyFirst = Asc(encodestring(0))
            KeyGap = (Asc(lUseGap))
            encodestring += String.Format(KeyFirst + KeyGap)
            For lPos = 1 To encodestring.Length
                characterarray.Substring(((lPos - 1) * lUseGap) + lStartAt) = encodestring.Substring(lPos - 1)     
    
           If (encodestring.Length - 2 = True) Then
    characterarray.replace (1Pos+1,KeyFirst)  
    
                End If
    
                If (encodestring.Length - 1 = True) Then
    characterarray.replace (1Pos+2,KeyGap)      
            End If
            Next
    
            secretmessage = characterarray.ToString
    
        End Sub
    Last edited by angelica; Apr 15th, 2009 at 05:51 AM.
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

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

    Re: Syntax error in string manipulation

    Where have you declared "characterarray"?

  3. #3

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

    Re: Syntax error in string manipulation

    It is declared as Public at the top of Form
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Syntax error in string manipulation

    Hey,

    Can you tell us what are the errors that you are getting?

    Also, fair enough characterarray is declared as Public at the top of your form, but what is it's type?

    Gary

  5. #5

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

    Re: Syntax error in string manipulation

    Dim characterarray() As Char

    Public Sub Main()
    characterarray = RandomGen1.ToCharArray()

    ===============================================
    characterarray.Substring(((lPos - 1) * lUseGap) + lStartAt)
    Errror on: Expression is a value and therefore cannot be the target of an assignment
    ================================================

    ======================================================
    characterarray.replace (1Pos+1,KeyFirst)
    Overload failed because no acceccible "Replace" accpets this number of arguments

    ====================================================
    Last edited by angelica; Apr 15th, 2009 at 07:20 AM.
    ------------------------------------------------------------------------
    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: Syntax error in string manipulation

    I'm confused.

    Arrays don't have a Substring or Replace method and so you should get errors like :

    'Replace' is not a member of 'System.Array'

    If characterarray was a string it would make more sense.

    The first error you are getting would be because you cant assign values to the result of a "substring" function - it is only for reading parts of a string, not for setting parts of it.
    Last edited by keystone_paul; Apr 15th, 2009 at 07:40 AM.

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Syntax error in string manipulation

    Hey,

    I would agree with Paul on this one.

    The methods that you are trying to use do not exist on that Type. Are you perhaps trying to index into the array?

    I might help by explaining exactly what you are trying to achieve.

    Gary

  8. #8

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

    Re: Syntax error in string manipulation

    What I am trying to do is this:

    I have a user input which is : Dim encodestring As String = txtmessage.Text

    and Im trying to insert it by replacing strings in the characterarray which is a randomgenerator declared in the

    Code:
    Sub Main:
    Dim characterarray() As Char
    characterarray = RandomGen1.ToCharArray()
    Now I get an integer IUseGap which is an integer which will serve as the character spaces between the number of characters in between each letterof encodestring.

    Then I get a KeyGap (integer converted to Ascii) and KeyFirst (converted dto ascii which will be the end of the insertion.

    I have to do these replacing .

    Let me give you an example

    encodestring = HEY

    and should be inserted in randomGen1 = abcdefgvvvvvvvvvvvhijkoo

    so the IuseGap would be 4 ( distance between i and length)
    and first would be "H"

    so for example I need to do abHdefgEvvvvY4Hvvvvvhijkoo

    Hope I made it a bit less complicated!
    Last edited by angelica; Apr 15th, 2009 at 08:32 AM.
    ------------------------------------------------------------------------
    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: Syntax error in string manipulation

    Hope I made it a bit less complicated!
    Well I don't know about gep13 but it just makes my head hurt

    The fact remains that you appear to be trying to call string methods on an array, which won't work.

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Syntax error in string manipulation

    Hey,

    I am going to have to give this one some more thought when I am not at work.

    What you are requesting is possible, but it will take some work.

    My question would be though, are you trying to copy some pre-existing code? Intellisense in the IDE would have told you that those methods don't exist.

    For the members that are available, have a look here:

    http://msdn.microsoft.com/en-us/libr...y_members.aspx

    Gary

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Syntax error in string manipulation

    two things....
    1) Replace takes two CHARACTER or STRING parameters.... and it RETURNS a string with the replaced bits.... so to use it to replace things use it like this: strMyString = strMyString.Replace("find this","Replace with")
    2) characterarray.replace (1Pos+1,KeyFirst) -- I'm hoping it's just a typo but to the best of my knowledge... you can't have variables start with a number (that's a 1 (one) not an l (lower case L)) ... even if you can... 1Pos and lPos aren't the same thing... even if they look similar.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

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

    Re: Syntax error in string manipulation

    Thanks guys,

    Techgnome I corrected the 1Pos and replace but Im still stuck. My latest version stand like this:

    The charAt are no VB.Net syntax but Im at a loss which to use

    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsecrettMessage.Click
            'Splitletters()
            Dim KeyFirst, KeyGap, secretmessage As String
            Dim encodestring As String = txtmessage.Text
            lStartAt = 200 + lUse5th
            Dim Pos1 As Long
            Dim Pos2 As Long
            KeyFirst = Asc(encodestring(0))
            KeyGap = (Asc(lUseGap))
            encodestring += String.Format(KeyFirst + KeyGap)
            For Pos1 = 1 To encodestring.Length
                characterarray.charAT((Pos1 - 1) * lUseGap + lStartAt) = encodestring.charAT(Pos1 - 1)
                If (encodestring.Length - 2 = True) Then
                    characterarray = characterarray.Replace(Pos1 + 1, KeyFirst)
    
                End If
                If (encodestring.Length - 1 = True) Then
                    characterarray = (characterarray.Replace(Pos1 + 2, KeyGap))
    
                End If
            Next
    
            secretmessage = characterarray.ToString
    
        End Sub
    ------------------------------------------------------------------------
    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