From: GS on
mp presented the following explanation :
> "GS" <gesansom(a)netscape.net> wrote in message
> news:hvlqnp$64l$1(a)news.eternal-september.org...
>> mp used his keyboard to write :
>>> "Larry Serflaten" <serflaten(a)gmail.com> wrote in message
>>> news:hvlijo$9n9$1(a)news.eternal-september.org...
>>>>
>>>> "mp" <nospam(a)Thanks.com> wrote
>>>>
>>>>> so FormSettings.ReadFile would just read a text file, parse the lines
>>>>> with
>>>>> split
>>>>> and fill Data(n) accordingly?
>>>>> that means i also have to cast strings to other datatypes if
>>>>> req'd(longs,doubles,etc)
>>>>> lots more code than one call to a get/put function :-)
>>>>
>>>> No extra typecasting code, Print and Line Input work with Variants.
>>>> Your form sets the value, and Print sends it to the disk. Line Input
>>>> reads it back from the disk and sets the value, just like your form does.
>>>
>>> from the help on Line Input
>>> Reads a single line from an open sequential file and assigns it to a
>>> string variable
>>>
>>> maybe that's also why i always thought of textfiles as containing strings
>>>
>>> is that line in the help incorrect?
>>
>> The line is correct. A text file contains only text unless the content is
>> binary. AFAIK, all text is String type. That means if you're storing
>> numbers or boolean values, for example, then you'll have to convert them
>> accordingly.
>>
>> -- Garry
>>
>> Free usenet access at http://www.eternal-september.org
>> ClassicVB Users Regroup! comp.lang.basic.visual.misc
>>
>
> thats' what I had thought but Larry said I don't have to cast so I'm
> confused.
> mark

I don't know what you mean by 'don't have to cast'! Larry has described
another way to do what you want via a class module, where you can reuse
it for any project. I have described one way to handle UDTs that you
use for every project, AND a way to handle project-specific UDTs as
required by each project. Larry's suggestion requires a bit more coding
but, as he states, you only need to do it once and just import the
class into your projects to use it.

What you need to decide is which of the two approaches works best for
you. Also, you need to determine whether you want to Put/Get your data
or use other means to save/load your UDT values.

I suspect a demo sample from Larry might be helpful, as I get the
feeling you're treading in unkown territory here. I have never done it
as Larry describes and so I have nothing to offer by way of a sample.
-Sorry!

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


From: Larry Serflaten on

"mp" <nospam(a)Thanks.com> wrote

> thats' what I had thought but Larry said I don't have to cast so I'm
> confused.

What is so confusing? A variant is a special data type, see help:

"You can use the Variant data type in place of any data type to work with data
in a more flexible way. If the contents of a Variant variable are digits, they
may be either the string representation of the digits or their actual value,
depending on the context."

Your program can use Strings, or Longs, or Doubles (etc.) but the class
stores them in an array of Variants. They go to the disk as strings, and
come back as strings. But your program can still use them as the original
data type.

Try it and see....

LFS


From: mp on

"Larry Serflaten" <serflaten(a)gmail.com> wrote in message
news:hvlijo$9n9$1(a)news.eternal-september.org...
>
> "mp" <nospam(a)Thanks.com> wrote
>
>> so FormSettings.ReadFile would just read a text file, parse the lines
>> with
>> split
>> and fill Data(n) accordingly?
>> that means i also have to cast strings to other datatypes if
>> req'd(longs,doubles,etc)
>> lots more code than one call to a get/put function :-)
>
> No extra typecasting code, Print and Line Input work with Variants.
> Your form sets the value, and Print sends it to the disk. Line Input
> reads it back from the disk and sets the value, just like your form does.
> Once you get the code working, you don't need to touch it again,
> so what if it takes half a dozen more lines???
>

ok this is what i'm trying in class cFormSettings
i think my loop is stupid
since i'm redimming at end of loop i have to remove the last empty slot
is that normal pattern?
or should I increment index at top of loop and add to index-1 item?

Sub ReadFile()
Dim lFhndl As Integer
lFhndl = FreeFile
Dim Index As Long
If FileExists(msFilename) Then
Open msFilename For Input As #lFhndl ' Open file.
Do While Not EOF(lFhndl) ' Loop until end of file.

Line Input #lFhndl, Data(Index) ' Read line into variable.
LogEntry "Data", Data(Index) Index = Index + 1
ReDim Preserve Data(Index)
Loop
ReDim Preserve Data(Index - 1) 'remove last empty slot

End If
End Sub


From: Larry Serflaten on

"GS" <gesansom(a)netscape.net> wrote

> The line is correct. A text file contains only text unless the content
> is binary. AFAIK, all text is String type. That means if you're storing
> numbers or boolean values, for example, then you'll have to convert
> them accordingly.

The program has settings of different types of variables, and it uses them
as those data types. The class stores the settings in an array of Variants.
The variant will coerse the values to strings when they go out to the disk
and will accept the values as strings when they are read back in. But
the program can still use them as the original data type.

Settings might be any scalar data type (eg. not objects or images, etc.)
and in order for the class to store it, the class has to have a variable
that can handle the data type. The Variant is VB's generic data type
that handle any of the other data types, so an array of variants can
be used to store all the settings.

In other words, the class puts Evil Type Conversion to a good use.

The only down side is that those are to be treated as settings, and not
as variables. The program should only assign a value or retrieve a value
from the class, and not use the class values in any math or other such
combinations. To avoid the evil type conversion situations the program
should put the value where it belongs (in a program variable of the proper
data type) and perform the math or other combinations on it there.


LFS


From: Larry Serflaten on

"mp" <nospam(a)Thanks.com> wrote

> ok this is what i'm trying in class cFormSettings
> i think my loop is stupid
> since i'm redimming at end of loop i have to remove the last empty slot
> is that normal pattern?
> or should I increment index at top of loop and add to index-1 item?

There is no reason you can't get back to that one line access method,
if you like it that much! <g> Paste the following code in a new form
and step through it to see that it will work as intended:

LFS

Private Sub Form_Load()

Const TestFile = "D:\Temp\Test.dat"
' note change to variant instead of array of variants
Dim data As Variant

' Store values
ReDim data(0 To 3)
data(0) = "one"
data(1) = 10
data(2) = True
data(3) = 0.5

' Save to file
Open TestFile For Binary As 1
Put #1, , data
Close

' simulate program restart
Erase data 'variable is now empty

' Read data from file
Open TestFile For Binary As 1
Get #1, , data
Close

' Values back in array, ready for use
Debug.Print "Data indexed"; LBound(data); "-"; UBound(data)
Debug.Print data(0)
Debug.Print data(1)
Debug.Print data(2)
Debug.Print data(3)

End Sub