VB2005 Find/Search in a ListOf Structure - vb.net

VB2005: I've been looking for several hours on a good example and i found some but unfortunately they are for VB2008+. Im currently working in VB2005 so it seems like this was harder to do in that release.
I have a class for a point
Public Class cPoint
Public Speed As Integer
Public Alt As Integer
Public Status As String = ""
Public Err As String = ""
End Class
I populate a list of points with MyPoints=List(of cPoint). Now all i need to do is find the first match with a supplied speed and alt. I tried
Dim p As cPoint = MyPoints.Find(Function(item As cPoint) item.Speed = 85)
But that doesn't work in VB2005 much less work with more than 1 filter. I just cant seem to find a good example that works in VB2005. I could iterate through the list but its kind of big and not very efficient. Any tips on how i can do this in VB2005?
~agp

VB.Net 2005 lacks lambda support hence that style of query won't work. The simplest version that will is to manually iterate with a For Each loop.
My p As cPoint = Nothing
For Each item in MyPoints
If item.Speed = 85 Then
p = item
Exit For
End If
Next

Related

devExpress searchLookupEdit not setting value

I have multiple searchLookupEdit controls on a form. All but one are working.
here is the code to populate one that is working
Private _list As sbList
_list = ag.ListByName("RMATroubleCode")
sluTroubleCode.Properties.DataSource = _list.Items
sluTroubleCode.EditValue = _item.TroubleCode.Value
here is the one that will not work
Private Sub populate_SKUOrdered(ByRef ctl As SearchLookUpEdit)
Dim ag As New cpAssemblyQuery()
Dim l As List(Of cpAssembly) = ag.TopLevelAssemblies()
ctl.Properties.DataSource = l
End Sub
populate_SKUOrdered(sluCreateSKUOrdered)
sluCreateSKUOrdered.EditValue = _item.SKU_OriginallyOrdered.Value
I have verified that sluCreateSKUOrdered has a DisplayMember and ValueMember property set correctly. They are both set to "SKU" so that is pretty simple.
The dropdown does contain the expected # of items.
The SKU i am trying to set is 'RM45'. So thats pretty simple to verify. It is not a case of trailing or leading spaces...
Are there some trouble shooting steps I can take? I am confident this is a case of simple oversight, but I can not find anything on DevExpress to help me resolve this issue.
I am using a ORM, it is necessary to use the following code
ctl.Properties.DisplayMember = "SKU.Value"
ctl.Properties.ValueMember = "SKU.Value"
The other controls were using interfaces, or simpler objects, so there was not a good analogy there.

Car Database Loop

This code doesn't seem to be working. I don't really know what loop to use to get it too add the information the user puts into the machine to print again.
The aim of this is for the user to either pick:
to print a menu that they have typed in an earlier database. If they haven't typed anything into the database, then it should be blank
Should let the user enter information into the database (What I am mostly struggling with) and do error checking so that it tells them if they have entered a number when they should have entered a letter and
To end the program.
When I do (2) It lets me type but it doesn't recall the information back in the database. Also it needs a number (4) which should return to the main menu. I think this is where the loops come in but I don't know which one to use.
Here is the code:
Structure Cars
Public carmake As String
Public carmodel As String
Public caryear As Integer
Public carlicence As String
End Structure
Module Module1
Sub Main()
Dim userchoice
Console.WriteLine("Choose weather to open the database(1), print it (2) or end (3)")
userchoice = Console.ReadLine()
If userchoice = 1 Then
Dim cardatabase(4) As Cars
Console.WriteLine("This will allow you to view the database.")
Console.WriteLine("Please enter the car make,licence,model and year")
cardatabase(1).carmake = Console.ReadLine()
cardatabase(1).carlicence = Console.ReadLine()
cardatabase(1).carmodel = Console.ReadLine()
cardatabase(1).caryear = Console.ReadLine()
ElseIf userchoice = 2 Then
Console.WriteLine("The database is,")
ElseIf userchoice = 3 Then
Console.WriteLine("Thanks for using this program.")
End If
Console.ReadLine()
End Sub
End Module
You have several issue with this code. Here's what I would recommend:
You need some sort of looping structure such as a While loop. Your test condition could be the userchoice variable.
In your If statements, you need to check if userchoice is equal to a string value instead of an integer value. So the line If userchoice = 1 Then should actually be If userchoice = "1" Then.
The cardatabase array should be declared outside of the loop. When you declare it in the loop, it will keep re-creating the array instead of adding more items to it.
Your Cars structure needs to be inside the Module Module1 block.
Since you don't know how many times the user may want to add a new car before they quit, I'd recommend using a List instead of an Array. Lists allow for easy dynamic re-sizing.
You need an integer variable to track the number of cars entered and use this as an index for your cardatabase collection.
Array/List indexes start with 0.
Cars should probably be a class instead of a structure. Also, it should be named Car. It's a single structure (or class, if you change it). The array itself should be called cars as it is a collection of a multiple Car structures (or objects if you change it to a class).
I was going to write example code here to demonstrate my points but it would be nearly an entire re-write and this would not help you understand why I made the changes I did.
My best advice to you is to go back through your book or tutorials that you read previously to get to this point and really try to understand what they are saying. If you don't get the concept, look it up elsewhere until you do.
Disclaimer: My recommendations are not comprehensive. I stopped examining your code when I identified all of the above issues right off the bat.

