# Visual Basic 5.0 help.

#### GluonZ

Now, I know VB is hated in a lot of instances and 5.0 is out of date, however it is the only computer programming course offered here. Basically, I'm trying to do an assignment for fun at home - we never have homework (as everyone does not have VB5.0 at home, even though there is the free variant) and just do it in class. I would like to get this particular program done at home however, since I have done the other two on this assignment sheet and want to learn how to do this one since he told us when we get to #3 on the assignment sheet we would need to know something new. The topic of these assignment sheets are string manipulation and list boxes. The previous two projects on this assignment were basically simple arrays/select case programs with list boxes.

I'm not sure exactly how to approach this question as he said we need to know something new. I'm not sure what it is, but maybe someone here could help.

The question:

"#3. Write a program that allows the user to type in as many words as they want. Your program should display the same message that the user typed in with all the letter cases reversed. That is, all the letters that the person typed in upper case are now lower case and all the letters that the person typed in lower case are now in upper case.

Example:

Person types in:
Now Is The Time

And the computer repeats back in another location:
nOW iS tHE tIME"

Now, I'm not sure if I absolutely need this new thing he was talking about or not - but any help is appreciated.

Were not very far in the course, all we have done are forms (obviously), math operations, select case and if then statements, arrays, option buttons, and currently list boxes and string manipulation.

Related Programming and Computer Science News on Phys.org

#### -Job-

Well, i haven't used VB 5.0, but i've used VB 6 and VB.NET. Here's sample code using VBScript (VBScript is not VB), which should be similar. For example save this as a file of extension .vbs (in windows) and double click to run.
I'm looping through the characters in the string the user provides, compare them with their upper case version, and then change case as necessary.
I used InputBox and msgbox which are built in VBScript functions for displaying input and alert boxes respectively. It should give you an idea.
Code:
Dim str, newstr, char
str = InputBox("Please type something, anything.")
newstr = ""

for i=1 to Len(str)
char = Mid(str, i, 1)
if char = UCase(char) then
'char is uppercase
newstr = newstr + LCase(char)
else
'char is lowercase
newstr = newstr + UCase(char)
end if
next

msgbox(newstr)

#### GluonZ

Ah, that makes sense. Thanks man.

#### 1016

GluonZ, if you don't mind i wanna to borrow this thread to ask a question...
well, are they anybody know how to save .dat files in VB 6.0 c source?

#### Jheriko

GluonZ, if you don't mind i wanna to borrow this thread to ask a question...
well, are they anybody know how to save .dat files in VB 6.0 c source?
.dat file is just an extension and not a particular file type. .dat has been used in lots of games and software installers etc... Please specify an exact type of .dat file.

Also, what is VB6.0 c source? is that Visual Basic or C code?

In any case you need to look up file I/O functionality. Outputting to any file type is always the same, but you will have to arrange the data manually to fit the format you want to generate. This means that you will need to find out exactly how the .dat in question is used for storing data.

In C there is fopen, fread, fprintf and so on.

In VB there is Open, Close, Read and Write.

