Results 1 to 16 of 16

Thread: Allocating variables to a sequential txt file

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Location
    Kent UK
    Posts
    9

    Unhappy Allocating variables to a sequential txt file

    First few days using VB.NET

    In VB5 I used the following routine to put values into variables A,b,c,d

    Open “C:\TextFiles\File.txt” For Input As #1
    For I = 1 To 13
    Input #1, A, b, c, d
    If A = Text1.Text Then I = 5
    Next I
    Close #1

    Where File.txt :-

    1, 23,45,44
    2,34,55,72
    3,21,15,88
    4,9,13,67
    5,10,7,28

    I can get the file information into VB.NET but cannot discover how to assign the variables.
    "Input" only seems able to input single items.
    Can anyone help?

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    File handling is a bit different in .NET you should look up the help on the FileStream, StreamWriter, StreamReader or consider using xml instead.

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I'm not sure what you are doing but it should be something like this:
    VB Code:
    1. 'open file
    2.         Dim fs As New IO.FileStream("..\File.txt", IO.FileMode.OpenOrCreate)
    3.         Dim sr As New IO.StreamReader(fs)
    4.         Dim i As Integer
    5.         For i = 1 To 13
    6.             'read a line
    7.             Dim line As String = sr.ReadLine()
    8.             'seperate values in line into an array
    9.             Dim values() As String = line.Split(",")
    10.             If values(0) = Text1.Text Then i = 5
    11.         Next
    12.         'close file
    13.         sr.Close()

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Location
    Kent UK
    Posts
    9
    Good luck!

    Looks promising I'll give it a try

    Phish

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Location
    Kent UK
    Posts
    9
    Please forgive my ignorance but I'm still not clear how I allocate values in "split" to A, b,c,d

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Well sr.Readline reads the entire line or record in as a string. Then the split method splits the comma delimited line into individual elements. There may be a .NET like Input method but if there is I'm not aware of it. They may expect you to either use the old VB6 way with some sort of variation or switch to a better form of data storage, namely xml.

    You could use Read instead of Readline and only read 1 character at a time but then you'd have to manually check for the comma and it seemed like a waste of time to me.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Location
    Kent UK
    Posts
    9
    Hmmm! all seems a bit complicated for what was previously a very simple operation.
    Perhaps if I explain in more detail what I am trying to do........

    We have a number of text files with all sorts of data in them…… price lists, calculation constants etc.

    My old VB5 programs calculated prices and repetitive time consuming calculations for a number of technical requirements.

    The old VB5 was getting a bit creaky with latest operating systems and the company in its wisdom went out and bought VB.net and passed the instruction "Rewrite all the old programs you did in VB.net"

    As you may have guessed I am not really a programmer!!!!

    Horrors!

    The Input command was able to recognise when the first element in the line of text matched the selected option in say a text box, stopped the if routine and the values were stored in the respective variable for manipulation in the calculation of price or what have you.

    I had reached the spot where the text imported into the program by a similar method to the one you described, its just separating out the elements to enable identification of the line required and insertion into the prescribed variables that has me stumped.

    Any further suggestions or code samples would be gratefully received.

  8. #8
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    maybe you could use Serialization, you made a class with all the variables you want and then serialized and de-serialized it, that would save you a lot of time..about serialization you can search the board as there are *i think* plenty of examples
    \m/\m/

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you have control over the way the data is stored, in other words if you aren't getting the comma delimited text file from another program or source, then the problem can be fixed very easily. I would suggest switching to use a data and xml. Although there will be a learning curve for you I think in the end you will find that life will be much easier afterwards. If you are familiar with databases or database programming then a dataset is much like a recordset only far better and more versatile. There are plenty of samples around if you search for DataSet or ADO.NET. This will emphasize the 'rewrite' part of 'rewriting your apps in .net'. I will try to post a sample later today but it would help if these values had some meaning. XML is self describing in other words it holds values as well as identifies what they are to represent.

  10. #10

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Location
    Kent UK
    Posts
    9
    A sample would be most kind.

    Right here is an actual problem:-

    on the form there is a combobox with 19 items in it.

    From the combobox a mesh type is selected from the 19 options and a routine within the code allocates a Mesh number 1 to 19.

    The user then puts in values into various textboxes and when the calculate button is hit the following occurs:-

    The data file PD.DAT is opened and the loop picks up the identifier (First item in the line) and allocates variable to all on the other items in the line.

    Open “C:\PD.DAT” For Input As #1
    For i = 1 To 19
    Input #1, A, K, a, b, c
    If A = Mesh Number Then i = 19
    Next i
    Close #1


    A, K, a, b, c
    1,174.454,0.599,0.441,1.559
    2,7.729,0.742,0.258,1.742
    3,17.593,0.71,0.29,1.71
    4,4.109,0.765,0.235,1.765
    5,4.544,0.757,0.243,1.757
    6,1.681,0.824,0.176,1.824
    7,55.078,0.616,0.384,1.616
    8,34.886,0.661,0.339,1.661
    9,42.648,0.732,0.268,1.732
    10,20.826,0.788,0.212,1.788
    11,12.772,0.777,0.223,1.777
    12,3.947,0.818,0.182,1.818
    13,27.199,0.729,0.271,1.729
    14,17.317,0.715,0.285,1.715
    15,3049.319,0.401,0.599,1.401
    16,1313.705,0.489,0.511,1.489
    17,670.664,0.533,0.447,1.553
    18,199.873,0.655,0.345,1.655
    19,13403.355,0.299,0.701,1.299


    The pressure drop calculation is then performed by a standard calculation which picks up user input from textboxes and constants for the type of mesh selected K, a, b, c

    Pressure drop = (Mesh thickness) * (K) * (Vapour Density ^ a) * (Vapour Viscosity ^ b) * (Velocity ^c)


    Works well but now its seems in VB.net I need another way of accessing the data.

    I would rather not have to rewrite the data as there are many pages of it for different tasks.

    It would not I suspect be too difficult a task to import then into excel spread sheets.

    I’ve now reached the stage where I’m looking for a job picking up litter!!!!!

  11. #11

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Let me know if those links are helpful enough and I'll do a sample then.

  13. #13

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Location
    Kent UK
    Posts
    9
    The theroy looks great, but unfortunately the programming level is a bit beyond me. Did I say a bit!

    The delimit XML in devcity seemed more understandable it's just that I have no idea how to impliment it.

    I do apreciate your interest and attempst to help, I'm sorry that it must seem like teaching a horse to play the piano!

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is the sample I promised. It will show you how to use an xml file and databinding for data. I'm afraid it may not be that great of an example since I don't have a clue what you are doing with the data. Feel free to ask questions on anything I left out or you don't understand. Also under the Tools menu in the sample you will find a converter to convert comma delimited text files into xml files. I figured that might be helpful since you said you had a lot of data already in text format.

  15. #15

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Location
    Kent UK
    Posts
    9
    Oh Dear it gets worse!

    Downloaded sample, but on attempting to open it I get a message Saying that it cannot be opened as it is from a later version of studio than the one I'm using.

    VB.net standard with only visual basic included purchased new within last 2 weeks.

    Are we snookered or can you save it in my version


    Regards

    Phish

  16. #16
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Follow the link in my signature to convert the solution from 2003 to 2002 and it should all be fine.

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