vb question (1 Viewer)

sunny

meh.
Joined
Jul 7, 2002
Messages
5,350
Gender
Male
HSC
2002
No. Integers range from -32768 to 32767.

You can set the length of a string because it is considered to be an array of chars, thus by limiting the length of the array you limit the number of characters in the string.
 

velox

Retired
Joined
Mar 19, 2004
Messages
5,521
Location
Where the citi never sleeps.
Gender
Male
HSC
N/A
Ah ic, no point then. One more question:

Say if everytime i open the program, i add a value and it stores it in the text file. How can i get it to average that number, if im adding a new number...say each week? How would i reference to them in the text file?
 

sunny

meh.
Joined
Jul 7, 2002
Messages
5,350
Gender
Male
HSC
2002
Assuming you add to the file line by line, with one number in each line, then you can use the Input function to read the file one line at a time. As you read it, you would use CInt to convert Input's string result into a number, which then you can use for arithmetic.
 

sunny

meh.
Joined
Jul 7, 2002
Messages
5,350
Gender
Male
HSC
2002
This should be enough to get you started.

Code:
Option Explicit             ' force variable declaration

Private Sub Form_Load()

    Dim x As Integer
    Dim str As String
    Dim num As Integer
    
    ' open file to read
    Open "C:\test.txt" For Input As #1
    
    x = 1
    ' read until end of file, for file 1
    Do Until EOF(1)
        Input #1, str       ' read one line from file #1
        num = CInt(str)     ' convert line read into a number (so you can do arithmetic on it)
        MsgBox "read the number: " + str + " on line: " + CStr(x)
        x = x + 1
    Loop

    ' finished, close file
    Close #1
End Sub
 

velox

Retired
Joined
Mar 19, 2004
Messages
5,521
Location
Where the citi never sleeps.
Gender
Male
HSC
N/A
What does this mean:


"MsgBox "read the number: " + str + " on line: " + CStr(x)"

Like what is "str."? So X is the line number? If so, how do we know that?

Arghh, sorry for all the questions...Im just trying to get this concept around my head, its all new.
 

sunny

meh.
Joined
Jul 7, 2002
Messages
5,350
Gender
Male
HSC
2002
velox said:
"MsgBox "read the number: " + str + " on line: " + CStr(x)"
If you run the code, you will find that it displays in the message box what it read on the line. So if the file had contents:
9
10
11
It should show 3 messages:
"read the number: 9 on line: 1"
"read the number: 10 on line: 2"
"read the number: 11 on line: 3"

velox said:
Like what is "str."?
Code:
Input #1, str       ' read one line from file #1
This line reads from file #1 (which is opened to be C:\test.txt), and places one line from the file into the string str.

velox said:
So X is the line number? If so, how do we know that?
Because it is declared an integer, and it is incremented (plus 1) each time in the loop runs, and since each time the loop runs, it reads one line, the variable x will keep track on which line we are currently on.
 
Last edited:

Ghost1788

Member
Joined
Jan 30, 2005
Messages
276
Location
Sydney
Gender
Male
HSC
2005
velox said:
u know how u can do this "Name As String * 40" which means the string can be only 40 chars long. Can u do the same for a integer?
U know for th input of the integer u could set the limit of the text box to the number of characters you what. and to make sure you only get numbers u could use VAL which gets rid of all characters that arent numbers that is if you integer is to be int and ur text box is to be Text1 then in VB6.0

int=VAL(text1.text)

this can be run via a commmand button or through a little more ingenuity you could put it in the text1_change procedure
Note : You will need to add a little more code to this...i think if you use the text1_change procedure
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top