Writing Entries in a VB Dictionary into a Text File - vb.net

I'm working on VB in college and am running into a snag with one of my assignments. Can someone help? I'm aiming to try to take the following dictionary code:
Public Class Inventory
Public ItemInventory As New Dictionary(Of String, Item)
Public Function iItem(ByVal key As String) As Item
Return ItemInventory(key)
End Function
Public Sub addItem(ByVal item As String, ByVal Desc As String, ByVal DRate As Double, ByVal WRate As Double, _
ByVal MRate As Double, ByVal Quantity As Integer)
With ItemInventory
.Add(item, New Item(item, Desc, DRate, WRate, MRate, Quantity))
End With
End Sub
Public Sub removeItem(ByVal item As String)
With ItemInventory
.Remove(item)
End With
End Sub
Public Function returnKeys() As String()
Dim Keys() As String
With ItemInventory
Keys = .Keys.ToList.ToArray
End With
Return Keys
End Function
End Class
Not pretty, I know, but it gets the job done, that's all I aim to do. Now a bit of this also has to do with displaying a dictionary item in the program, which I'm also having issues with, however, I'd like to take this one step at a time, so we'll get to that later, if possible.
As per writing, this is my current code for reading and writing:
Imports System.IO
Public Class InventoryFile
Public Sub RFile(ByVal FPath As String, ByRef dInventory As Inventory)
Dim infile As StreamReader = File.OpenText(FPath)
Dim entireLine As String = infile.ReadLine()
Dim fields() As String = entireLine.Split(","c)
While infile.EndOfStream
Dim dItem As New Item
dItem.ID = fields(0)
dItem.Description = fields(1)
dItem.Daily = fields(2)
dItem.Weekly = fields(3)
dItem.Monthly = fields(4)
dItem.Quantity = fields(5)
'AddItem
dInventory.addItem(dItem.ID, dItem.Description, dItem.Daily, dItem.Weekly, _
dItem.Monthly, dItem.Quantity)
End While
End Sub
Public Sub WFile(ByVal FPath As String, ByRef dInventory As Inventory)
Dim outfile As StreamWriter = File.CreateText(FPath)
For Each Item As KeyValuePair(Of String, Item) In dInventory.ItemInventory
Next
End Sub
End Class
I hope that posted right. Now, reading in, as far as I understand, works just fine, in terms of a file going into a dictionary, however 'WFile', my StreamWriter, is what's got me stumped. Can someone help me with that? Likewise, its supposed to close and write to the file upon closing, and my only code for the close button is the Me.Close() command. How would I write a trigger to make the program write to the file? Know that the main form code, and my 'InventoryFile' are both separate classes, so this has to be done by referencing the other classes in question

Try this to write each dictionary key/value pair on a single line in the file:
Dim fs As FileStream
' Open the stream and write to it.
fs = File.OpenWrite(FPath)
For Each Item As KeyValuePair(Of String, Item) In dInventory.ItemInventory
fs.Write("{0}:{1}", Item.Key, Item.Value)
Next
UPDATE:
Since Item is both the variable used in the loop and the name of a class, change it to another name like singleItem and then pull out the other pieces of information from the Value portion of the key/value pair, because the Value is actually an Item class object, like this:
Dim fs As FileStream
' Open the stream and write to it.
fs = File.OpenWrite(FPath)
For Each singleItem As KeyValuePair(Of String, Item) In dInventory.ItemInventory
fs.Write("{0}:{1}:{2}:{3}:{4}:{5}:{6}", singleItem.Key, singleItem.Value.ID, singleItem.Value.Description, singleItem.Value.Daily, singleItem.Value.Weekly, singleItem.Value.Monthly, singleItem.Value.Quantity)
Next

In order to create the format your RFile procedure can read you need something like this:
For Each kvp As KeyValuePair(Of String, Item) In dInventory.ItemInventory
' using Karl's compact approach, but add commas
' since your Read expects them
outfile.Write("{0},{1},{2},{3},{4}...", kvp.Key, kvp.Value.ID, _
kvp.Value.Description, ... kvp.Value.Quantity.Tostring)
' the Value part of the fvp is another class, right? `Value.XXX` should drill
' into it to get to the members
Next
outfile.flush
outfile.close ' look into 'Using...'
Thats NOT how I would do it, look into serialization for a less fragile way to read/write the data. It is basically meant for just this sort of thing: save class data for later, and it is not that hard to use.
as for hooking it up, the button click would just call Inventory.WFile

