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
Re: Syntax error in string manipulation
Where have you declared "characterarray"?
Re: Syntax error in string manipulation
It is declared as Public at the top of Form
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
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
====================================================
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.
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
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!
Re: Syntax error in string manipulation
Quote:
Hope I made it a bit less complicated!
Well I don't know about gep13 but it just makes my head hurt:confused:
The fact remains that you appear to be trying to call string methods on an array, which won't work.
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
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
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 :D
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