Results 1 to 12 of 12

Thread: easy code?

  1. #1

    Thread Starter
    Hyperactive Member sterankin's Avatar
    Join Date
    Jul 2001
    Location
    N.Ireland
    Posts
    336

    easy code?

    This should prove no problem, but my VB is very rusty.

    Say the user enters a string of 12 digit numbers, it will always be 12 digits. e.g.

    235467890318

    What is the code to loop through this string, and retrieve the numbers at positions 6 7 8 9 and 10

    (i.e. in this eg the numbers needed are 78903)


    Hope that makes sense, many thanks

    Steve

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    VB Code:
    1. Private Sub printOutFrom(ByVal strString As String, ParamArray atPositions())
    2.     Dim i As Long, x As String: x = "," & Join(atPositions, ",") & ","
    3.     For i = 1 To Len(strString)
    4.         If InStr(x, "," & i & ",") <> 0 Then
    5.             Debug.Print "Character at position : " & i & " : " & Mid(strString, i, 1)
    6.         End If
    7.     Next
    8. End Sub
    9.  
    10. Private Sub Form_Load()
    11.     Dim a As String: a = "235467890318"
    12.     printOutFrom a, 6, 7, 8, 9, 10
    13. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    MerryVIP
    Guest
    VB Code:
    1. Dim MyNumber As String
    2. Dim Result As String
    3. MyNumber = 235467890318
    4. Result = Mid(MyNumber, 6, 5)

    And mid is like this:
    Mid(String, Starting position (first is 1), How many characters?)

    Hope this helps,

  4. #4
    MerryVIP
    Guest
    plenderj: Why make the thing so complicated?

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by MerryVIP
    plenderj: Why make the thing so complicated?
    More robust
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  6. #6
    MerryVIP
    Guest
    But wasn't he asking easy code?

  7. #7

    Thread Starter
    Hyperactive Member sterankin's Avatar
    Join Date
    Jul 2001
    Location
    N.Ireland
    Posts
    336

    yeah

    the easy code works great, the other code scares me

    Thanks to you both

  8. #8
    MerryVIP
    Guest
    It's nice to help And to be "right" Okay, I better silent myself or I don't have a head when I leave...

  9. #9
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I think my code is ok... doesn't look too scary to me
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  10. #10
    MerryVIP
    Guest
    The thing is in the amount of code and if it's easy for a beginner to understand. The less code, the less complex, the easier it is. I think I'm kind of professional in this, keeping things simple and understandable (I have done that all my life, one of my targets always when I've coded a program/game).

    Anyways, what were too much in your example:

    - new sub, which contains a lot of things, which a beginner probably doesn't know (to mention, declaring strings and ByVal)
    - use of for-loop, if, InStr, Debug-window and Mid together really makes it messy to look at except for an experienced coder like you and me (yet, I needed to think it a few times before I understood everything - beginners usually want something to come out fast too)
    - the code lines are too long (sometimes this is a "have-to-do" situatation, but not in this case) (it's far easier to read many short lines vertically, seeing fast what a line does, than to read one really long line)


    Yay, I wrote a that...people could learn something about it

  11. #11
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I think the ParamArray is probably the most extreme thing in there
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  12. #12
    MerryVIP
    Guest
    Heh, I missed that one That thing doesn't even exist in VB4...darn, I talk a lot about that, VB4

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