•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 402,059 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,397 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser: Programming Forums
Views: 2654 | Replies: 1
![]() |
Hello,
I am currently writing a powerpoint presentation that is an interactive survey. It takes in a file of data, and adds it to a big dynamic array. When the user answers questions it picks the data from the array and populates an excel spreadsheet for a final output of a spreadsheet of that data. The problem I am having is taking in the data. I take file line by line, and each line is separated by commas another function rips that line apart to store each string chunk in the class Entitlements variables. I then want to put this Entitlements object into the array. It always puts only the last line for all the lines when it finishes. What am I doing wrong?
If anyone can help me I'd greatly appreciate it. I've been banging my head on it for a week or two now.
kharri5,
I am currently writing a powerpoint presentation that is an interactive survey. It takes in a file of data, and adds it to a big dynamic array. When the user answers questions it picks the data from the array and populates an excel spreadsheet for a final output of a spreadsheet of that data. The problem I am having is taking in the data. I take file line by line, and each line is separated by commas another function rips that line apart to store each string chunk in the class Entitlements variables. I then want to put this Entitlements object into the array. It always puts only the last line for all the lines when it finishes. What am I doing wrong?
'Name: GetLinesFromText
'Returns: nothing
'Description: This opens the file reads it line by line and sends it to be parsed. It then
'stores the parsed results into the permissionslist array for later use
'
Sub GetLinesFromText()
Dim sFileName As String
Dim iFileNum As Integer
Dim sBuf As String
Dim curEnt As New Entitlements
'For parsiing the line into separate strings
ReDim Substr(0) As String
Dim SubStrCount As Integer
Dim lineCount As Integer
Dim wordCount As Integer
Dim i As Integer
lineCount = 0
wordCount = 0
' edit this:
sFileName = REPORT_DATA_FILE
' does the file exist? simpleminded test:
If Len(Dir$(sFileName)) = 0 Then
MsgBox ("You didn't input a file at all. Please make sure the package was unzipped to the C: drive directly")
Exit Sub
End If
iFileNum = FreeFile()
Open sFileName For Input As iFileNum
'while not at the end of the file
Do While Not EOF(iFileNum)
'up the line counter to one (we use this for the dynamic array's size)
're dimension preserve the array to up its size by 1 more in prep for a new item
'as well as keep all the items its accumulated so far
ReDim Preserve permList(lineCount) As Entitlements
'Set permList(lineCount) = Nothing
'use line input to get the line
Line Input #iFileNum, sBuf
' now you have the next line of the file stored in sBuf
' rip apart sBuf into individual strings, storing the count of them
' in SubStrCount. This will be needed to iterate to the right number
SubStrCount = ParseString(Substr(), sBuf, ",")
' the below process will store the sub-strings in permList:
' Get the first string. That's a heading topic entitlement
If (SubStrCount = 5) Then
curEnt.Category = Substr(1)
curEnt.Title = Substr(2)
curEnt.Id = Substr(3)
curEnt.Enttlmnt = Substr(4)
curEnt.EntType = Substr(5)
'plug in the item at the count - 1 position for the 0 based array
Else
MsgBox ("Your source text file contains a line with more items than allowed" _
& "Please check the source file or use another one in proper format" _
& "This presentation will not work as expected unless you start over")
Exit Sub
End If
If (lineCount = 1) Then
MsgBox ("XXXfor permList(0) the first three elems are: " & permList(0).Category & " :: " _
& permList(0).Title & " :: " & permList(0).Id)
End If
MsgBox (lineCount)
'Me thinks the problem lies right here, but cannot figure out a solution at all
Set permList(lineCount) = curEnt
MsgBox ("for permList(0) the first three elems are: " & permList(0).Category & " :: " _
& permList(0).Title & " :: " & permList(0).Id)
lineCount = lineCount + 1
'IT JUST KEEPS OVERWRITING ALL MY ROWS TO THE ONE THAT WAS LAST
'MsgBox ("The Current ent details are: " & curEnt.Category & " :: " _
' & curEnt.Title & " :: " & curEnt.Id & " :: " _
' & curEnt.Id & " :: " & curEnt.Enttlmnt & " :: " _
' & curEnt.EntType)
Loop ' we've reached the end of the file now
' close the file
Close iFileNum
'after all of this the array now stores the values we will need to fill in the
'excel sheet with data
MsgBox ("for permList(0) the first three elems are: " & permList(0).Category & " :: " _
& permList(0).Title & " :: " & permList(0).Id)
MsgBox ("for permList(1) the first three elems are: " & permList(1).Category & " :: " _
& permList(1).Title & " :: " & permList(1).Id)
End Sub
If anyone can help me I'd greatly appreciate it. I've been banging my head on it for a week or two now.
kharri5,
I see that a number of people viewed this but didn't care enough about (or have a solution to) the problem I posted (i admit it wasn't a ton of time true since the initial inquiry). Anywhooo, I figured it out so for those in the future who wish to know.
It was setting the array values to whatever curEnt was last time in the loop, so regardless
of what I did, it would set all array items to curEnt's last value. For instance if I did this
And after that loop was finished if I printed this:
I'd get "" :: "" :: ""
Despite having set this AFTER I put curEnt into the array and not setting the array's values to those blanks at all. WTF???
The solution?
I set curEnt = Nothing at the top of the loop
I have no earthly clue why that fixed it...which is terrible I know...someone want to shed some light on me??
Because tho it's fixed, I'm lost as to the reason for it.
kharri5
It was setting the array values to whatever curEnt was last time in the loop, so regardless
of what I did, it would set all array items to curEnt's last value. For instance if I did this
VB Syntax (Toggle Plain Text)
If (SubStrCount = 5) Then curEnt.Category = Substr(1) curEnt.Title = Substr(2) curEnt.Id = Substr(3) curEnt.Enttlmnt = Substr(4) curEnt.EntType = Substr(5) Else MsgBox ("Your source text file contains a line with more items than allowed" _ & "Please check the source file or use another one in proper format" _ & "This presentation will not work as expected unless you start over") Exit Function End If 'Me thinks the problem lies right here, but cannot figure out a solution at all Set permList(lineCount) = curEnt curEnt.Category = "" curEnt.Title = "" curEnt.Id = "" curEnt.Enttlmnt = "" curEnt.EntType = "" lineCount = lineCount + 1
And after that loop was finished if I printed this:
VB Syntax (Toggle Plain Text)
MsgBox ("for permList(0) the first three elems are: " & permList(0).Category & " :: " _ & permList(0).Title & " :: " & permList(0).Id) MsgBox ("for permList(1) the first three elems are: " & permList(1).Category & " :: " _ & permList(1).Title & " :: " & permList(1).Id) MsgBox ("for permList(2) the first three elems are: " & permList(2).Category & " :: " _ & permList(2).Title & " :: " & permList(2).Id)
I'd get "" :: "" :: ""
Despite having set this AFTER I put curEnt into the array and not setting the array's values to those blanks at all. WTF???
The solution?
I set curEnt = Nothing at the top of the loop
VB Syntax (Toggle Plain Text)
Do While Not EOF(iFileNum) 'CRUCUIAL SETTING THIS TO NOTHING...DON'T KNOW WHY Set curEnt = Nothing 'up the line counter to one (we use this for the dynamic array's size) 're dimension preserve the array to up its size by 1 more in prep for a new item 'as well as keep all the items its accumulated so far ReDim Preserve permList(lineCount) As Entitlements
I have no earthly clue why that fixed it...which is terrible I know...someone want to shed some light on me??
Because tho it's fixed, I'm lost as to the reason for it.
kharri5
![]() |
•
•
•
•
•
•
•
•
DaniWeb VB.NET Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
.net .net framework 3.0 access api asm assembly x86 programming hla demo asp blogger blogging c c++ ccna code coding combo competition compiler computer custom data developer development dropdownlist evaluation gdata google high-performance java mcse microsoft microsystems module net networking object online planning platform practices programming python reuse rss skin software sun theme tools tutorials web xml
- Previous Thread: a list of items
- Next Thread: validate registraion form in VB.Net


Linear Mode