Related

Contents of List(Of String) are not saved

I am attempting to parse paragraphs such as the following...
Group 1. Does this or does that. Or Sometimes this. Or that.
Group 2. I do lots of things. But not this. Or that.
Group 3. I do this. I do that. Sometimes this. Sometimes that.
The "Group 1-3" are the org Names, and each following sentence separated by a period is a function.
Code:
Public Sub parseParagraphs(paragraphList As List(Of String))
Dim listOfOrgs As New List(Of EAB_Org)
Dim listOfFuntions As New List(Of String)
Dim orgName As String
For Each item In paragraphList
listOfFuntions.Clear()
Dim words As String() = item.Split(New Char() {"."c}) 'Splits on periods
orgName = words(0) 'Sets the orgName
For index As Integer = 1 To words.Count - 1 'rest of items in list are functions performed
listOfFuntions.Add(words(index))
Next
Dim anOrg As New EAB_Org(orgName, listOfFuntions)
listOfOrgs.Add(anOrg)
Next
End Sub
EAB Class:
Public Class EAB_Org
Dim orgName As String
Dim listOfTasks As List(Of String)
Public Sub New(theOrgName As String, theListOfTasks As List(Of String))
orgName = theOrgName
listOfTasks = theListOfTasks
End Sub
Public Function getOrgName()
Return orgName
End Function
Public Function getListOfTasks()
Return listOfTasks
End Function
End Class
For some reason, when I print out the contents of listOfOrgs, all the org names are correct, but the functions are all of the same and always the last set of functions read in.
Code I use to print:
Public Sub writeExcel(listOfOrgs As List(Of EAB_Org))
For Each anItem In listOfOrgs
Console.WriteLine(anItem.getOrgName)
For Each anotherItem In anItem.getListOfTasks
Console.WriteLine(anotherItem)
Next
Next
End Sub
Output Looks Like:
Group 1
I do this. I do that. Sometimes this. Sometimes that.
Group 2
I do this. I do that. Sometimes this. Sometimes that.
Group 3
I do this. I do that. Sometimes this. Sometimes that.
The problem is that in the constructor for EAB_Org, theListOfTasks is just a pointer to listOfFuntions (which you keep modifying) in the parseParagraphs Sub. In the constructor, you will need to create a new List(Of String) and copy the values from theListOfTasks into it.
Change the constructor to the following:
Public Sub New(theOrgName As String, theListOfTasks As List(Of String))
orgName = theOrgName
listOfTasks = New List(Of String)
For Each item As String In theListOfTasks
listOfTasks.Add(item)
Next
End Sub

Readonly in keyvaluepair

