Calling Multiple Object/Variables "Name" with Looping - vb.net

I got stucked on making looping for adding these Variables to ComboBox, i want to call it with a simpler way with Looping for, but i failed so many times, i've been googling around but i still failed, so any help will be appreciated
Public MyPass1 As String = "John"
Public MyPass2 As String = "Andrew"
Public MyPass3 As String = "Stewart"
Public MyPass4 As String = "Meiny"
Public MyPass5 As String = "Franco"
Public MyPass6 As String = "Hanks"
Public MyPass7 As String = "Buzz"
Public MyPass8 As String = "Timmy"
Public MyPass9 As String = "George"
Public MyPass10 As String = "Sanders"
Sub Putitem(ByVal MyPass)
With cmbAsk
For i As Integer = 0 To 9
Dim c As Integer
c = i + 1
Items.Add(MyPass(c)) 'The main problem is here, i want to do looping for calling it.
i = c
Next
End With
End Sub
Any help would be appreciated. Thanks in advance.

Instead of storing the values in individual variables, you can store them in an array:
Public MyPasses As String() = New String() {
"John",
"Andrew",
"Stewart",
"Meiny",
"Franco",
"Hanks",
"Buzz",
"Timmy",
"George",
"Sanders"
}
You can then access via:
Items.Add(MyPasses(c))

You need a collection to add not public strings.
Private collection() As String = {"John", "Mark", "Frank"} 'initializer
cmbAsk.Items.AddRange(collection.ToArray)
AddRange method

Related

Is there a function to write/export values of list-/class items?

From an external application data is extracted and written to a list with several sublist items, here is an example narrowed down to the main list creation:
Public Class clMain
Public lsLevel_1 As New List(Of clLevel_1)
End Class
Public Class clLevel_1
Public sgName As String
Public lsLevel_2 As New List(Of clLevel_2)
End Class
Public Class clLevel_2
Public sgName As String
Public lsLevel_3 As New List(Of clLevel_3)
End Class
Public Class clLevel_3
Public sgName As String
Public sgComment As String
End Class
Module Code
Sub Main()
Dim lv_clMain As clMain = New clMain
For lv_i16Level_1 = 10 To 11
Dim lv_clLevel_1 As New clLevel_1
lv_clLevel_1.sgName = "Level 1: " & lv_i16Level_1
For lv_i16Level_2 = 20 To 21
Dim lv_clLevel_2 As New clLevel_2
lv_clLevel_2.sgName = "Level 2: " & lv_i16Level_2
For lv_i16Level_3 = 30 To 31
Dim lv_clLevel_3 As New clLevel_3
lv_clLevel_3.sgName = "Level 3: " & lv_i16Level_3
lv_clLevel_2.lsLevel_3.Add(lv_clLevel_3)
Next
lv_clLevel_1.lsLevel_2.Add(lv_clLevel_2)
Next
lv_clMain.lsLevel_1.Add(lv_clLevel_1)
Next
End Sub
End Module
Once the list has been "filled", I would like to write/export the values to a file. What would be a good approach for this tasks? I think creating a function with lv_clMain as parameter. But as lv_clMain is not a collection type, and it can contain several entries of lsLevel_1 I am somewhat lost about the correct approach.
Thanks to user Hursey, I found a solution using XML serialisation.
Function SerializeXml(ByVal obData As Object, ByVal sgFileName As String, tyData As Type)
Dim obXmlSerializer As System.Xml.Serialization.XmlSerializer = New System.Xml.Serialization.XmlSerializer(tyData)
Dim obSW As System.IO.TextWriter = New System.IO.StreamWriter(sgFileName)
obXmlSerializer.Serialize(obSW, obData)
obSW.Close()
End Function

Using classes for JSON serialization

