Computing.Net > Forums > Programming > VB6 Question

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

VB6 Question

Reply to Message Icon

Name: rvrock
Date: May 16, 2004 at 16:20:27 Pacific
OS: xp
CPU/Ram: 1 gig
Comment:

New to vb6. Struggling on a small program to determine how many 3s there is when a user inputs in a five-digit number. I'm suppose to use a loop for this. Can't figure out how to compare each digit separtely.

Any help/direction would be greatly appreciated.
Thanks.



Sponsored Link
Ads by Google

Response Number 1
Name: StuartS
Date: May 16, 2004 at 16:42:20 Pacific
Reply:

Convert the number to a string. Then loop through the string using Mid$ counting each digit as a character.

Dim a As String
Dim b As Integer

a = Str(12345)

For b = 1 To Len(a)

If Mid$(a, b, 1) = "3" Then
' its a 3
End If

Next

The first character in the string will always be a space for a positive number or - for a negative number.

Stuart



0

Response Number 2
Name: wizard-fred
Date: May 16, 2004 at 16:47:43 Pacific
Reply:

num$ = str$(number)
found% = 0
for c% = 1 to 5
if (mid$(num$, c%, 1) = "3") then
found% = found% + 1
end if
next c%


0

Response Number 3
Name: StuartS
Date: May 16, 2004 at 17:31:50 Pacific
Reply:

Wizard-Fred,

A little VB tip for you.

Although you have declared the variables with type characters like $ and % and it works, the ability to do so is only there for backward compatibility with earlier versions of VB.

It is good practise to declare your variables with Dim statements, in fact its a good idea to force variable declaration in the Tools > Options menu of VB. Forcing variable declaration helps with debugging with more complex programmes.

With type character it is to easy to miss type a variable and produce all sort of strange bugs without throwing up errors messages.

Or to have number defined as a variant because you forgot to insert the integer type character. A variant is 64 bits while an integer is 16 bits.

Stuart


0

Response Number 4
Name: wizard-fred
Date: May 17, 2004 at 08:17:52 Pacific
Reply:

Bob, Stuart

First a slight correction
line 3 change - for c% = 1 to len(num$)

As Stuart points out correctly conversion from number to string has a position for an implied sign.
The reason I didn't catch it is that I normally input everything as strings and convert during or after validation.

I did not intend for my example to be the final code, only a skeleton. Stuart you are right in that variables should be declared up front. Actually I do very little coding in VB. I have progressed through various BASIC's; HP3000Basic, AlphaBASIC, Commodore BASIC, MBasic, CB80, CB86, BASICA(GWBASIC), MS BusinessBasicCompiler, MS QuickBasicCompilers, Summit Basic, True Basic, Stamp Basic, AVRBasic, Casio Basic, Radio Shack Basic, and I have tried almost every downloadable variant. I presently program mostly in PowerBasic and various dBase3 variants. I have VB6, VC++, VFoxPro as well as Borland C, COBOL, Fortran, Java, Pascal, LOGO, & Assembler. So I know a little about much, and more about BASIC. My other interest is client/server databases, MySQL, DB2, PowerBuilder, PostgreSQL, MaxDB/SAP. Mostly open source, or free.



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


where can i buy old versi... Need help writting BASIC ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: VB6 Question

easy VB6 questions www.computing.net/answers/programming/easy-vb6-questions/5807.html

Simple VB6 question www.computing.net/answers/programming/simple-vb6-question/14752.html

VB6 Question www.computing.net/answers/programming/vb6-question/7840.html