If you use the msdn library to look these up (http://msdn.microsoft.com) you will find a complete specification for each function/method as well as useful links to related functions. Some of the pages have examples too.

Generally you will want to use binary file access. Every other type is a subset of binary access anwway...

Good luck working it out...

#### GluonZ

Ok, I'm back with another question.

Question:

"Write a program that will allow the user to type in as many words as they want. Your program should then display how many of each letter in the alphabet was typed in. That means how many a's did the person type in, how many b's did the person type in, how many c's did the person type in, etc. The computer should count both upper case and lower case letters as the same. Display your answer in a list box. Your program must use at least one array."

Ok - a few things. First of all, I could already write this program using a bunch of kount's (for all the letters), but there must be some better way to do it, no? Also, maybe this is what I'm missing, but how would we fit an array into this? Whenever we had arrayed anything before we just copied a text box or something and used a simple loop. Arraying actual things on the form won't really be necessary here, so am I missing something in code?

Now, I know I could write this program already - however I feel that there must be a better, more efficient way. An explanation or example would be of great help and greatly appreciated.

#### Jheriko

What you want to do is find the ascii code for each character and use that to generate an index for an array. IIRC A is 65 and a is 97. So if a character is upper case and a letter we can subtract 64 from the ascii code and otherwise if it is still a letter we subtract 96, this generates a "1-based" array index from 1 to 26. If you need it to start at 0 just subtract 65 and 97 instead.

#### verty

Homework Helper
Gluonz, it doesn't sound like you know what an array is. Perhaps it's time to refresh your knowledge. Do that.

#### -Job-

A dictionary is actually a better fit to this problem than an array.

#### GluonZ

After reading Jheriko's post and some other help I did something like this

Code:
Dim i, iCount As Integer
Dim Text As String

For i = 65 To 90
Text = Text1
Text = UCase(Text1)
iCount = Len(Text) - Len(Char(i))
List1.AddItem Chr(i) & " - " & iCount
Next i
That didn't work and I was not sure why, and I was told that I need the Replace() function so it looked like this:

Code:
Dim i, iCount As Integer
Dim Text As String

For i = 65 To 90
Text = Text1
Text = UCase(Text1)
iCount = Len(Text) - Len(Replace(Text, Chr(i), ""))
List1.AddItem Chr(i) & " - " & iCount
Next i
Turns out that feature was implemented in VB6.0 and was not in VB5.0... any ideas how to do it without the replace function? (I would do 97 to 122 for lower case too).

Thanks for the help again.

Last edited:

#### -Job-

You can just use your own Replace function:
Code:
Function Replace(originalString, matchChar, replaceWith)
str = ""
for i=1 to Len(originalString)
char = Mid(originalString, i, 1)
if matchChar = char then
str = str & replaceWith
else
str = str & char
end if
next
Replace = str
End Function

#### Jheriko

After reading Jheriko's post and some other help I did something like this

Code:
Dim i, iCount As Integer
Dim Text As String

For i = 65 To 90
Text = Text1
Text = UCase(Text1)
iCount = Len(Text) - Len(Char(i))
List1.AddItem Chr(i) & " - " & iCount
Next i
That didn't work and I was not sure why, and I was told that I need the Replace() function so it looked like this:

Code:
Dim i, iCount As Integer
Dim Text As String

For i = 65 To 90
Text = Text1
Text = UCase(Text1)
iCount = Len(Text) - Len(Replace(Text, Chr(i), ""))
List1.AddItem Chr(i) & " - " & iCount
Next i
Turns out that feature was implemented in VB6.0 and was not in VB5.0... any ideas how to do it without the replace function? (I would do 97 to 122 for lower case too).

Thanks for the help again.
You have chosen a really elaborate way of doing this (IMO), its also slow (Len on a string runs until it finds a null in memory every single time for instance). Why not take the simpler approach and simply implement the algorithm using arrays? I am going to write a vbscript to illustrate the point... I will be back!

#### Jheriko

It took me a while, I had to wrestle with VBscript a little. I rarely ever use it and it is different enough from VB 3/4/5/6 to give me a headache. Also my VB in general is rusty...

Anyway

Code:
Dim counts(26)
Dim i
Dim l
Dim retval
Dim s
Dim out

For i = 0 to 25
counts(i) = 0
Next

retval = InputBox("Input text:","Testy","Some text")

l = Len(retval)
retval = lcase(retval)

For i = 1 to l
s = Asc(Mid(retval,i,1)) 'get ascii value of character
if (s>96) and (s<123) then
counts(s - 97) = counts(s - 97) + 1
end if
Next

out = ""

For i = 0 to 25
out = out & Chr(i+97) & " " & counts(i) & " "
Next

MsgBox out
This works as a vbscript, converting to vb6 is trivial, just at the types to the Dims and add some "next i" instead of just "next".

More importantly though notice how the only annoying function inside the loop is mid.

[rant]Writing this reminded me of exactly all the reasons I hate VB.. Chr, Asc, Mid... all of these are annoying features that you don't have or need in a lower level language. Direct access to bytes makes writing this sort of thing a lot cleaner....[/rant]