PDA

View Full Version : how to make an "isletter" class in vba?



xena2305
01-21-2012, 03:03 AM
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.

Bob Phillips
01-21-2012, 05:33 AM
Why does it need to be a class, because your class assignment says so?

xena2305
01-21-2012, 06:22 AM
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.

Bob Phillips
01-21-2012, 09:01 AM
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.

xena2305
01-21-2012, 11:23 AM
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?

Bob Phillips
01-21-2012, 05:44 PM
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?

xena2305
01-22-2012, 09:38 AM
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-----

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

Paul_Hossler
01-22-2012, 10:08 AM
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


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



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

Bob Phillips
01-22-2012, 10:35 AM
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.

mikerickson
01-22-2012, 12:13 PM
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.

EGMono
01-23-2012, 06:05 PM
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.


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.

mikerickson
01-23-2012, 07:42 PM
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?

Kenneth Hobs
01-24-2012, 02:41 PM
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/creatingnetfunctionlib.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/showthread.php?33828-VB.NET-112-Create-DLL