I am trying to develop a vb programe where i am using List to store data. I want to use all datas saved in that list from another class.
In Class 1, i am trying to include a method that will return my list. And form class 2, i will call that method and get that list.
But code isnt working.
Can anyone plz help. ! ! !
Thanks.
For Example.
In class one, Name- Add.vb' My thie method was supposed to return list.
Public Property getList() As List(Of Car)
Get
Return CarDetails
End Get
Set(ByVal value As List(Of Car))
End Set
End Property
and In class 2.
Private addobject As Add
Private details As New List(Of Car)
addobject = New Add()
details = addobject.getList()
But I am not getting the list.
You want to create an external class, or class library to the get the list data, and have both forms call that function.
I am not sure this is the problem you are facing, but the getlist in the Add class is a property, not a method. This means that you should not have the trailing paranthesis when calling it. You can change your code into this:
' create a new instance of Add '
Dim addobject As New Add()
' get the list from the property '
Dim details As List(Of Car) = addobject.getlist
Related
I have a form that alters the content of a class within a list box. The information is updated correctly, but my ToString override on my object doesn't refresh - meaning the old ToString doesn't change. How would I fix this?
Here's my object:
Public Class Destination
Public strDestinationName As String
Public strAddress As String
Public intQuality As Integer
Public intPrice As Integer
Public Overrides Function ToString() As String
Return strDestinationName
End Function
End Class
Here's the code where it should be updated
Dim selectedDestination As Destination
selectedDestination = CType(ListForm.lbNames.SelectedItem, Destination)
selectedDestination.strDestinationName = tbName.Text
selectedDestination.strAddress = tbAddress.Text
selectedDestination.intPrice = cbPrice.SelectedIndex
selectedDestination.intQuality = cbQuality.SelectedIndex
Me.Close()
Regardless of how you add items to a ListBox, it is the ListBox that actually displays the data. In your case, it appears that you are adding Destination objects to the ListBox somehow, given that the SelectedItem is a Destination object. Given that you have written that ToString method, you are presumably relying on that to produce the text that the ListBox displays for each item. You are now expecting to be able to change the value of the strDestinationName field of one of the items and have the ListBox reflect that change. How exactly do you think that is going to happen?
The ToString method has to be called in order to get the new value and who do you think is going to call it? It would be the ListBox that calls it because it is the ListBox that displays the result. When you change that field, you are expecting the ListBox to call your ToString method but why would it do that? What reason has the ListBox got to call that method? It has no knowledge of the change you made so why would it think that it has to get new data?
The solution to your problem is to change your code in some way to notify the ListBox that data has changed so that it knows that it needs to get that new data and display it. There are multiple ways that you could do that.
The simplest option would be to bind your data to the ListBox via a BindingSource and then, when you modify an item, call the ResetItem method or similar of the BindingSource. That will raise an event that is handled by the ListBox and the ListBox then knows that it needs to refresh the data for that item. That is what will prompt the ListBox to call your ToString method and get the new data to display. You would add the BindingSource to the form in the designer and then do the binding where you are currently adding the items, e.g.
Dim destinations As New List(Of Destination)
For i = 1 To 10
Dim d As New Destination
d.strDestinationName = "Destination " & i
destinations.Add(d)
Next
destinationBindingSource.DataSource = destinations
destinationListBox.DataSource = destinationBindingSource
The modification would look something like this:
Dim selectedDestination = DirectCast(destinationBindingSource.Current, Destination)
selectedDestination.strDestinationName = "New Destination"
destinationBindingSource.ResetCurrentItem()
The Current property returns the item currently selected in the bound UI and the ResetCurrentItem method notifies the bound UI to refresh the display of that item.
This is really not the best way to go about it though, given that you have control over the item type. What you ought to do is implement the type using properties rather than fields, get rid of the ToString method that only returns the value of one property and then add a change event to that property:
Public Class Destination
Private _destinationName As String
Public Property DestinationName As String
Get
Return _destinationName
End Get
Set(value As String)
If _destinationName <> value Then
_destinationName = value
OnDestinationNameChanged(EventArgs.Empty)
End If
End Set
End Property
Public Property Address As String
Public Property Quality As Integer
Public Property Price As Integer
Public Event DestinationNameChanged As EventHandler
Protected Overridable Sub OnDestinationNameChanged(e As EventArgs)
RaiseEvent DestinationNameChanged(Me, e)
End Sub
End Class
You can now bind a list of Destination objects directly and specify any of those properties as the DisplayMember to have that property value displayed:
Dim destinations As New List(Of Destination)
For i = 1 To 10
Dim d As New Destination
d.strDestinationName = "Destination " & i
destinations.Add(d)
Next
destinationListBox.DisplayMember = "DestinationName"
destinationListBox.DataSource = destinations
You don't need the ToString method because the DisplayMember specifies that the value of the property with that name should be displayed. When you modify the value of the DestinationName property of an item, it will raise the DestinationNameChanged event and that will notify the ListBox that it needs to refresh the display for that item, so you don't need any additional code to make the ListBox update.
That's fine if you only plan to modify existing items. There's still a problem if you want to add and/or remove items after binding though. The List(Of T) class that is used to bind the items to the control in this example does not have any events to notify the control of changes to the list like that. In that case, you can use a BindingSource again if you want. If you add and remove items via the BindingSource then it will raise that appropriate events and the ListBox will update. If you wanted to add and remove via the underlying list then you'd have to call an appropriate method of the BindingSource when you made a change.
An alternative would be to use a BindingList(Of Destination) instead of a List(Of Destination). As the name suggests, the BindingList(Of T) class is made for binding, so it will automatically raise the appropriate events when the list changes to enable the UI to update without extra code from you. Using the combination of property change events in your item class and a BindingList(Of T), you can add, edit and remove items in the bound list and the UI will reflect those changes automatically.
I got a form which declared an array in public,
Public requestedqueue() As Integer
I would like to pass the array to form3 and perform other calculations there, but how?
I tried doing(at a new form):
public newrequest() As Integer
newrequest = form2.requestedqueue
I tried to show it at a new form by doing:
TextBox1.Text = = String.Join(",",form2.newrequest)
But whenever I run into form3 it would say the newrequest is null.
But it shows as an array in form2, Im so confused.
I'm not sure what you mean by
But it shows as an array in form2
but newrequest will be Nothing because you have set it equal to the value of requestedqueue which is Nothing until you populate the array with some values.
If you had Public requestedqueue() As Integer = {1, 2, 3} then you would not encounter the error.
One way to pass data to a form is to add a property to the second form.
Lets say you have a form called Form3 and this is your form's code. In the code you will need to declare an array of integer to hold the passed data, and also declare a public property so you have a way of passing the array
Public Class Form3
Dim requestedqueue() As Integer
Public Property ArrayParameter As Integer()
Get
Return arrayData
End Get
Set(value() As Integer)
arrayData = value
End Set
End Property
End Class
Then, to pass the data from Form1, in form1, you would simply use
Form3.ArrayParameter=requestedqueue()
to set the parameter.
and if you wish, you can show the form as normal, or if the form is already visible, you can process the code using button clicks etc.
If you want to process the data in an already open form immediately without any user interaction, you can write a Procedure that does the processing and include that in the Set portion of your property.
For example. If you want to add all the elements of the array to a ListBox called ListBox1 in Form3, you could write a procedure like this..
Private Sub AddDataToListbox()
ListBox1.Items.Clear()
For Each item As Integer In requestedqueue
ListBox1.Items.Add(item)
Next
End Sub
and change your Form3.ArrayParameter code to this
Public Property ArrayParameter As Integer()
Get
Return requestedqueue
End Get
Set(value() As Integer)
requestedqueue = value
AddDataToListbox()
End Set
End Property
within webservce MyService.
DocumentList Class containing properties id, name -- Class is Serializable
Public Function getDocList(args...) as List(Of DocumentList)
'code to add to documentlist
'no issue here...
End Function
Within project.
Dim thisService as New MyService
Sub Main
Dim docList as New List(of DocumentList)
docList = thisService.getDocList(args...)
End Sub
I get error - cannot convert -1 dimensional array to generic list of DocumentList.
If I consume DocumentList within the service, no issues. I can iterate with for each. But, within the project, which calls the service, I cannot consume DocumentList.
I have set the service to pass a string, and this is possible. Only the List(Of DocumentList) is an issue.I think I may need reflection, or a proper understanding of collections. Not sure.
Thank you.
Dim docList as DocumentList()
Cannot return a complex type, had to set my DocumentList as an array in the project calling the webservice.
http://www.asquestion.com/question/60333227137608045/Problem-returning-a-collection-from-a-web-service
I'm triying to build a language dictionary in VB NET in order to be able to have several languages in the application.
This dictionary has a init method that loads from database the entire dictionary and stores it in memory.
In the rest of the classes of the project, I added the reference and I can use directly the object without create a new instance because their methods are shared.
My question is how i have to load the dictionary content in order that the rest of classes only accessing to Language.GetWord method obtains the properly record of the collection, same way as My.Settings class.
When My.Settings is used from any class the values are loaded. I'm looking for the same effect.
This is the code of the class:
Public Class LanguageProvider
Private Shared CurrentLanguage As Collection
Public Shared ReadOnly Property GetWord(ByVal Label As String)
Get
Try
Return CurrentLanguage.Item(Label)
Catch ex As Exception
Return Label
End Try
End Get
End Property
Public Shared Sub LoadLanguage(ByVal CultureId As Integer)
Dim SQLController As New SqlDataAccessController
SQLController.SQLSentence = "SELECT DNF_CULTURE_LANGUAGE_LABELS.LABEL, DNF_CULTURE_LANGUAGE_LABELS.VALUE FROM DNF_CULTURE_LANGUAGE_LABELS WHERE DNF_CULTURE_LANGUAGE_LABELS.CULTUREID = " & CultureId & " ORDER BY LABEL;"
SQLController.OpenConnection()
Dim dr As SqlClient.SqlDataReader
dr = SQLController.GetData()
CurrentLanguage = New Collection
While dr.Read()
CurrentLanguage.Add(dr.Item("Value"), dr.Item("Label"))
End While
dr.Close()
SQLController.CloseConnection()
End Sub
End Class
Function LoadLanguage must be called when the application loads, in order to access once to database.
After that property GetWord must access to the collection with the words and return the result. The problem is that the instances are not the same and the dictionary is not loaded when a class uses it.
Thanks.
Something along the lines of
Public Shared ReadOnly Property GetWord(ByVal Label As String)
Get
Try
If CurrentLanguage Is Nothing then
LoadLanguage(theDefaultCultureIdYouWant)
' Now CurrentLanguage is not Nothing any more,
' so LoadLanguage won't be called again
end if
Return CurrentLanguage.Item(Label)
Catch ex As Exception
Return Label
End Try
End Get
End Property
Of course, you have to provide a value for theDefaultCultureIdYouWant, depends on what you want to happen if the user just calls GetWord without explicitly mentioning a specific culture.
I am trying to figure out how to set the value of the text that is displayed for each item of a list control, such as a checkbox list, but really this applies to most list controls, not just the checklistbox control.
I have a checklistbox control,
Friend WithEvents clstTasks As System.Windows.Forms.CheckedListBox
that I usually want to populate with a list of task names. I call the add method to add a Task object to the list. I know that if I override the ToString method, whatever value is returned by that function will be displayed as the text for the list item.
However, in rare situations, I want to display something else other than just the name. For example, perhaps I want to display the name and the value of another property, such as the value of the Boolean property "Optional" shown in parenthesis following the name.
What is the best way to do this?
The best that I can think of is to define a property which is set in the GUI layer and then used by the ToString function to determine how it should behave when called. If the controlling property is set to one value, the ToString will return the Name, else it will return the Name followed by the value of the Optional flag. This seems a little disjoint a kludged to me.
The other alternative which seems a little overkill is to define a new class which inherits from Task, for example TaskOptional, which overrides the Tostring method on the base class. In this subclass, the ToString function would return the Name/Optional flag value. However, this, too, seems a little nuts to have to come up with a new class just to modify how the text is being displayed in the presentation layer. I feel that I should be able to control this in the presentation layer without changing the business object or creating a new derived object.
What is the best way to accomplish this?
For Each CurrentTask As Task In _MasterTaskList
clstTasks.Items.Add(CurrentTask, True)
Next
Public Class Task
Private _Name As String
Private _Optional As Boolean
Public Sub New (name As String, optional As Boolean)
_Name = name
End Sub
Public Overrides Function ToString() As String
Return _Name
End If
End Function
End Class
You can set the DisplayMember property of your CheckedListBox to the name of one of your custom class' property.
Let's say you create a property like the following:
Public ReadOnly Property NameOptional() As String
Return _Name & " (" & _Optional & ")"
End Property
then you can set the display member like this:
clstTasks.DisplayMember = "NameOptional"
When you set a display member, this property will be displayed instead of the ToString value.
You could do the following
Public Overrides Function ToString() As String
Return string.Format("{0}{1}", _Name, IIF(_Optional, " (Optional)", ""))
End Function
EDIT: You will have to set the value of _optional in the constructor, which is missing in the code you have provided.