add a table of Structures to a listbox vb.net - vb.net

i have a structure in a module that i instanciate a table of its type
Public Structure Client
Public _nom As String
Public _prenom As String
Public _age As Integer
End Structure
Module Module1
Public TableauClient(0) As Client
End Module
that i need to populate nd resize every time i hit a certain button
Dim Dimension As Integer = 0
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TableauClient(Dimension)._nom = TextBox1.Text.ToString()
TableauClient(Dimension)._prenom = TextBox2.Text.ToString()
TableauClient(Dimension)._age = Val(TextBox3.Text)
Dimension += 1
ReDim TableauClient(Dimension)
End Sub
the problem is i need to fill a listbox with all the elements in the table when i hit another button but i don't even know where to start to do this, tried datasource or add item by item by using concatenations between the three fields but still couldn't get it right

you can still use the structure but, I think it will be easier to work with class. Instead of Array make a list
dim TableauClient as new List(of Client)
create toString inside your class / structure
Public Overrides Function ToString() As String
return _nom
End Function
and just add those into your listbox
listbox.items.add(TableauClient (0))

Related

How to enable multiple column for a listbox on VB.NET web application?

What i'm trying to do is to display two columns on a listbox so the user has more information.
The way i fill the listbox is by using a SqlDataSource with a custom query and then i attach that Data Source to the list box.
My problem is in the Data Source i can only pick two values one for the listbox display and the other values is to select the datafield value for the list box.
How can i display multiple column values from a SqlDataSource on a Listbox?
First create a Class for your data. I added a parameterized constructor so it would be easy to create a Coffee without separately setting properties. I also overrode the .ToString method so the ListBox would display what I want. If you really want columns you get into padding to a set length.
Public Class Coffee
Public Property Name As String
Public Property Type As String
Public Sub New(cofName As String, cofType As String)
Name = cofName
Type = cofType
End Sub
Public Overrides Function ToString() As String
Return $"{Name}, {Type}"
End Function
End Class
To fill the list, create a new coffee and add it to the list.
Private CoffeeList As New List(Of Coffee)
Private Sub FillCoffeeList()
Using cn As New SqlConnection(My.Settings.CoffeeConnection),
cmd As New SqlCommand("Select Top 10 Name, Type From Coffees;", cn)
cn.Open()
Using reader = cmd.ExecuteReader
While reader.Read
Dim c As New Coffee(reader.GetString(0), reader.GetString(1))
CoffeeList.Add(c)
End While
End Using
End Using
End Sub
Then in the Page.Load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
FillCoffeeList()
ListBox1.DataSource = CoffeeList
ListBox1.DataBind()
End If
End Sub
The ListBox calls .ToString on each Coffee object in CoffeeList and displays what we determined in the Class.

How to access a new instantiated class method from another function in another class?

I feel comfortable when instantiating a class, then using it within the same function, but now I need to instantiate a class from a toolbar button:
Dim pll As New Polyline()
Debug.WriteLine("TSB_Polyline_Click:: a new polyLine is born : " & pll.Count)
pll.AddPoint(0, 0)
And then I need to run the pll.AddPoint from my class method in another sub:
Public Sub MyEvent(sender as object, e as myEvent) Handles myEvent
Dim x as Double, y as Double
pll.AddPoint(x,y)
There I have my error (System.NullReferenceException: 'Object reference not set to an instance of an object.'), pll = Nothing (no error in my constructor, my class worked from the toolbar button_Click)
I tried to declare pll public and shared:
Public Shared pll As Polyline
And even to import it:
Imports oGIS.Polyline
Nothing works. My Class is instanciated in the first Sub (toolbar button), but somehow dies when leaving the Sub...
Is there any other solution that doing it using VB6 instead of VB.Net ?
I am a bit desesperate, I found no such topic anywhere on Google...
If you need to access a variable in more than one method then it should be declared outside all of them. It should be Private if you don't need to access it outside the type it's declared in and it should only be Shared if you specifically need all instances of the class accessing the same object. In your case, Private and not Shared is the obvious choice:
Private pll As Polyline
You now set that field in the first method and get it in the second.
I am guessing a partial look at your class would be something like this...
Public Class Polyline
Public ReadOnly Property CountOfPoints As Integer
Get
Return ListOfPoints.Count
End Get
End Property
Public Property ListOfPoints As New List(Of Point)
Public Sub AddPoint(x As Integer, y As Integer)
Dim p As New Point(x, y)
ListOfPoints.Add(p)
End Sub
End Class
Declare a class level (in this case the class is a Form) variable which can be seen and used by any method in your form.
Public Class Form1
Private LocalPolyLine As Polyline
'This is a local list, not part of the class
'It is a list of the number of instances of the class you have created
Private ListOfLines As New List(Of Polyline)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Pretend this is a button on your toolbar used to create a new line
'Each time you click this button you have a completely new line with no points
LocalPolyLine = New Polyline
'If you want to keep track of how many lines you have...
ListOfLines.Add(LocalPolyLine)
End Sub
Private Sub AddPoint(x As Integer, y As Integer)
Debug.Print($"Number of Lines is {ListOfLines.Count}")
Debug.Print($"Number of points on current line {LocalPolyLine.CountOfPoints}")
LocalPolyLine.AddPoint(x, y)
Debug.Print($"Number of points on current line {LocalPolyLine.CountOfPoints}")
End Sub
'To see all the points on the line...
Private Sub ViewPoints()
For Each p In LocalPolyLine.ListOfPoints
Debug.Print($"The coordinates of the point are {p.X},{p.Y}")
Next
End Sub
End Class

