"Bas Cost Budde" <b.costbudde@heuvelqop.nl> wrote in message
news:cqbaf4$4cg$1@news2.solcon.nl...[color=blue]
> WindAndWaves wrote:
>[color=green][color=darkred]
> >>I have written a new function that tests emails. Use it if you like,[/color][/color][/color]
but[color=blue][color=green][color=darkred]
> >>what I was wondering is if this is good coding. Anyone with any
> >>suggestions, please let me know.[/color][/color]
>
> I don't like GoTo. Not in a high-level language, at least. It makes the
> code logic hard to follow.
>
> I learned to structure my logic and program flow in diagrams
> (Nassi-Schneider type) where there is no representation for goto.
>
> I will put my interpretation below.
>[color=green][color=darkred]
> >>The other thing I wanted to know is if the function can return both a[/color][/color][/color]
true[color=blue][color=green][color=darkred]
> >>or a false value and a message (string) with the error description.[/color][/color]
>
> A function cannot return (=be evaluated as) two values. But you can have
> parameters that the function changes. In fact, if you don't specify
> otherwise, every parameter to a function can be changed inside the
> function. I usually denote this with a prefix VAR in the variable name.
>
> Public Function TestEmail(S As String, varResult As String) As Boolean
> 'checks if an email address is valid
> Dim i As Long
> Dim j As Long
> Dim AtP As Long ' position of at sign
> Dim D1p As Long ' position of first dot
> Dim L As Long ' L = length of string
> Dim CC As Byte ' character code, made byte specifically so that it gives
> 'an error if it is over or under its range
> Dim bRes As Boolean
> bRes = True 'assume valid until proven otherwise
> varResult = ""
> '--- check length
> L = Len(S)
> If L < 7 Then
> bRes = False 'smallest email
x@x.com = 7 characters
> varResult = varResult & "; too short"
> End If
> '---check @ sign
> AtP = InStr(1, S, "@", vbTextCompare)
> If AtP < 2 Or AtP > (L - 3) Then
> bRes = False
> varResult = varResult & "; portion before or after @ sign too[/color]
small"[color=blue]
> End If
> If InStr(AtP, S, "@", vbTextCompare) > 0 Then
> bRes = False
> varResult = varResult & "; more than one @ sign"
> End If
> '--- check dot
> D1p = InStr(AtP, S, ".", vbTextCompare)
> If D1p < 1 Then
> bRes = False
> varResult = varResult & "; missing dot"
> End If
> '--- check characters
> For i = 1 To L
> Select Case Asc(Mid(S, i, 1))
> Case 36, 37, 38, 43, 45, 46, 48 To 57, 64 To 90, 95, 97 To 122,
> 192 To 246, 249 To 255
> 'DO NOTHING
> Case Else
> bRes = False
> varResult = varResult & "; invalid character at position " & i
> End Select
> Next i
> xt:
> varResult = Mid(varResult, 3) 'cut off the first delimiter
> TestEmail = bRes
> Exit Function
> er:
> varResult = varResult & "; " & Err.Description
> Resume xt
> End Function[/color]
Thank your for your input Bas. The reason I used the goto was to make the
function as fast as possible. If, for example, it has less than 7
characters then there is no point in checking other stuff. The function is
meant to test several thousand email addresses so you want it to be fast.
Having said that, I saw some other nice ideas that I would like to use.
Thank you.
Groet uit NZ
Thiemen