Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting the string length 'Dim strInput as string * 2000 1

Status
Not open for further replies.

moki

Programmer
Dec 5, 2000
37
0
0
Hello,

Is there a way to dimension a string length using a variable.

Ex:

Dim strInput as String * 2000

>>Would like to get the same result as above using:

Dim strInput as String * intLength

>>I know VB6 requires a constant but is there a way to set the length of 'strInput' using a variable some other way.

Thank you....
 
Why? Why would you need to have the length of a string set at any specific value?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Hmmm....so would a string of spaces x characters long suffice?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Thanks to Strongm's propmting, I found this informative page via google:

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I am performing the following to replace nulls to spaces in a text file. If I use 'Line Input #1, strInput' the nulls are stripped and the fixed record size is reduced by the number of nulls. I am parsing the text file records, code not shown.
===============================================
Dim strInput As String * 2000
Dim intLength As Integer
Dim lngRec_Loc As Long

intLength = Command()

Open "D:\data\xyz_file.txt" For Random As #1 Len = intLength
Open "d:\data\xyz_99.txt" For Random As #2 Len = intLength

Do Until EOF(1)
lngRec_Loc = lngRec_Loc + 1
Get #1, lngRec_Loc, strInput
strInput = Replace(strInput, Chr(0), "?")
Put #2, lngRec_Loc, strInput
Loop
Close #1, #2
Unload Me
========================================================

I would like to do the following:

intLength = command()
Dim strInput as String * intLength

I know VB6 will not permit this - is there another way to set the string size?

Thanks for your help.
 
Sure, but that's just another way of specifying the length of a string, which is what you were questioning the need for. Another alternative would be a dynamic array of bytes.
 
Yes, am array like:

Dim strInput() As String ' watch out the scope of this variable
ReDim strInput (intLenght) As String


-Expanding strondm's suggestion.
 
Er...
Code:
Dim strInput() as Byte
ReDim strInput(intLength)
Is more in line with strongm's suggestion. He mentions an array of bytes, not of strings.
 
TomThumbKP, you might also be interested in this thread: thread222-558844
 
Yes. I used his idea of the "dynamic array",.. not also the type of the array!
:)
 
Thank you for the information but it doesn't work with the 'Get #1, lngRec_Loc, strInput' because strInput is now an Array.

The reason I'm doing this is to retain the nulls in the record coming from file #1. I want a 2000 (not always, this will be a general purpose pgm to read diff. fixed length record sizes) chunk from the fixed length records in file #1.

If I Open the file with 'For Input' the nulls drop after reading it into the strInput string causing a problem when I parse the data.

ex: Data
ABCDEFGHI.JKLMNOP.QRSTU (Fixed length record for illustration '.' = null)
ABCDEFGHIJKLMNOPQRSTU (Open method = 'For Input' drops the nulls)
ABCDEFGHI.JKLMNOP.QRSTU (Open method = 'For Random' retains the nulls)

Using 'As Random' is the only way I know of that will retain the nulls so I can replace them with spaces.

Dim strInput() As Byte '*****
Dim intLength As Integer
Dim lngRec_Loc As Long

intLength = 2000
ReDim strInput(intLength) '*****

Open "D:\xyz_file.txt" For Random As #1 Len = intLength
Open "c:\xyz99_file.txt" For Random As #2 Len = intLength

Do Until EOF(1)
lngRec_Loc = lngRec_Loc + 1
Get #1, lngRec_Loc, strInput ' ******** Out-of-memory error
strInput = Replace(strInput, Chr(0), " ")
Put #2, lngRec_Loc, strInput
Loop
Close #1, #2
Unload Me

Sorry for being so long winded and thank you for all of your responses...I hope I'm making my self clear regarding this request.

 
Have you tried using the filesystem object and the Read method? Make sure to add a reference to Microsoft Scripting Runtime.

Dim f1 As TextStream
Dim f2 As TextStream
Dim fso As New FileSystemObject
Dim strInput As String
Dim intLength As Integer

Set f1 = fso_OpenTextFile(InputFileName, ForReading)
Set f2 = fso_OpenTextFile(outputfilename, ForWriting, True)
intLength = 2000
Do
strInput = f1.read(intLength)
strInput = Replace(strInput, Chr(0), " ")
f2.Write strInput
Loop Until f1.atendofstream

Swi
 
Thank you Swi that did the trick. Learning something new everyday..

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top