CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Override method

  1. #1
    Join Date
    Jan 2010
    Posts
    7

    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.

  2. #2
    Join Date
    Oct 2006
    Posts
    327

    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.

  3. #3
    Join Date
    Jan 2010
    Posts
    7

    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 :/

  4. #4
    Join Date
    Oct 2006
    Posts
    327

    Re: Override method

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

  5. #5
    Join Date
    Jan 2010
    Posts
    7

    Re: Override method

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

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Override method

    Then, this belongs in the VB.Net forum!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Jan 2010
    Posts
    7

    Re: Override method

    ok ty and sorry for posting in the wrong forums.

  8. #8
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    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.

  9. #9
    Join Date
    Jan 2010
    Posts
    38

    Re: Override method

    Quote Originally Posted by cptfantuti View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured