Consulting

Results 1 to 13 of 13

Thread: how to make an "isletter" class in vba?

  1. #1
    VBAX Regular
    Joined
    Aug 2011
    Posts
    18
    Location

    how to make an "isletter" class in vba?

    Hi,
    i need to build a function that checks is a single character is a number, lower case letter or upper case.
    it needs to be build as a class.
    can anyone point me in the right direction, what are the propoties, user interface and stracture of this class.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why does it need to be a class, because your class assignment says so?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Aug 2011
    Posts
    18
    Location
    Quote Originally Posted by xld
    Why does it need to be a class, because your class assignment says so?
    Yes, its for my work, it needs to be a class so others can use it on other application's and project's.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    How does a class do that? A class would be contained within a VBA project, which is contained with an Excel workbook, and so is available to that book only unless it gets referenced; but that doesn't needed classes for that.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Aug 2011
    Posts
    18
    Location
    Quote Originally Posted by xld
    How does a class do that? A class would be contained within a VBA project, which is contained with an Excel workbook, and so is available to that book only unless it gets referenced; but that doesn't needed classes for that.
    its going to be referenced with early binding, why dont i need class for early binding?

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What is going to be referenced with early binding? It seems to me that you have a 'solution'. and are scratching around for a problem. Are you sure you are using VBA for this?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Regular
    Joined
    Aug 2011
    Posts
    18
    Location
    Quote Originally Posted by xld
    What is going to be referenced with early binding? It seems to me that you have a 'solution'. and are scratching around for a problem. Are you sure you are using VBA for this?
    This is my code:
    When i presented it i was told i need to improve it, for example to replace
    the case statment(isletter) with something more "class" like.
    this code gets a string from the user and sort it to upper\lower case and numbers.

    ----CLASS MODULE-----

    [VBA]Public Function sort(x As String) As Integer
    Dim i As Integer
    Dim ch As String
    Dim lnum1 As Integer
    Dim lnum2 As Integer
    Dim lnum3 As Integer
    lnum1 = 1
    lnum2 = 1
    lnum3 = 1
    For i = 1 To Len(x)
    ch = Mid(x, i, 1)
    Select Case Asc(ch)
    Case 65 To 90
    Cells(lnum1, 1) = ch
    lnum1 = lnum1 + 1
    Case 97 To 122
    Cells(lnum2, 2) = ch
    lnum2 = lnum2 + 1
    Case 48 To 57
    Cells(lnum3, 3) = ch
    lnum3 = lnum3 + 1
    End Select
    Next
    End Function[/VBA]
    Last edited by Bob Phillips; 01-22-2012 at 10:31 AM. Reason: Added VBA tags

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    1. You said ...

    i need to build a function that checks is a single character is a number, lower case letter or upper case.
    but

    [vba]
    Public Function sort(x As String) As Integer
    Dim i As Integer
    Dim ch As String
    Dim lnum1 As Integer
    Dim lnum2 As Integer
    Dim lnum3 As Integer
    lnum1 = 1
    lnum2 = 1
    lnum3 = 1
    For i = 1 To Len(x)
    ch = Mid(x, i, 1)
    Select Case Asc(ch)
    Case 65 To 90
    Cells(lnum1, 1) = ch
    lnum1 = lnum1 + 1
    Case 97 To 122
    Cells(lnum2, 2) = ch
    lnum2 = lnum2 + 1
    Case 48 To 57
    Cells(lnum3, 3) = ch
    lnum3 = lnum3 + 1
    End Select
    Next
    End Function
    [/vba]


    seems like it seperates each Upper, Lower, and Digits to Columns 1, 2, and 3.

    so I'm more than a little confused as to what it is you're trying to do

    Why not just a simple VBA function? It's just as transportable as a class module

    How about an example of the Before and After?

    Paul
    Last edited by Paul_Hossler; 01-22-2012 at 10:18 AM.

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by xena2305
    When i presented it i was told i need to improve it, for example to replace
    the case statment(isletter) with something more "class" like.
    Who told you that? Case statements are good code, but they have absolutely nothing to do with 'class like'. 'Class like' would refer to properties and methods as far as I can see, but that is just coding semantics (although I do endorse what Paul is saying about what it does).

    Send that fool round to me, I will sort him/her out for you.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Why is that a Function?
    What value do you want returned?
    If its put in a worksheet cell, the write to sheet part will cause it to fail.
    Last edited by mikerickson; 01-22-2012 at 12:24 PM.

  11. #11
    VBAX Newbie
    Joined
    Jan 2012
    Posts
    4
    Location
    I'm confused about the term "class like" as well, since IsNumber, IsEmpty, IsNull, etc, are functions and not classes.

    Perhaps extracting the Select Case bit and putting it into its own IsLetter(string) As Boolean function will make things "look" better?

    I too am curious as to the bigger picture to what problem your code is written to solve, and can't suggest something more "class like" without knowing. As a side note, I personally find classes harder to test, and only use when and where they make the most sense.

    Quote Originally Posted by xena2305
    Hi,
    i need to build a function that checks is a single character is a number, lower case letter or upper case.
    it needs to be build as a class.
    can anyone point me in the right direction, what are the propoties, user interface and stracture of this class.

  12. #12
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    If one were to write a class similar to the Dictionary object, one might find a use for a function that differentiated between upper case, lower case and numerals.

    Has the OP looked at the Like operator?

  13. #13
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Is it a VBA class that you want or a DLL?

    I have used Chip Pearson's method in vb.net to create a DLL and used it in both Excel and Workperfect using late binding but early binding can be used too. For details see: http://www.cpearson.com/excel/creati...nctionlib.aspx

    My vb.net DLL project files are at: http://www.box.com/s/65ekeevb6ol0qg7pld4n

    For other details on my DLL like Chip's and details about the file above, see: http://www.wpuniverse.com/vb/showthr...112-Create-DLL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •