Archived

This topic is now archived and is closed to further replies.

Motwner

quick VB question (Please Help)

Recommended Posts

Okay... I want to read in one five-digit Integer and determine how many digits in the Integer are 7''s. I was wondering if there is a VB function for this????

Share this post


Link to post
Share on other sites
Not really. But you can do this:

  
Public Function count7(Num as integer) as integer
Dim strNum as string, i as integer, cnt as integer
strNum = Str(Num)
For i = 1 to len(strNum)
If mid(strNum, i, 1) = "7" then _
cnt = cnt + 1
Next i
count7 = cnt
End Function

Share this post


Link to post
Share on other sites
Or even further, you can do this..

    
Public Function count7(num as integer) as integer
Dim strNum as string, i as integer, i2 as integer
strNum = str(num)
i = instr(strNum, "7")
While Not (i=0)
i2 = i
i = instr(i+1, strNum, "7")
Loop
count7 = i2
End Function


This will probably work faster.

Edited by - sporty on February 20, 2002 8:10:47 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
why not use do until instead of while not?

do until (i=0)

or even "while i" should work properly... since anything not = 0 SHOULD be true (I think VB handles this properly).

Also, i think Sporty messed up.. he put i2 = i, where he really meant i2 = i2 + 1


Billy - BillyB@mrsnj.com

I really should create a user name sometime

Share this post


Link to post
Share on other sites
Yes you''re right, I forgot that part

I only use While Not because I''m simply more used to reading it that way. Seems a bit clearer when I''m quickly browsing old code. Just a preference.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''m actually used to using while not also (as that''s what we get in C/C++ while loops), but in VB I prefer to use until as it makes it very easy to read.. do until x = 0... just as simple (if not simpler) to read as do while not (x=0)... but again, just personal preference.

Billy

Share this post


Link to post
Share on other sites