Well I have created a program that takes some files (Mp3) and change their tags
recently I wanted to add some new Subs (like: Take the songs name and make every letter in it upercase). The problem is that i use a list with its items to be keyvaluepairs
Public MP3List As New List(Of KeyValuePair(Of String, String))
When i tried to edit the key or value of any Item in that list i get an error (That this is READONLY)
Example:
For Each Song In MP3List
Song.Key = "Something"
Next
I add items like this :
Private Function OpenAFile()
Dim MP3List1 = MP3List
Dim oFileDialog As New OpenFileDialog
oFileDialog.Title = "Επέλεξε ένα MP3 Άρχειο"
oFileDialog.Filter = "MP3 Files|*.mp3|All Files|*.*"
oFileDialog.Multiselect = True
Dim Path As String = ""
Dim Name As String = ""
Dim NewPair As New KeyValuePair(Of String, String)
If oFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
For Each sPath In oFileDialog.FileNames
Path = New String(sPath)
Name = New String(Strings.Split(Path, "\").ToList(Strings.Split(Path, "\").ToList.Count - 1))
NewPair = New KeyValuePair(Of String, String)(Name, Path)
If Not MP3List1.Contains(NewPair) Then MP3List1.Add(NewPair)
Next
End If
Return MP3List1
End Function
So the idea is this: Each time i press A button to add a song it will run the function OpenAFile() and it was working fine then . Now that i want to change a key or value i get this error
Thanks for the Help and sorry for bad english
The Keys in a KeyValuePair are readonly because they are often used as the key in a hash table. Changing the key would cause issues where you would lose your item in the hash.
If you want to do something like this, you could always create your own data type that stores a key and value. An overly simplified example would be as follows.
Public Structure PathNamePair
Public Property Path As String
Public Property Name As String
Public Sub New(path As String, name As String)
Me.Path = path
Me.Name = name
End Sub
End Structure
I will note that in order to get better performance with your Contains method, you should also implement IEquatable(Of T), but that's probably beyond the scope of this question. I will also note that it is not best practice to have a ValueType (Structure) that is mutable.

List of user made Object not updating values indivudally

I'm pretty sure this problem is really obvious, but I can't seem to make due. I have a list of a user defined object (not by me, but I can look into editing if need be). I tried to declare it to have 14 blank objects. That way when I go to listname(5).setvalues(), it only edits that value. Instead it edits all of them (i.e. all 14) in the list or leaves them to be null.
Here's the code:
Dim currentProperties As New List(Of ExtendedCamObject)
'create a blank list
For i As Integer = 0 To 13
' Dim exp As New ExtendedCamObject
' currentProperties.Add(exp)
currentProperties.Add(New ExtendedCamObject)
Next
propVal = "4012"
currentProperties(8).SetValues(ExtendedCamObject.PropertyTypes.Max_Bitrate, propVal)
This leaves them to null. If I do the commented out version instead (removing the other line in the for loop), it sets them all to the same value. Here's the set value's definition in the class definition:
Private m_strValue As String
Private m_PropertyType As String
Public Sub SetValues(ByVal ExtendedProperty As PropertyTypes,
ByVal strValue As String)
m_PropertyType = CType(ExtendedProperty, PropertyTypes)
m_strValue = strValue
End Sub
I didn't write this user object, but I noticed that there aren't any 'get/set' property items from the original coder. Is that why my values are not being set correctly?
You could use some code clean up here:
Public Class ExtendedCamObject
Private _strValue As String
Private _PropertyType As ExtendedProperty
Public Sub SetValues(ByVal ExtendedProperty As PropertyTypes, ByVal strValue As String)
_PropertyType = ExtendedProperty
_strValue = strValue
End Sub
...
End Class

Why won't the correct definition in my code work in an "If/Else" statement?

I am making a quiz for my computer science class and the basic concept is that you have 15 keywords and 15 definitions. All need to be randomly displayed and the correct answer has to appear. The user has to match the correct definition to the keyword twice and then that keyword and definition are not displayed again. When all have been answered twice the quiz is over.
I have stored both my keywords and my definitions in the same file so they don't get out of sync. The text file looks like so:
Keyword1,Definition1
Keyword2,Definition2
Keyword3,Definition3
...
Thanks to the help from others here on StackOverflow I have managed to get the randomizing and dictionary completed. For reference that looks like this:
Public Class Form1
Private kv As Dictionary(Of String, Answer) 'Define kv as a dictionary
Private keyword As String
Private correctDefinition As String
Const NUMBER_OF_ANSWERS As Integer = 3 'Define the constant NUMBER_OF_ANSWERS
Private Sub RandomiseAnswers()
Dim r As New Random
Dim kvRandom As List(Of KeyValuePair(Of String, Answer)) =
kv.OrderBy(Function() r.Next).ToList
'questions will appear in random order
For Each line As KeyValuePair(Of String, Answer) In kvRandom
Dim keyword As String = line.Key
Dim correctDefinition As String = line.Value.Answer
Dim keywords As New List(Of String)
keywords.Add(keyword)
keywords.AddRange(kv.Keys.Except({keyword}).
OrderBy(Function() r.Next).Take(NUMBER_OF_ANSWERS - 1))
Dim definitionsRandom As List(Of String) =
keywords.Select(Function(x) kv(x).Answer).OrderBy(Function() r.Next).ToList
LabelKeyword.Text = keyword
RadioButtonDef1.Text = definitionsRandom(0)
RadioButtonDef2.Text = definitionsRandom(1)
RadioButtonDef3.Text = definitionsRandom(2)
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'The text file is structured like so:
'Keyword1,Definition1
'Keyword2,Definition2
'Keyword3,Defintion3
'...
'This makes sure that the keywords and the definitions do not get out of sync when randomising, as a dictionary is a key-value data structure. Call the key, get the right value etc.
kv = New Dictionary(Of String, Answer) 'kv = a new dictionary
For Each line As String In IO.File.ReadAllLines("C:\Users\Matt\Documents\keywords.txt") 'Foreach loop populating the dictionary from the text file
Dim parts() As String = line.Split(",") 'Split the Keywords from the definitions where the , is
kv.Add(parts(0), New Answer With {.Answer = parts(1), .Answered = False}) 'Add the two parts (Keyword and Definition) to the Parts with the Answer class
Next
Dim r As New Random 'Define r as new random
Dim kvRandom As List(Of KeyValuePair(Of String, String)) =
kv.OrderBy(Function() r.Next) _
.Select(Function(x) New KeyValuePair(Of String, String)(x.Key, x.Value.Answer)) _
.ToList() 'A Select method to convert the items in the list properly
'questions will appear in random order
For Each line As KeyValuePair(Of String, String) In kvRandom
Dim keyword As String = line.Key
Dim correctDefinition As String = line.Value 'Checks that the correct definition will be displayed when its keyword is randomly displayed.
'Define keywords as a new list
Dim keywords As New List(Of String)
keywords.Add(keyword) 'Adds the keyword to the list as the Part(0) from the text file in the first loop
keywords.AddRange(kv.Keys.Except({keyword}).
OrderBy(Function() r.Next).Take(NUMBER_OF_ANSWERS - 1))
Dim definitionsRandom As List(Of String) =
keywords.Select(Function(x) kv(x).Answer).OrderBy(Function() r.Next).ToList 'Define definitionsRandom as a list. Checks against correctDefinition to know to always display the correct answer as one of the
'random definitions
LabelKeyword.Text = keyword 'Randomly display a keyword
RadioButtonDef1.Text = definitionsRandom(0) 'Randomly display a definition. Checks against correctDefinition to see make sure one definition is always the right answer.
RadioButtonDef2.Text = definitionsRandom(1) 'Randomly display a definition. Checks against correctDefinition to see make sure one definition is always the right answer.
RadioButtonDef3.Text = definitionsRandom(2) 'Randomly display a definition. Checks against correctDefinition to see make sure one definition is always the right answer.
Next
End Sub
Private Sub ButtonNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNext.Click
'If (RadioButtonDef1.Checked And RadioButtonDef1.Text = correctDefinition) Or (RadioButtonDef2.Checked And RadioButtonDef2.Text = correctDefinition) Or (RadioButtonDef3.Checked And RadioButtonDef3.Text = correctDefinition) Then
' If kv(keyword).Answered Then
' kv.Remove(keyword)
' Else
' kv(keyword).Answered = True
' End If
'Else
' MsgBox("Incorrect Answer!")
'End If
If (RadioButtonDef1.Checked And RadioButtonDef1.Text = correctDefinition) Or (RadioButtonDef2.Checked And RadioButtonDef2.Text = correctDefinition) Or (RadioButtonDef3.Checked And RadioButtonDef3.Text = correctDefinition) Then
MsgBox("Correct Answer")
Else
MsgBox("Incorrect Answer. The correct answer is: " & correctDefinition)
End If
End Sub
End Class
I use a subroutine in my buttonNext_click to prevent duplicate code, as you can see above Private Sub RandomiseAnswers().
my issue however is when I come down to the If/else statement where I determine whether the user has selected the correct answer. As you can see I have commented out the original line as I found that it didn't work, and therefore I created one which would display a message box. By using that If/Else statement I have found that it always displays the Incorrect Answer message box and it doesn't display the correct definition.
What is puzzling me the most is that I know that correctDefinition works as it will always display the correct definition to the current keyword however I cannot seem to fathom out how to make it work in the conditional/s.
Does anyone have a possible solution as to why it doesn't seem to like being used in the If/Else statement/s but does work when randomising.
Sorry if it is hard to understand what I am asking.
Any help is much appreciated.
EDITS
code relevant to the Answer class
Public Class Answer
Public Answer As String
Public Answered As Boolean
End Class
You declare correctDefinition twice in your code. Declare it once or better yet make correctDefinition an array eg.
Private correctDefinition() String = New String() { "Definition 1", "Definition 2", "Definition 3" }

VB.NET - Load a List of Values from a Text File

I Have a text file that is like the following:
[group1]
value1
value2
value3
[group2]
value1
value2
[group3]
value3
value 4
etc
What I want to be able to do, is load the values into an array (or list?) based on a passed in group value. eg. If i pass in "group2", then it would return a list of "value1" and "value2".
Also these values don't change that often (maybe every 6 months or so), so is there a better way to store them instead of a plain old text file so that it makes it faster to load etc?
Thanks for your help.
Leddo
This is a home work question?
Use the StreamReader class to read the file (you will need to probably use .EndOfStream and ReadLine()) and use the String class for the string manipulation (probably .StartsWith(), .Substring() and .Split().
As for the better way to store them "IT DEPENDS". How many groups will you have, how many values will there be, how often is the data accessed, etc. It's possible that the original wording of the question will give us a better clue about what they were after hear.
Addition:
So, assuming this program/service is up and running all day, and that the file isn't very large, then you probably want to read the file just once into a Dictionary(of String, List(of String)). The ContainsKey method of this will determine if a group exists.
Function GetValueSet(ByVal filename As String) As Dictionary(Of String, List(Of String))
Dim valueSet = New Dictionary(Of String, List(Of String))()
Dim lines = System.IO.File.ReadAllLines(filename)
Dim header As String
Dim values As List(Of String) = Nothing
For Each line As String In lines
If line.StartsWith("[") Then
If Not values Is Nothing Then
valueSet.add(header, values)
End If
header = GetHeader(line)
values = New List(Of String)()
ElseIf Not values Is Nothing Then
Dim value As String = line.Trim()
If value <> "" Then
values.Add(value)
End If
End If
Next
If Not values Is Nothing Then
valueSet.add(header, values)
End If
Return valueSet
End Function
Function GetHeader(ByVal line As String)
Dim index As Integer = line.IndexOf("]")
Return line.Substring(1, index - 1)
End Function
Addition:
Now if your running a multi-threaded solution (that includes all ASP.Net solutions) then you either want to make sure you do this at the application start up (for ASP.Net that's in Global.asax, I think it's ApplicationStart or OnStart or something), or you will need locking. WinForms and Services are by default not multi-threaded.
Also, if the file changes you need to restart the app/service/web-site or you will need to add a file watcher to reload the data (and then multi-threading will need locking because this is not longer confined to application startup).
ok, here is what I edned up coding:
Public Function FillFromFile(ByVal vFileName As String, ByVal vGroupName As String) As List(Of String)
' open the file
' read the entire file into memory
' find the starting group name
Dim blnFoundHeading As Boolean = False
Dim lstValues As New List(Of String)
Dim lines() As String = IO.File.ReadAllLines(vFileName)
For Each line As String In lines
If line.ToLower.Contains("[" & vGroupName.ToLower & "]") Then
' found the heading, now start loading the lines into the list until the next heading
blnFoundHeading = True
ElseIf line.Contains("[") Then
If blnFoundHeading Then
' we are at the end so exit the loop
Exit For
Else
' its another group so keep going
End If
Else
If blnFoundHeading And line.Trim.Length > 0 Then
lstValues.Add(line.Trim)
End If
End If
Next
Return lstValues
End Function
Regarding a possible better way to store the data: you might find XML useful. It is ridiculously easy to read XML data into a DataTable object.
Example:
Dim dtTest As New System.Data.DataTable
dtTest.ReadXml("YourFilePathNameGoesHere.xml")