Results 1 to 9 of 9

Thread: String to Type

  1. #1

    Thread Starter
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    String to Type

    I have a question,

    Suppose I have a type:

    VB Code:
    1. private type tpPerson
    2.     sdFirstName as string * 25
    3.     sdLastName as string * 25
    4. 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:
    1. dim Person as tpPerson
    2. dim MyString as string
    3.  
    4. while not eof(1)
    5.     readline #1,MyString
    6.     Person.FirstName = left(Mysting,25)
    7.     Person.LastName = left(Mysting,25)
    8. wend

    But the type is more complex. In C this can be done with pointers, but in VB. Any idea
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  3. #3
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: String to Type

    if its ALWAYS just first & last name (No middle initial) use split
    VB Code:
    1. Dim Lines() as string
    2. Dim tmp() as string
    3. Open "FIlename" for input as #1
    4. Lines = split(Input(lof(1),1),vbcrlf)
    5. CLose #1
    6. for x = 0 to ubound(tmp)
    7. tmp = split(Lines(x)," ")
    8. Person.FirstName = tmp(0)
    9. Person.LastName = tmp(1)
    10. 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"

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: String to Type

    It was specified that it was NOT just names
    I don't live here any more.

  5. #5

    Thread Starter
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    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.
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  6. #6
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    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"

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  8. #8

    Thread Starter
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    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
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: String to Type

    Well as long as your source data is padded so that it is in fact FIXED LENGHT, you can use:
    VB Code:
    1. Get #1, Person
    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
  •  



Click Here to Expand Forum to Full Width