|
-
May 14th, 2006, 06:15 PM
#1
Thread Starter
Frenzied Member
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?
-
May 14th, 2006, 06:23 PM
#2
Re: Adding contents of text file to the code
VB Code:
Select Case SomeVariable
Case 1
MsgBox "Hello"
'or some thing else
Case 2
MsgBox "Bye"
Case 3
MsgBox "Tomorrow"
Case 4
MsgBox "Yesterday"
Case 6
MsgBox "Sunday"
'Case etc
' MsgBox "etc"
'etc
'etc
End Select
-
May 14th, 2006, 07:03 PM
#3
PowerPoster
Re: Adding contents of text file to the code
Change to Public Const's For use within a Module.
VB Code:
Private Const str1 As String = "Monday"
Private Const str2 As String = "Hello"
Private Const str3 As String = "What"
Private Const str4 As String = "This"
Private Const str5 As String = "That"
Private Sub Command1_Click()
Const Something As Integer = 1
Select Case Something
Case 1
MsgBox "1-" & str1
Case 2
MsgBox "2-" & str2
End Select
End Sub
OR
VB Code:
Private strArray(5) As String
Private Sub Command1_Click()
Dim e As Integer
strArray(1) = "Monday"
strArray(2) = "Hello"
strArray(3) = "What"
strArray(4) = "This"
strArray(5) = "That"
For e = 1 To 5
Select Case e
Case 1
MsgBox "1-" & strArray(e)
Case 2
MsgBox "2-" & strArray(e)
End Select
Next e
End Sub
OR
VB Code:
Private Const strList As String = "Monday,Hello,What,This,That"
Private strArray As Variant
Private Sub Command1_Click()
Dim e As Integer
strArray = Split("," & strList, ",")
For e = 1 To UBound(strArray)
Select Case e
Case 1
MsgBox "1-" & strArray(e)
Case 2
MsgBox "2-" & strArray(e)
End Select
Next e
End Sub
-
May 14th, 2006, 07:16 PM
#4
Thread Starter
Frenzied Member
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:
Select Case SomeVariable
Case 1 sString = "Hello"
Case 2 sString = "Bye"
Case 3 sString = "Tomorrow"
Case 4 sString = "Yesterday"
Case 5 sString = "Sunday"
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
-
May 14th, 2006, 07:20 PM
#5
Re: Adding contents of text file to the code
Even though you put in one line the execution time or memory will be same
-
May 14th, 2006, 07:22 PM
#6
Re: Adding contents of text file to the code
Anyway, you can do that using colons :
VB Code:
Select Case SomeVariable
Case 1 : sString = "Hello"
Case 2 : sString = "Bye"
Case 3 : sString = "Tomorrow"
Case 4 : sString = "Yesterday"
Case 5 : sString = "Sunday"
End Select
When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
-
May 14th, 2006, 07:28 PM
#7
Thread Starter
Frenzied Member
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?
-
May 14th, 2006, 07:31 PM
#8
PowerPoster
Re: Adding contents of text file to the code
 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:
'// MAIN CONFIG MODULE
Public strArray(5) As String
Public SomeVariable As Integer
'// START UP SUB
Public Sub ProgramStart()
strArray(1) = "Monday"
strArray(2) = "Hello"
strArray(3) = "What"
strArray(4) = "This"
strArray(5) = "That"
End Sub
'// IN YOUR FORM
Private Sub Command1_Click()
Dim e As Integer
For e = 1 To 5
If SomeVariable = e Then
sString = strArray(e)
End If
Next e
End Sub
-
May 14th, 2006, 07:54 PM
#9
Thread Starter
Frenzied Member
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.
-
May 14th, 2006, 10:11 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|