-
Nov 2nd, 2005, 10:00 AM
#1
Thread Starter
Frenzied Member
String to Type
I have a question,
Suppose I have a type:
VB Code:
private type tpPerson
sdFirstName as string * 25
sdLastName as string * 25
end type
And I have a file with many records:
Bill Gates
vrgrwe breipgre
Is there a simple way to load each record into the type. I know I can do :
VB Code:
dim Person as tpPerson
dim MyString as string
while not eof(1)
readline #1,MyString
Person.FirstName = left(Mysting,25)
Person.LastName = left(Mysting,25)
wend
But the type is more complex. In C this can be done with pointers, but in VB. Any idea
-
Nov 2nd, 2005, 10:09 AM
#2
Re: String to Type
You'd need to use Mid$() instead of left and right, (use mid$ for all the fields and the code will look more sensible anyway.
This also depends what data types you are importing.
For numeric types you'll need to specify the right cast...
person.ID = cint(mid$(mystring, 26, 4))
for example.
I don't live here any more.
-
Nov 2nd, 2005, 10:18 AM
#3
Re: String to Type
if its ALWAYS just first & last name (No middle initial) use split
VB Code:
Dim Lines() as string
Dim tmp() as string
Open "FIlename" for input as #1
Lines = split(Input(lof(1),1),vbcrlf)
CLose #1
for x = 0 to ubound(tmp)
tmp = split(Lines(x)," ")
Person.FirstName = tmp(0)
Person.LastName = tmp(1)
Next
something like that, but Person will need to be an array
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Nov 2nd, 2005, 10:27 AM
#4
Re: String to Type
It was specified that it was NOT just names
I don't live here any more.
-
Nov 2nd, 2005, 10:36 AM
#5
Thread Starter
Frenzied Member
Re: String to Type
I know I can use mid and left/right but I thought there something better, like using memcopy api or so, to do it in 1 step.
-
Nov 2nd, 2005, 10:37 AM
#6
Re: String to Type
Where was that specified??
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Nov 4th, 2005, 06:08 AM
#7
Re: String to Type
You can't do a direct copy, because your fields are of variable length - and the arithmetic plus DLL call overhead will make it slower than using Mid$ anyway.
-
Nov 4th, 2005, 07:37 AM
#8
Thread Starter
Frenzied Member
Re: String to Type
I've done it using the MID$ sollution (thought of it myself, I just wanted to know if it could be done in 1 statement). And because the fields are FIXED LENGHT I thought it could be done. But thanks for the effort
-
Nov 4th, 2005, 07:55 AM
#9
Re: String to Type
Well as long as your source data is padded so that it is in fact FIXED LENGHT, you can use:
That also requires that you omit the line break between records.
So your data needs to look like this:
Code:
Bill Gates vrgrwe breipgre
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|