quick VB question (Please Help)

Started by
4 comments, last by Motwner 22 years, 2 months ago
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????
Advertisement
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 = cntEnd Function  
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 = i2End Function    


This will probably work faster.

Edited by - sporty on February 20, 2002 8:10:47 PM
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
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.
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

This topic is closed to new replies.

Advertisement