Assigning value of User Defined Class (UDC) to textbox

Im learning about UDC in vb.net and trying to get a very simple program to display the value of a UDC in a textbox my code follows below:
Structure carDriverInfo
Dim carMake As String
Dim driverName As String
End Structure
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim car As carDriverInfo
Dim driver As carDriverInfo
car.carMake = "Ford Fiesta"
driver.driverName = "J Hudgsons"
TextBox1.Text = car
TextBox2.Text = driver
End Sub
The problem is the compiler gives me the error that CarDriverInfo cannot get converted to string...
What am I doing wrong here?
Compiler doesn't know what variable to show. You can choose one value that will represent string output of your structure by adding following to your structure
Public Overrides Function ToString() As String
Return carName
End Function
But if you want to get different values from your structure you need to define your variables as properties and use those properties as you see fit like
car.carName

Vb.net Custom Class Property to lower case

I am trying to create a settings class.
The Property Test() is a list of strings.
When I add a string such as: t.test.Add("asasasAAAAA")
I want it to autmatically turn lowercase.
For some reason it is not. Any Ideas?
p.s.
using t.test.Add(("asasasAAAAA").ToLower) will not work for what I need.
Thank you.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim t As New Settings
t.test.Add("asasasAAAAA")
t.test.Add("aBBBBBAAAAA")
t.test.Add("CCCCCsasAAAAA")
End Sub
End Class
Public Class Settings
Private strtest As New List(Of String)
Public Property test() As List(Of String)
Get
Return strtest
End Get
Set(ByVal value As List(Of String))
For i As Integer = 0 To value.Count - 1
value(i) = value(i).ToLower
Next
strtest = value
End Set
End Property
End Class
ashakjs
That's the reason: set accessor of your property is actually never called.
when you use t.test.Add("asasasAAAAA") you are actually calling a get accessor, which returns a list, after that specified string is added to this list, so .ToLower function is never called.
Simple way to fix this:
Dim list as New List(Of String)
list.Add("asasasAAAAA")
list.Add("aBBBBBAAAAA")
list.Add("CCCCCsasAAAAA")
t.test = list
Alternatively, you can implement your own string list (easiest way - inherit from Collection (Of String)), which will automatically convert all added string to lower case.
What you are trying to do and what you are doing don't match. To do what you want, you need to create your own collection class extending the generic collection - or provide a custom method on your settings class which manually adjusts the provided string before adding it to the local (private) string collection.
For an example of the second option, remove the public property of the settings class which exposes the list of string and use a method like the following:
Public Sub Add(ByVal newProp As String)
strtest.Add(newProp.toLower())
End Sub

How can I make a list in a Repeater Section?

I need to create a repeater section that will show 4 columns - First Name, Last Name, a link based off of stored column data that says.
All the data plus some extra not being used is in a players profile. How do I link the data on the code-behind to the repeater control with the databinders?
I am using visual studio 2008, VB.NET for the code behind.
Have you considered using a DataGrid instead of a repeater?
Here's a bit of a breakdown on when to use each.
http://msdn.microsoft.com/en-us/library/aa479015.aspx
To more directly answer your question you'll need to set the Repeater's DataSource property to a DataView or an ArrayList. As such:
Sub Page_Load(Sender As Object, e As EventArgs)
If Not IsPostBack Then
Dim values As New ArrayList()
values.Add(New PositionData("Microsoft", "Msft"))
values.Add(New PositionData("Intel", "Intc"))
values.Add(New PositionData("Dell", "Dell"))
Repeater1.DataSource = values
Repeater1.DataBind()
Repeater2.DataSource = values
Repeater2.DataBind()
End If
End Sub
Public Class PositionData
Private myName As String
Private myTicker As String
Public Sub New(newName As String, newTicker As String)
Me.myName = newName
Me.myTicker = newTicker
End Sub
Public ReadOnly Property Name() As String
Get
Return myName
End Get
End Property
Public ReadOnly Property Ticker() As String
Get
Return myTicker
End Get
End Property
End Class