469,602 Members | 1,640 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

Populate dictionary with text file VB

GTXY20
29
Hi All,

I have been able to create a successful application with Python but I am being asked to create in VB.

I have a text file with the following data:

1,a
1,b
1,c
2,a
2,a
2,b
3,a
3,c

I need to create a dictionary in VB so that I can then iterate over it so that my key is the first field and my item is the concatenation of unique items for each key in the text file:

In Python I would have:

Expand|Select|Wrap|Line Numbers
  1. {1:(a,b,c), 2:(a,b), 3:(a,c)}
I am starting with the following in VB:

Expand|Select|Wrap|Line Numbers
  1. Private Sub ImpFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImpFile.Click
  2.         Dim uhdict As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)
  3.         Dim openimp As New OpenFileDialog()
  4.         If openimp.ShowDialog() = DialogResult.OK Then
  5.             Dim sr As New System.IO.StreamReader(openimp.FileName)
  6.             sr.Close()
  7.         End If
  8.     End Sub
  9.  
How do I begin to assign a variable for the key, assign a variable for the items and then add to the dictionary uhdict.

Any help and guidance wher to start is very much appreciated.

Thanks again.
Oct 17 '07 #1
3 3045
Shashi Sadasivan
1,435 Expert 1GB
Hi All,

I have been able to create a successful application with Python but I am being asked to create in VB.

I have a text file with the following data:

1,a
1,b
1,c
2,a
2,a
2,b
3,a
3,c

I need to create a dictionary in VB so that I can then iterate over it so that my key is the first field and my item is the concatenation of unique items for each key in the text file:

In Python I would have:

Expand|Select|Wrap|Line Numbers
  1. {1:(a,b,c), 2:(a,b), 3:(a,c)}
I am starting with the following in VB:

Expand|Select|Wrap|Line Numbers
  1. Private Sub ImpFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImpFile.Click
  2.         Dim uhdict As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)
  3.         Dim openimp As New OpenFileDialog()
  4.         If openimp.ShowDialog() = DialogResult.OK Then
  5.             Dim sr As New System.IO.StreamReader(openimp.FileName)
  6.             sr.Close()
  7.         End If
  8.     End Sub
  9.  
How do I begin to assign a variable for the key, assign a variable for the items and then add to the dictionary uhdict.

Any help and guidance wher to start is very much appreciated.

Thanks again.
Are you having issues with reading the text file and extracting its values or with adding elements into the dictionary? Or all of it?
Dictionary has an Add method

Expand|Select|Wrap|Line Numbers
  1. int x = 0;//put your number here
  2. string val = "value";//
  3. uhdict.Add(x,val);
(Do apply the conversion to vb .net from one of the online converters)
Oct 17 '07 #2
GTXY20
29
Thanks but I am having difficulty assigning the variables to the text file so that if I use split by "," the first variable say a is assigned that value and that will be the key and the next value after the comma is assigned to say variable b and it will be the value for the key - I then need to append to the values where the key is the same when reading the text file and splitting using comma. Something like:

1,a
1,b
1.c


Key = 1

Value = a,b,c

So can I use a for loop to say if Key is present append value if it doesn't currently exist and sort values.

Thanks.
Oct 17 '07 #3
Shashi Sadasivan
1,435 Expert 1GB
Thanks but I am having difficulty assigning the variables to the text file so that if I use split by "," the first variable say a is assigned that value and that will be the key and the next value after the comma is assigned to say variable b and it will be the value for the key - I then need to append to the values where the key is the same when reading the text file and splitting using comma. Something like:

1,a
1,b
1.c


Key = 1

Value = a,b,c

So can I use a for loop to say if Key is present append value if it doesn't currently exist and sort values.

Thanks.
Hi ,
PLease have a look at some tutorials on how to use a Dictionary.
By the way you speak, I dont think you have looked at this datattype.

good luck
Oct 17 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Berimor | last post: by
9 posts views Thread by oz | last post: by
20 posts views Thread by Simon Strobl | last post: by
4 posts views Thread by John Townsend | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.