All,
I need some help with understanding how classes can work with vb.NET and JSON.NET. I'm completely new to this. I've tried searching for answers, but I'm probably not asking the right questions. Here's my dilemma:
I have a JSON that I need to send to a REST API.
{
"paInfo":[
{
"providerAccountName":"someClient",
"providerAccountDescription":"A fine client.",
"providerName":"provider",
"externalProviderIdentifier":"BU4377890111"
},
{
"providerAccountName":"someClient1",
"providerAccountDescription":"A fine client.",
"providerName":"provider",
"externalProviderIdentifier":"BU4377890111"
}
],
"hubAccountName":"test"
}
I ran this through https://jsonutils.com/ to build my class as:
Public Class PaInfo
Public Property providerAccountName As String
Public Property providerAccountDescription As String
Public Property providerName As String
Public Property externalProviderIdentifier As String
End Class
Public Class addHubAcct
Public Property paInfo As PaInfo()
Public Property hubAccountName As String
End Class
From there, I'm trying to assign values to the class properties, but I don't quite understand how to pass the values for PaInfo to the property. Below is a snippet of code I'm using to assign values. If I try to assign a.paInfo = p, it errors:
error BC30311: Value of type 'PaInfo' cannot be converted to
'PaInfo()'
If I don't pass anything through to a.paInfo, I get a zero-length string in the JSON serialization.
Private Sub serializeAcct()
Dim p As New PaInfo
Dim a As New addHubAcct
p.providerAccountName = "Test\name'This ""that and the other'"
p.providerAccountDescription = "acct desc"
p.providerName = "tester"
p.externalProviderIdentifier = "123456"
a.hubAccountName = "Tester"
a.paInfo = p 'Here's my hangup
Dim o As String = JsonConvert.SerializeObject(a)
Dim deserializedProduct As addHubAcct = JsonConvert.DeserializeObject(Of addHubAcct)(o)
Stop
End Sub
?o.tostring,nq
{"paInfo":null,"hubAccountName":"Tester"}
Change the addHubAcct class like this:
Public Class addHubAcct
Public Property paInfo As New List(Of PaInfo)()
Public Property hubAccountName As String
End Class
And then change the bad line in serializeAcct() like this:
a.paInfo.Add(p)
You likely have other problems as well, but that should get you past the current obstacle.
Using List and .ToArray is what I was missing with my original code.
Private Sub serializeAcct()
Dim p1 As New PaInfo
Dim ps As New List(Of PaInfo)
Dim a As New addHubAcct
p1.providerAccountName = "Test\name'This ""that and the other'"
p1.providerAccountDescription = "acct desc"
p1.providerName = "tester"
p1.externalProviderIdentifier = "123456"
ps.Add(p1)
a.hubAccountName = "Tester"
a.paInfo = ps.ToArray
Dim o As String = JsonConvert.SerializeObject(a)
End Sub

Evaluate expression

So, I have an object with some properties, like this: Dim.d1, Dim.d2,...,Dim.d50
that return strings. For example: Dim.d1="Description A", Dim.d2="Description B",etc.
What I want to do is to attribute these descriptions to the headers of a Gridview and for that I was thinking using indexes, like this pseudocode:
for i=0 until 49
e.Row.Cells[i].Text = Evaluate(Dim.d(i+1))
So, basically, I need a way to change the call to my object properties depending on the index, but I don't know if it is possible. When index i=0, call Dim.d1, when index i=1 call Dim.d2, and so on until 50.
Any ideas?
This is what Arrays or Lists are for!
var dim = new string[50];
dim[0] = "Description A";
dim[1] = "Description B";
..// etc
for(var i=0;i<49;i++)
{
e.Row.Cells[i].Text = dim[i];
}
You can use methods in the System.Reflection namespace to do this. However, the answer is presented in order to answer the question - you should look at using some of the options suggested by other answerers e.g. use a List(Of String) or something similar.
Anyway, let's say you have a class:
Public Class Class1
Public Property d1 As String
Public Property d2 As String
Public Property d3 As String
End Class
And then, let's say you create an instance of that class and set its properties:
Dim obj As New Class1
obj.d1 = "Foo"
obj.d2 = "Bar"
obj.d3 = "Test"
If you then want to have a loop from 1 to 3, and access e.g. d1, d2, d2 etc then this is where you use Reflection:
For i As Integer = 1 To 3
Dim info As System.Reflection.PropertyInfo = obj.GetType().GetProperty("d" & i)
Dim val As String = info.GetValue(obj, Reflection.BindingFlags.GetProperty, Nothing, Nothing, Nothing)
Debug.Print(val.ToString)
Next
Will give you the output:
Foo
Bar
Test
Like Jamiec already posted, use an Array or List.
Where do you description labels come from?
If you have your descriptions in a comma separated string, here is the vb.net code:
dim descriptions as String = "Description A,Description B,Description C"
dim myArray as String() = descriptions.Split(cchar(","))
for i as Integer = 1 To myArray.Length
e.Row.Cells(i-1).Text = myArray(i)
Next

Is there a way to separate parts of a String that is separated by filter units?