Dynamic creation of class instances

Is there any way to create class instances on the fly and refer to them later? I have a class with various methods and properties designed to hold & calculate product data. I'd like the application to be able to handle as many individual products as the user needs. This code clearly won't work but it should give you an idea of what I'm asking:
For x = 1 To howEverMany
Dim product_ & x.ToString() As New myProductClass
Next x
I appreciate this may not be the best approach (I should probably use lists or arrays to hold the product data) but I'm curious as to whether this is possible from a technical standpoint. I'm using VB.Net but answers in any .Net language will be welcome. Thanks.
Store them in a List(Of T). Then you can LINQ one or more objects in the list.
'class level
Private Products As New List(Of myProductClass)
'place where you load them
For x As Integer = 1 To howEverMany
Dim myc As New myProductClass
mpc.Id = x
'set other properties as needed
Products.Add(mpc)
Next
Get one by it's Id:
Dim mpc4 = (From p In Products Where p.Id = 4).FirstOrDefault
If Not mpc4 Is Nothing Then
'object exists
End If
You can make other similar queries on your List as well.

Visual Studio (List.Count - 1) = List.Count

I've been trying around for a bit now and I'm totally confused why my program behaves this way.
I tried to search on google and here but maybe I just have trouble phrasing my question right or I'm the only one with this problem.
Anyways, so here's my problem:
Protected myList as List(Of CustomClass) = new List(Of CustomClass)
Public Sub mySub()
Dim ListCount as Integer = myList.Count
ListCount = ListCount - 1
For i As Integer = 0 To ListCount Step 1
If myList(i).MyStatus = FMyStatus Then
myList(i).MyFunction.Invoke()
myList.RemoveAt(i)
End If
Next
End Sub
This throws me an exception all the time that i is larger than the myList-index.
The problem is that ListCount even though I reduced it by 1 still stays the same, it's not possible to reduce it by 1.
But if I for example reduce it by 2, it's totally fine and does it stuff.
Is this common? Did I miss on something? I already tried to find something on the msdn website for the List.Count Property but wasn't able to find something that explains me why I can't reduce the value by 1.
Hope you guys can enlighten me.
Cheers.
You need to loop backwards to accomplish this:
For i As Integer = myList.Count - 1 To 0 Step -1
If you go the other way, when you remove an item, the list becomes smaller, but you are still trying to go to the end of the original list.

DNN Dal+ - retrieve individual info class collection items (vb.NET)

I can't seem to find any answers that work. Here's the setup:
Info class:
Public Class ProductStageInfo
Private _ProductNumber As String
Private _ProductReference As String
Public Sub New()
End Sub
Public Property ProductNumber() As String
Get
Return _ProductNumber
End Get
Set(ByVal Value As String)
_ProductNumber = Value
End Set
End Property
End Class
and so on; I have four class declarations in the info class, the one above has fifteen different items - product number, product reference, product name, and so forth. The other's are catalogue classifications, which 'stage' of production the product is in, quality assurance questions; etc.
Then in the Controller class for DNN, I have those various info classes filled via queries to the DB DNN was deployed on; example:
Public Shared Function LoadStages(ByVal ProductNumber As String) As List(Of ProductStageInfo)
Return CBO.FillCollection(Of ProductStageInfo)(CType(DataProvider.Instance().ExecuteReader("Product_LoadStages", ProductNumber), IDataReader))
End Function
and everything works so far, I can fill a datalist using <%# DataBinder.Eval(Container.DataItem, "ProductNumber" %> and in code behind:
Dim ProductStageList As List(Of ProductStageInfo)
ProductStageList = ProductController.LoadStages(ProductNumber)
ProductStageDataList.DataSource = ProductStageList
ProductStageDataList.DataBind()
so far, so good...
but now I need to allow individuals to 'create' stages, and one of the business reqs' is that people shouldn't be able to create, for example, a delivery stage before a packaging stage.
So, how do I go about 'finding' a product number, product reference, stage number, within a collection? I thought I could fill the collection with all the stages of a certain product number, and then do an if/then stage = 0 found, stage > 5 found, etc.
If ProductStageList.Contains(strProductNumber) then
end if
gives error value of type string cannot be converted to namespace.ProductStageInfo; same thing for ProductStageList.Find...
maybe I just don't understand the whole collection/index/thing. All the examples I've found are regarding single dimension collections - 'how to find name within this collection', and the responses use strings to search through them, but somehow the Info class is being treated differently, and I'm not sure how to translate this...
any hints, tips, advice, tutorials.... appreciate it :)
thanks!
Pretty sure I just found the answer by reviewing another module; basically I need to create an empty object instead of a list object of the same class and use the two to iterate through using for/each, etc.
Dim objStages As ProductStagesInfo
Dim intStages, StageSelected As Integer
Dim intStageOption As Integer = -1
Dim blnValid As Boolean = True
Dim ProductChosen As String = lblStagesCNHeader.Text
Dim ProductStageList As List(Of ProductStagesInfo) = ProductController.LoadStages(ProductChosenNumber)
For intStages = 0 To StageList.Count - 1
objStages = StageList(intStages)
intStageOption += 1
Select objStages.StageSetNumber
Case "0"
Next
objStages._ provides me the ability to get the data I needed to do the business logic
<.<
seems so simple once you see it, wish I could just store it all in my brain
blah!