Results 1 to 10 of 10

Thread: Adding contents of text file to the code

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Adding contents of text file to the code

    Hello, I have a text file, which looks like this:

    Code:
    1=Hello
    2=Bye
    3=Tomorrow
    4=Yesterday
    5=Sunday
    etc
    etc
    etc
    If my application returns a "3" then it extracts the text file from the application (resource file), reads it and shows the corresponding text (in this case "Tomorrow").

    I'd like to get rid of the text file and add the list of text to the code, but I don't know how to do this. Basically I want to check the list internally for the number and show the corresponding text.

    Do I need to add the list as one huge string or how would I do this?

  2. #2
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Adding contents of text file to the code

    VB Code:
    1. Select Case SomeVariable
    2. Case 1
    3.     MsgBox "Hello"
    4.     'or some thing else
    5. Case 2
    6.     MsgBox "Bye"
    7. Case 3
    8.     MsgBox "Tomorrow"
    9. Case 4
    10.     MsgBox "Yesterday"
    11. Case 6
    12.     MsgBox "Sunday"
    13. 'Case etc
    14. '    MsgBox "etc"
    15. 'etc
    16. 'etc
    17. End Select
    CS

  3. #3
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Adding contents of text file to the code

    Change to Public Const's For use within a Module.

    VB Code:
    1. Private Const str1 As String = "Monday"
    2. Private Const str2 As String = "Hello"
    3. Private Const str3 As String = "What"
    4. Private Const str4 As String = "This"
    5. Private Const str5 As String = "That"
    6.  
    7. Private Sub Command1_Click()
    8.  
    9.     Const Something As Integer = 1
    10.     Select Case Something
    11.         Case 1
    12.             MsgBox "1-" & str1
    13.         Case 2
    14.             MsgBox "2-" & str2
    15.     End Select
    16.    
    17. End Sub

    OR

    VB Code:
    1. Private strArray(5) As String
    2.  
    3. Private Sub Command1_Click()
    4.    
    5.     Dim e As Integer  
    6.     strArray(1) = "Monday"
    7.     strArray(2) = "Hello"
    8.     strArray(3) = "What"
    9.     strArray(4) = "This"
    10.     strArray(5) = "That"
    11.     For e = 1 To 5
    12.         Select Case e
    13.             Case 1
    14.                 MsgBox "1-" & strArray(e)
    15.             Case 2
    16.                 MsgBox "2-" & strArray(e)
    17.         End Select
    18.     Next e
    19.    
    20. End Sub

    OR

    VB Code:
    1. Private Const strList As String = "Monday,Hello,What,This,That"
    2. Private strArray As Variant
    3.  
    4. Private Sub Command1_Click()
    5.  
    6.     Dim e As Integer
    7.     strArray = Split("," & strList, ",")
    8.     For e = 1 To UBound(strArray)
    9.         Select Case e
    10.             Case 1
    11.                 MsgBox "1-" & strArray(e)
    12.             Case 2
    13.                 MsgBox "2-" & strArray(e)
    14.         End Select
    15.     Next e
    16.  
    17. End Sub

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Adding contents of text file to the code

    Thank you, cssriraman.

    The list is quite long. Is there a way to put each Case on one line?

    Something like this:

    VB Code:
    1. Select Case SomeVariable
    2. Case 1 sString = "Hello"
    3. Case 2 sString = "Bye"
    4. Case 3 sString = "Tomorrow"
    5. Case 4 sString = "Yesterday"
    6. Case 5 sString = "Sunday"
    7. End Select

    Thanks rory, but the text file contains a few hundred lines (18kb) and that requires me to add twice as much "text" as I had with the text file.
    Last edited by Chris001; May 14th, 2006 at 07:21 PM. Reason: typo

  5. #5
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Adding contents of text file to the code

    Even though you put in one line the execution time or memory will be same
    CS

  6. #6
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Adding contents of text file to the code

    Anyway, you can do that using colons :
    VB Code:
    1. Select Case SomeVariable
    2. Case 1 : sString = "Hello"
    3. Case 2 : sString = "Bye"
    4. Case 3 : sString = "Tomorrow"
    5. Case 4 : sString = "Yesterday"
    6. Case 5 : sString = "Sunday"
    7. End Select
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    CS

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Adding contents of text file to the code

    Thanks again, cssriraman. I just prefer to have it as a list. It makes it a bit easier for me to edit the list if needed.

    Btw, is the execution time and memory usage of doing it this way the same as reading it from a text file or not? If not, is there much difference?

  8. #8
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Adding contents of text file to the code

    Quote Originally Posted by Chris001
    Thanks rory, but the text file contains a few hundred lines (18kb) and that requires me to add twice as much "text" as I had with the text file.
    Agreed, extra little bit of code, but easier to edit that text in the future.
    As you see below its a small loop instead of Select Statements.

    VB Code:
    1. '// MAIN CONFIG MODULE
    2. Public strArray(5) As String
    3. Public SomeVariable As Integer
    4.  
    5. '// START UP SUB
    6. Public Sub  ProgramStart()
    7.     strArray(1) = "Monday"
    8.     strArray(2) = "Hello"
    9.     strArray(3) = "What"
    10.     strArray(4) = "This"
    11.     strArray(5) = "That"
    12. End Sub
    13.  
    14. '// IN YOUR FORM
    15. Private Sub Command1_Click()
    16.    
    17.     Dim e As Integer  
    18.     For e = 1 To 5
    19.         If SomeVariable = e Then
    20.            sString = strArray(e)
    21.         End If
    22.     Next e
    23.    
    24. End Sub

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Adding contents of text file to the code

    Thank you, rory

    I still would like to know if doing this internally makes much difference in execution time and memory usage compared to reading from a text file.

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Adding contents of text file to the code

    It increases memory usage and decreases execution time. If 18k makes the difference between running and not running you need a new computer. If reading a few lines from a text file (actually you can read the entire file into memory in one shot) makes an appreciable difference in execution time, you also need a new computer.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

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