I am writing this program where the output below is saved to a text file
Unresolved%Bob%NA%Smith%123%8
and opened up by a separate VB program. the text above is then made into a string
"Unresolved%Bob%NA%Smith%123%8" = String1
I now need to get each part separated by the % into it's own string. ex.
"Unresolved" = string1a
"Bob" = string1b
"NA" = string1c
and so on. So my question is, is this possible and how? or is there a better way?
With the String.Split method.
Dim input As String = "Unresolved%Bob%NA%Smith%123%8"
'make the string array
Dim output() As String = input.Split("%"c)
Now you have an array of all the parts.
Load them into memory - create an object to pars the data into:
Public Class Client
Public Property FirstName As String
Public Property MiddleName As String
Public Property LastName As String
Public Property Status As String
Public Property Value1 As String
Public Property Value2 As String
End Class
In method that parses the data:
Dim input As String = "Unresolved%Bob%NA%Smith%123%8"
'make the string array
Dim output() As String = input.Split("%"c)
Dim _client As New Client
_client.Status = output(0)
_client.FirstName = output(1)
_client.MiddleName = output(2)
_client.LastName = output(3)
_client.Value1 = output(4)
_client.Value2 = output(5)
After all that being said, you could parse lines from a file and add them into a collection if needed.

Unable to cast custom listviewitem class in vb.net

This is a followup question to another question I asked earlier. I thought I had everything I needed, but I'm running into another issue. I'm trying to use a custom listviewitem class that attaches additional information to a lisview item. Here is the class:
Public Class albumListViewItem
Inherits ListViewItem
Public hash As String
Public id As Integer
Public provider As String
Public providerID As String
Public providerURL As String
Public providerArtistID As String
Public albumName As String
Public albumType As String
Public numTracks As Integer
Public imageURLs() As String
Public genres() As String
Public styles() As String
Public label As String
Public year As String
Public country As String
Public rating As String
Public editorsPick As Boolean
Public sampleStreamURL As String
Public providerReview As String
End Class
When I try to cast a listviewitem to my custom class like this:
Dim albumItem As albumListViewItem = CType(lsvHidden.items.item(0), albumListViewItem)
I get the following error, "Unable to cast object of type 'System.Windows.Forms.ListViewItem' to type 'AudioMatic.albumListViewItem'."
What am I missing here?
From your previous question and this one, I think a better fit for your problem would be to use a regular ListViewItem and store the accessory information in ListViewItem.Tag
You can do
Dim listViewItem As New ListViewItem("SomeText")
Dim albumInfo As New albumListViewItem()
albumInfo.albumName = "SomeAlbum"
...
listViewItem.Tag = albumInfo
listView1.Items.Add(listViewItem)
and then retrieve it like this
Dim selectedItem As ListViewItem = listView1.SelectedItems(0).Item
Dim alubmInfo As albumListViewItem = TryCast(selectedItem.Tag, alubmListViewItem)
Dim albumName as String = albumInfo.albumName
see if this solution will work for you.
If you step through the code and watch the variable "lsvHidden.items.item(0)" you should be able to first tell if it is in fact of type "albumListViewItem" or something else. Are you sure it was albumListViewItem that was added to the list in the first place?
Some alternatives to what you are doing;
1. You can implement an object and store it in the tag of the ListViewItem.
2. The following article seems to describe another approach of adding Columns to the listview to allow storing extra information on the listview itself; http://www.codeproject.com/KB/list/ListViewExtendedItem.aspx
I can appreciate your situation as I would have expected that to work. And I can see advantages and reasons for doing it that way as well. Not sure if the code project sample is adaptable to what you need, so you'll need to review the concept.
The working code:
Public Class albumListViewItem
Inherits ListViewItem
Public hash As String
Public id As Integer
Public provider As String
Public providerID As String
Public providerURL As String
Public providerArtistID As String
Public albumName As String
Public albumType As String
Public numTracks As Integer
Public imageURLs() As String
Public genres() As String
Public styles() As String
Public label As String
Public year As String
Public country As String
Public rating As String
Public editorsPick As Boolean
Public sampleStreamURL As String
Public providerReview As String
End Class
Storing information using listviewitem.tag:
Dim listViewItem As New ListViewItem("SomeText")
Dim albumItem As New albumListViewItem
albumItem.albumName = "Test Album"
albumItem.id = "testid"
albumItem.Text = albumItem.albumName
albumItem.year = "2011"
albumItem.numTracks = 10
'....
listViewItem.Tag = albumItem
'add viewable items to listview
albumItem.SubItems.Add(albumItem.year)
albumItem.SubItems.Add(albumItem.numTracks)
'....
ListView1.Items.Add(albumItem)
Reading the information that was previously stored:
Dim albumInfo As albumListViewItem = CType(ListView1.SelectedItems(0), albumListViewItem)
Dim id as string = alumInfo.id