-
January 19th, 2010, 02:22 PM
#1
Override method
Hi wondering if anyone can help i need to create and method which checks that a message contains capital letters from A to Z and if so returns a string that is obtained by replacing each occurance of Z by A each Y by B each C by X and so on.
-
January 19th, 2010, 03:14 PM
#2
Re: Override method
Hello,
Something like that ?
Code:
Dim origstring As String, titi() As Byte, translated As String, i As Integer
origstring = "ABC DEFGHIJKLMNOPQRSTUVWXYZDC"
titi = StrConv(origstring, vbFromUnicode)
translated = ""
For i = 0 To UBound(titi)
If Chr(titi(i)) Like "[A-Z]" Then
translated = translated & Chr(90 - titi(i) + 65)
Else
translated = translated & Chr(titi(i))
End If
Next
MsgBox translated
which could of course be written that way too (might be a bit faster)
Code:
Dim origstring As String, titi() As Byte, translated As String, i As Integer
origstring = "ABC DEFGHIJKLMNOPQRSTUVWXYZDC"
titi = StrConv(origstring, vbFromUnicode)
translated = ""
For i = 0 To UBound(titi)
Select Case titi(i)
Case 65 To 90
translated = translated & Chr(90 - titi(i) + 65)
Case Else
translated = translated & Chr(titi(i))
End Select
Next
MsgBox translated
Last edited by moa; January 19th, 2010 at 04:36 PM.
-
January 19th, 2010, 05:21 PM
#3
Re: Override method
I get the error
"Error 1 Name 'vbFromUnicode' is not declared. C:\MT264\Block 2\Unit 5\StringTester\MT264String.vb 176 36 StringTester"
i'll expand on my first post,
The method ive been trying and failing on is one that checks that the imputted text is in capitals from A to Z and if so changes A to Z, B to Y, D to X and so on. for example "imputed text is ABC result = ZYX
Last edited by cptfantuti; January 19th, 2010 at 05:21 PM.
Reason: spelling :/
-
January 19th, 2010, 05:34 PM
#4
Re: Override method
Originally Posted by cptfantuti
I get the error
"Error 1 Name 'vbFromUnicode' is not declared. C:\MT264\Block 2\Unit 5\StringTester\MT264String.vb 176 36 StringTester"
Huh...
Are you sur you are using VB6 ?
EDIT : as my code is working on VB6 and on VBA, I just reckon you are developping under something else (most probably VB.Net, which does not use vbFromUnicode) ...
If so : please ask an administrator to move your discussion to the right place.
Last edited by moa; January 19th, 2010 at 05:42 PM.
-
January 19th, 2010, 06:56 PM
#5
Re: Override method
Originally Posted by moa
Huh...
Are you sur you are using VB6 ?
EDIT : as my code is working on VB6 and on VBA, I just reckon you are developping under something else (most probably VB.Net, which does not use vbFromUnicode) ...
If so : please ask an administrator to move your discussion to the right place.
Im using Microsoft Visual Basic 2008 Express Edition.
-
January 19th, 2010, 07:46 PM
#6
Re: Override method
Then, this belongs in the VB.Net forum!
-
January 19th, 2010, 09:31 PM
#7
Re: Override method
ok ty and sorry for posting in the wrong forums.
-
January 20th, 2010, 04:03 AM
#8
Re: Override method
you could use different and faster ways, but here is a small piece of code that does do what you want.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const Alpha As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim s As String = TextBox1.Text.ToUpper()
Dim sb As New System.Text.StringBuilder
Dim index As Integer
For Each c As Char In s
index = Alpha.IndexOf(c)
index = Alpha.Length - index
sb.Append(Alpha.Substring(index - 1, 1))
Next
TextBox1.Text = sb.ToString()
End Sub
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
January 21st, 2010, 03:05 AM
#9
Re: Override method
Originally Posted by cptfantuti
Hi wondering if anyone can help i need to create and method which checks that a message contains capital letters from A to Z and if so returns a string that is obtained by replacing each occurance of Z by A each Y by B each C by X and so on.
Here you are Fantuti - some code that does exactly what you want and which I believe will work on any VB platform (the following was written in VB2005) and I'm pretty sure it will work in VB6.
Setup two texboxes called txtSource and txtTarget together with a button and place the following code within the Button_Click event. The utility will examine the text that is in txtSource and deposit the result in txtTarget.
It is simple, understandable, maintainable and portable:
Const CODEA As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Const CODEB As String = "ZYXWVUTSRQPONMLKJIHGFEDCBA"
Dim strInput As String
Dim strOutput As String
Dim intLoop As Integer
Dim intPos As Integer
Dim strChar As String
strInput = Trim(txtSource.Text)
strOutput = ""
For intLoop = 1 To Len(strInput)
strChar = Mid$(strInput, intLoop, 1)
intPos = InStr(CODEA, strChar)
Select Case intPos
Case Is > 0
strOutput = strOutput & Mid$(CODEB, intPos, 1)
Case Else
strOutput = strOutput & strChar
End Select
Next intLoop
txtTarget.Text = strOutput
Whether this question has been placed in the right place or not I am not sure. What I am sure of is that you have a solution to your issue which you can block copy and use immediately.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|