Home > Archive > Visual Basic Syntax > March 2006 > vb functions
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Hemant_india 2006-02-21, 7:03 pm |
| hi everybody
is there any function in vb lke between()
e.g if between(a1, a2,a3) then
some code
end if
a1=expression to be searched between a2 and a3
--
hemu
| |
| Al Reid 2006-02-21, 7:03 pm |
| "Hemant_india" <Hemantindia@discussions.microsoft.com> wrote in message news:CDBD2657-E592-43BB-B9EB-7D2B8900DE50@microsoft.com...
> hi everybody
> is there any function in vb lke between()
> e.g if between(a1, a2,a3) then
> some code
> end if
> a1=expression to be searched between a2 and a3
> --
> hemu
There is no native "Between" function in VB. You can write one.
Public Function Between(ByVal Find, ByVal First, ByVal Last) As Boolean
If Find >= First And Find <= Last Then
Between = True
End If
End Function
Used as follows:
Dim i as Integer
i = 5
If Between(i, 0, 10) Then
' Do whatever..
End If
--
Al Reid
| |
| Rick Rothstein [MVP - Visual Basic] 2006-02-21, 7:04 pm |
| > is there any function in vb lke between()
> e.g if between(a1, a2,a3) then
> some code
> end if
> a1=expression to be searched between a2 and a3
A dedicated function? No, although you could write your own...
Function Between(Value, Lower, Upper) As Boolean
Between = (Lower <= Value And Value <= Upper)
End Function
I've left Value, Lower and Upper as Variants so that you can freely mix
numeric data types, so you might want to add some error checking routines to
the code.
Rick
| |
| Al Reid 2006-02-21, 7:04 pm |
| "Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
news:uXvl9$vNGHA.668@TK2MSFTNGP11.phx.gbl...
>
> A dedicated function? No, although you could write your own...
>
> Function Between(Value, Lower, Upper) As Boolean
> Between = (Lower <= Value And Value <= Upper)
> End Function
>
> I've left Value, Lower and Upper as Variants so that you can freely mix
> numeric data types, so you might want to add some error checking routines to
> the code.
>
> Rick
>
>
Rick,
Same approach as the one I posted but yours is shorter and doesn't use that pesky If..Then thing<g>.
--
Al Reid
| |
| argusy 2006-02-22, 3:57 am |
| It wouldn't be Rick if it didn't contain a one-liner!!
This one was easy, but Rick's come up with some doozies at times
Argusy
Al Reid wrote:
> "Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
> news:uXvl9$vNGHA.668@TK2MSFTNGP11.phx.gbl...
>
>
>
> Rick,
>
> Same approach as the one I posted but yours is shorter and doesn't use that pesky If..Then thing<g>.
>
> --
> Al Reid
>
>
| |
| Al Reid 2006-02-22, 7:59 am |
| "argusy" <argusy@slmember.on.net> wrote in message news:43FC345A.1070307@slmember.on.net...
> It wouldn't be Rick if it didn't contain a one-liner!!
> This one was easy, but Rick's come up with some doozies at times
>
> Argusy
>
>
Yes, having been here since late 1999, I am quite aware of Rick's penchant for one-liners.
--
Al Reid
| |
| Rick Rothstein [MVP - Visual Basic] 2006-02-22, 7:59 am |
| > Yes, having been here since late 1999, I am
> quite aware of Rick's penchant for one-liners.
A penchant for one-liners? Who? Me? Maybe I've posted one or two of them...
hardly enough to say I have a penchant for them. Really now...<vbg>
Rick
| |
| Rick Rothstein [MVP - Visual Basic] 2006-02-22, 7:59 am |
| > It wouldn't be Rick if it didn't contain a one-liner!!
> This one was easy, but Rick's come up with
> some doozies at times
Doozies? Why I think I resent that... everyone of my one-liners have been
clear, concise, easy to read and obvious as to how they work without
requiring any explanations. Hardly what one would call "doozies" now,
really.<vbg>
Rick
| |
| argusy 2006-02-23, 7:58 am |
| You're lovin'' this, aren't you, Rick!!
Argusy
Rick Rothstein [MVP - Visual Basic] wrote:
>
>
> Doozies? Why I think I resent that... everyone of my one-liners have been
> clear, concise, easy to read and obvious as to how they work without
> requiring any explanations. Hardly what one would call "doozies" now,
> really.<vbg>
>
> Rick
>
>
| |
| Rick Rothstein [MVP - Visual Basic] 2006-02-23, 7:04 pm |
| > You're lovin'' this, aren't you, Rick!!
LOL .... Yep!!!
Rick
| |
| Hemant_india 2006-02-23, 7:04 pm |
| searchstring should compare every value falling into the ramge of exp1 and exp2
--
hemu
"Al Reid" wrote:
> "Hemant_india" <Hemantindia@discussions.microsoft.com> wrote in message news:CDBD2657-E592-43BB-B9EB-7D2B8900DE50@microsoft.com...
>
> There is no native "Between" function in VB. You can write one.
>
> Public Function Between(ByVal Find, ByVal First, ByVal Last) As Boolean
>
> If Find >= First And Find <= Last Then
>
> Between = True
>
> End If
>
> End Function
>
>
>
> Used as follows:
>
>
>
> Dim i as Integer
>
> i = 5
>
> If Between(i, 0, 10) Then
>
> ' Do whatever..
>
> End If
>
>
>
> --
>
> Al Reid
>
>
>
| |
| Al Reid 2006-02-23, 7:04 pm |
| Ok. Is that a comment, a suggestion, agreement or is it not doing what you wanted? Are you suggesting that searchstring is an
array or a collection?
--
Al Reid
"Hemant_india" <Hemantindia@discussions.microsoft.com> wrote in message news:95EF4500-2E7B-46C1-A49E-09C87AECD6B8@microsoft.com...[color=darkred]
> searchstring should compare every value falling into the ramge of exp1 and exp2
> --
> hemu
>
>
> "Al Reid" wrote:
>
news:CDBD2657-E592-43BB-B9EB-7D2B8900DE50@microsoft.com...[color=darkred]
| |
| Hemant_india 2006-02-28, 4:01 am |
| i'm trying to do following
please suggest otherwise
Public Function ac_clean(rng As String)
Dim lstring As String
Dim fstring As String
Dim n As Long
mylen = Len(rng)
Debug.Print mylen
For n = 1 To mylen
lstring = Mid(rng, n, 1)
If Asc(lstring) = 48 Then
fstring = fstring & lstring
Else
fstring = fstring
End If
If Asc(lstring) = 49 Then
fstring = fstring & lstring
Else
fstring = fstring
End If
If Asc(lstring) = 50 Then
fstring = fstring & lstring
Else
fstring = fstring
End If
If Asc(lstring) = 51 Then
fstring = fstring & lstring
Else
fstring = fstring
End If
If Asc(lstring) = 52 Then
fstring = fstring & lstring
Else
fstring = fstring
End If
If Asc(lstring) = 53 Then
fstring = fstring & lstring
Else
fstring = fstring
End If
If Asc(lstring) = 54 Then
fstring = fstring & lstring
Else
fstring = fstring
End If
If Asc(lstring) = 55 Then
fstring = fstring & lstring
Else
fstring = fstring
End If
If Asc(lstring) = 56 Then
fstring = fstring & lstring
Else
fstring = fstring
End If
If Asc(lstring) = 57 Then
fstring = fstring & lstring
Else
fstring = fstring
End If
If Asc(lstring) = 58 Then
fstring = fstring & lstring
Else
fstring = fstring
End If
If Asc(lstring) = 59 Then
fstring = fstring & lstring
Else
fstring = fstring
End If
Next n
ac_clean = fstring
End Function
--
hemu
"Al Reid" wrote:
> Ok. Is that a comment, a suggestion, agreement or is it not doing what you wanted? Are you suggesting that searchstring is an
> array or a collection?
>
> --
> Al Reid
>
>
> "Hemant_india" <Hemantindia@discussions.microsoft.com> wrote in message news:95EF4500-2E7B-46C1-A49E-09C87AECD6B8@microsoft.com...
> news:CDBD2657-E592-43BB-B9EB-7D2B8900DE50@microsoft.com...
>
>
>
| |
| Rick Rothstein [MVP - Visual Basic] 2006-02-28, 4:01 am |
| A couple of comments first. One, when your If-Then test fails, you are
performing this operation...
fstring = fstring
Why? Nothing has changed and you are not changing anything, so fstring is
already equal to itself... the assignment is doing nothing (and may be
wasting time if the compiler is not "smart" enough to determine the
statement is useless). Two, this assignment
mylen = Len(rng)
is done only once and the mylen variable is used only once afterwards in
this statement
For n = 1 To mylen
(I'm ignoring the Debug.Print statement as it won't appear in your final
code). Since VB only evaluates the For loop expressions once, just before
the loop is executed, there is no need to use an intermediate variable; just
do this
For n = 1 To Len(rng)
Now for some replacement code. You can perform range testing in If-Then
test. All of your individual If-Then-Else blocks can be replaced, in their
entirety, with this
If Asc(lstring) >= 48 And Asc(lstring) <= 59 Then
fstring = fstring & lstring
End If
Here is your code, revise as described above...
Public Function ac_clean(rng As String)
Dim lstring As String
Dim n As Long
For n = 1 To mylen
lstring = Mid(rng, n, 1)
If Asc(lstring) >= 48 And Asc(lstring) <= 59 Then
fstring = fstring & lstring
End If
Next
ac_clean = fstring
End Function
That is it. Well, actually, one more thing. You don't have to convert the
characters assigned to lstring to their ASCII values in order to do your
comparisons... you can test strings directly.
Public Function ac_clean(rng As String)
Dim lstring As String
Dim n As Long
For n = 1 To mylen
lstring = Mid(rng, n, 1)
If lstring >= "0" And lstring <= ";" Then
fstring = fstring & lstring
End If
Next
ac_clean = fstring
End Function
I'm kind of curious about your including the colon (ASCII 58) and semi-colon
(ASCII 59) in your parsing test. Did you perhaps mean to stop your testing
at ASCII 57 (the character "9")? If so, just replace the ";" above with "9".
Rick
[color=darkred]
> i'm trying to do following
> please suggest otherwise
> Public Function ac_clean(rng As String)
> Dim lstring As String
> Dim fstring As String
> Dim n As Long
>
> mylen = Len(rng)
> Debug.Print mylen
> For n = 1 To mylen
> lstring = Mid(rng, n, 1)
> If Asc(lstring) = 48 Then
> fstring = fstring & lstring
> Else
> fstring = fstring
> End If
>
> If Asc(lstring) = 49 Then
> fstring = fstring & lstring
> Else
> fstring = fstring
> End If
>
> If Asc(lstring) = 50 Then
> fstring = fstring & lstring
> Else
> fstring = fstring
> End If
> If Asc(lstring) = 51 Then
> fstring = fstring & lstring
> Else
> fstring = fstring
> End If
> If Asc(lstring) = 52 Then
> fstring = fstring & lstring
> Else
> fstring = fstring
> End If
> If Asc(lstring) = 53 Then
> fstring = fstring & lstring
> Else
> fstring = fstring
> End If
> If Asc(lstring) = 54 Then
> fstring = fstring & lstring
> Else
> fstring = fstring
> End If
> If Asc(lstring) = 55 Then
> fstring = fstring & lstring
> Else
> fstring = fstring
> End If
> If Asc(lstring) = 56 Then
> fstring = fstring & lstring
> Else
> fstring = fstring
> End If
> If Asc(lstring) = 57 Then
> fstring = fstring & lstring
> Else
> fstring = fstring
> End If
> If Asc(lstring) = 58 Then
> fstring = fstring & lstring
> Else
> fstring = fstring
> End If
> If Asc(lstring) = 59 Then
> fstring = fstring & lstring
> Else
> fstring = fstring
> End If
>
> Next n
> ac_clean = fstring
>
> End Function
> --
> hemu
>
>
> "Al Reid" wrote:
>
you wanted? Are you suggesting that searchstring is an[color=darkred]
news:95EF4500-2E7B-46C1-A49E-09C87AECD6B8@microsoft.com...[color=darkred]
and exp2[color=darkred]
message[color=darkred]
Boolean[color=darkred]
| |
| Hemant_india 2006-02-28, 7:58 am |
| thanks all of you
it is a great help
thanks again
--
hemu
"Rick Rothstein [MVP - Visual Basic]" wrote:
>
> A dedicated function? No, although you could write your own...
>
> Function Between(Value, Lower, Upper) As Boolean
> Between = (Lower <= Value And Value <= Upper)
> End Function
>
> I've left Value, Lower and Upper as Variants so that you can freely mix
> numeric data types, so you might want to add some error checking routines to
> the code.
>
> Rick
>
>
>
| |
| Bob Butler 2006-03-03, 7:58 am |
| "Hemant_india" <Hemantindia@discussions.microsoft.com> wrote in message
news:6C62A4CB-225E-4403-81F7-888B463A8426@microsoft.com
> hi guys
> another problem
> once declared, lower limits of the tow dimensional arrays cannot be
> ReDimed i do not know how many records i want to collect in an array
> if i declare say myarray(1000,2,3)
> in that case how to remove empty elements?
Reverse the order and use myarray(3,2,1000)
--
Reply to the group so all can participate
VB.Net: "Fool me once..."
| |
| Clinton 2006-03-24, 4:15 am |
|
A dedicated function? Maybe not. Just a multi-use statment:
Select Case aNumber
Case aRangeStart To aRangeEnd:MsgBox"In Range"
End Select
"Rick Rothstein [MVP - Visual Basic]" wrote:
>
> A dedicated function? No, although you could write your own...
>
> Function Between(Value, Lower, Upper) As Boolean
> Between = (Lower <= Value And Value <= Upper)
> End Function
>
> I've left Value, Lower and Upper as Variants so that you can freely mix
> numeric data types, so you might want to add some error checking routines to
> the code.
>
> Rick
>
>
>
| |
| Clinton 2006-03-24, 4:15 am |
| > A dedicated function?
Multi-functional statment:
Select Case aNumber
Case aRangeStart To aRangeEnd
MsgBox"In Range"
Case Else
MsgBox"Out of Range"
End Select
Also, if "aNumber" is stored in a string, then convert it to numeric in
order to avoid a non-numeric comparison
Select Case CDec(aNumber)
|
|
|
|
|