Adding member to list overides previous members - vb.net

I need to read from an Excel file a list of dogs with their birth date, and store them in a list. The read works well, and every time I add a new dog to the list, the list count grows. So far so good.
The problem is that when I add the 2nd record, the first record is replaced by the first record content, so I have two identical records. And so on with every new record. In the end, I have many same records - all with the value of the last dog.
The dog class is:
Public Class DogClass
Public Name As String
Public Dob As Date
Public Age As Integer
Public Sex As String
Public Sub setDogName(ByVal _name As String)
Name = _name
End Sub
Public Sub setDogDOB(ByVal _dob As Date)
Dob = _dob
End Sub
Public Sub setDogAge(ByVal _age As String)
Age = _age
End Sub
Public Sub setDogSex(ByVal _sex As String)
Sex = _sex
End Sub
End Class
The class of the list is:
Public Class DogsListClass
Public dogsList As New List(Of DogClass)
Public Function DodgsCnt() As Integer
DodgsCnt = dogsList.Count()
End Function
Public Function DogExsists(_dogName As String) As Boolean
Dim res As Boolean = False
For Each item As DogClass In dogsList
If item.Name = _dogName Then
res = True
End If
Next
Return res
End Function
Public Sub AddDog(_dog As DogClass)
dogsList.Add(_dog)
End Sub
End Class
The calling:
Dim tdog As New DogClass
Dim DogsList As New DogsListClass
Do
tdog.setDogName(MyExcel.Cells(row_cnt, col_cnt).text)
tdog.setDogDOB(MyExcel.Cells(row_cnt, col_cnt + 1).value)
DogsList.AddDog(tdog)
Loop
Any idea why the records are being overridden?

DogsList variable must be declared outside the Do...Loop, otherwise you are creating a "New" DogsList at each iteration and, based on this code, you should end up having one single item in the collection, not many.
Also, declaring DogsList in the loop, prevents you from using it outside in the rest of your code.

Related

Delete duplicates from list

I have the following class :
Public Class titlesclass
Public Property Link As String
Public Property Title As String
Public Function Clear()
Link.Distinct().ToArray()
Title.Distinct().ToArray()
End Function
End Class
And the following code :
For Each title As Match In (New Regex(pattern).Matches(content)) 'Since you are only pulling a few strings, I thought a regex would be better.
Dim letitre As New titlesclass
letitre.Link = title.Groups("Data").Value
letitre.Title = title.Groups("Dataa").Value
lestitres.Add(letitre)
'tempTitles2.Add(title.Groups("Dataa").Value)
Next
I tried to delete the duplicated strings using the simple way
Dim titles2 = lestitres.Distinct().ToArray()
And calling the class function :
lestitres.Clear()
But the both propositions didn't work , i know that i'm missing something very simple but still can't find what it is
Easier to use a class that already implements IComparable:
Dim query = From title In Regex.Matches(content, pattern).Cast(Of Match)
Select Tuple.Create(title.Groups("Data").Value, title.Groups("Dataa").Value)
For Each letitre In query.Distinct
Debug.Print(letitre.Item1 & ", " & letitre.Item2)
Next
or Anonymous Types:
Dim query = From title In Regex.Matches(content, pattern).Cast(Of Match)
Select New With {Key .Link = title.Groups("Data").Value,
Key .Title = title.Groups("Dataa").Value}
For Each letitre In query.Distinct
Debug.Print(letitre.Link & ", " & letitre.Title)
Next
Ok, Since I notice you are using a ClassHere is one option you can do in order to not add duplicate items to your List within a class.I'm using a console Application to write this example, it shouldn't be too hard to understand and convert to a Windows Form Application if need be.
Module Module1
Sub Main()
Dim titlesClass = New Titles_Class()
titlesClass.addNewTitle("myTitle") ''adds successfully
titlesClass.addNewTitle("myTitle") '' doesn't add
End Sub
Public Class Titles_Class
Private Property Title() As String
Private Property TitleArray() As List(Of String)
Public Sub New()
TitleArray = New List(Of String)()
End Sub
Public Sub addNewTitle(title As String)
Dim added = False
If Not taken(title) Then
Me.TitleArray.Add(title)
added = True
End If
Console.WriteLine(String.Format("{0}", If(added, $"{title} has been added", $"{title} already exists")))
End Sub
Private Function taken(item As String) As Boolean
Dim foundItem As Boolean = False
If Not String.IsNullOrEmpty(item) Then
foundItem = Me.TitleArray.Any(Function(c) -1 < c.IndexOf(item))
End If
Return foundItem
End Function
End Class
End Module
Another option would be to use a HashSet, It will never add a duplicate item, so even if you add an item with the same value, it wont add it and wont throw an error
Sub Main()
Dim titlesClass = New HashSet(Of String)
titlesClass.Add("myTitle") ''adds successfully
titlesClass.Add("myTitle") '' doesn't add
For Each title As String In titlesClass
Console.WriteLine(title)
Next
End Sub
With all of that aside, have you thought about using a Dictionary so that you could have the title as the key and the link as the value, that would be another way you could not have a list (dictionary) contain duplicate items

"Expression of type "" is not queryable" error

I'm attempting to get an assignment done for class and have hit this error:
Error 1 Expression of type 'Homework_Code_Examples.Form1.State' is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider. D:\Users\Stryke\Documents\Visual Studio 2013\Projects\Homework Code Examples\Homework Code Examples\Form1.vb 11 32 Homework Code Examples
I've never encountered this before and searching via Google, MSDN and these forums all tell me that I must target the correct .NET framework for my program, however I am completely lost as to which framework I should target as the recommended answers all give the same error upon being recompiled. Currently, I am using .NET Framework 4.5 in Visual Studio 2013.
I don't know if it helps at all or not, but here is my code so that you may see what it is I'm trying to achieve...
Public Class Form1
Dim states As State
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
states = New State()
Dim query = From st In states
Let name = states.Name
Let density = states.Density()
Order By density Descending
Select name, density
End Sub
Private Sub stateInfo_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles stateInfo.CellContentClick
End Sub
Class State
Private m_name, m_abbr, m_date As String
Private m_area, m_pop As Integer
Public Property Name()
Get
Return m_name
End Get
Set(value)
m_name = value
End Set
End Property
Public Property Abbreviation()
Get
Return m_abbr
End Get
Set(value)
m_abbr = value
End Set
End Property
Public Property joinDate()
Get
Return m_date
End Get
Set(value)
m_date = value
End Set
End Property
Public Property landArea
Get
Return m_area
End Get
Set(value)
m_area = value
End Set
End Property
Public Property Population
Get
Return m_pop
End Get
Set(value)
m_pop = value
End Set
End Property
Public Function Density()
Dim popDensity As Integer
popDensity = Population / landArea
Return popDensity
End Function
End Class
End Class
Linq needs to iterate through multiple objects. In your case, it can be list or collection of State class.
Example with List(Of Type)
Change line
Dim states As State
to
Dim states As List(Of State)
and
states = New State()
into
states = New List(Of State)
Example with array
Change line
Dim states As State
to
Dim states() As State
and
states = New State()
into
ReDim states(10) 'or use a variable inside
Here's a sample of how to populate your data. I also fix some error in your query. Compare your query to mine to see what I changed.
' initialize a list with some values
Dim states = New List(Of State) From {
New State With {.Name = "Ohio", .Population = 10000, .landArea = 10},
New State With {.Name = "Texas", .Population = 20000, .landArea = 300},
New State With {.Name = "Florida", .Population = 5000, .landArea = 1000}
}
' query the list
Dim query = From st In states
Let name = st.Name
Let density = st.Density()
Order By density Descending
Select name, density
I also improve your State class to use proper data type instead of object.
Class State
Private m_name, m_abbr, m_date As String
Private m_area, m_pop As Integer
Public Property Name As String
Get
Return m_name
End Get
Set (value As String)
m_name = value
End Set
End Property
Public Property Abbreviation As String
Get
Return m_abbr
End Get
Set(value As String)
m_abbr = value
End Set
End Property
Public Property joinDate As String
Get
Return m_date
End Get
Set(value As String)
m_date = value
End Set
End Property
Public Property landArea As Integer
Get
Return m_area
End Get
Set(value As Integer)
m_area = value
End Set
End Property
Public Property Population As Integer
Get
Return m_pop
End Get
Set(value As Integer)
m_pop = value
End Set
End Property
Public Function Density() As Double
Dim popDensity As Double
popDensity = Population / landArea
Return popDensity
End Function
End Class

Vb conceptual understanding of creating objects within a class

I was wondering if you could help me understand how to create a Card object (with a value and suit) and use 52 of those cards to make an object called deck.
I have created my card class how do I initialize every card inside the deck class? Should I do it one by one? How do I link all those cards to one deck.
Thanks
As it happen I did read your previous question earlier today.
First, create a suit enum.
Public Enum Suit As Integer
Hearts = 1
Diamonds = 2
Clovers = 3
Spades = 4
End Enum
Then create the card class. Notice that the properties are read only as a card never changes its value. (Maybe not true if you're a magician)
Public Class Card
Public Sub New(suit As Suit, value As Integer)
Me.m_suit = suit
Me.m_value = value
End Sub
Public ReadOnly Property Suit() As Suit
Get
Return Me.m_suit
End Get
End Property
Public ReadOnly Property Value() As Integer
Get
Return Me.m_value
End Get
End Property
Private m_suit As Suit
Private m_value As Integer
End Class
Finally, create the deck class and populate 52 cards.
Public Class Deck
Public Sub New()
Dim cards = New Card(52 - 1) {}
Dim num As Integer = 0
For s As Integer = 1 To 4
For v As Integer = 1 To 13
cards(num) = New Card(CType(s, Suit), v)
num += 1
Next
Next
Me.m_cards = New Collections.ObjectModel.ReadOnlyCollection(Of Card)(cards)
End Sub
Public ReadOnly Property Cards() As Collections.ObjectModel.ReadOnlyCollection(Of Card)
Get
Return Me.m_cards
End Get
End Property
Private ReadOnly m_cards As Collections.ObjectModel.ReadOnlyCollection(Of Card)
End Class
You need two Enumerations and two Classes:
Enumerations
CardFaceValue - with values ranging from Ace-10 (inclusive), Jack, Queen, King.
CardFaceType - with values Hearts, Spades, Clubs, Diamonds
Classes
Deck - Has one property to contain the collection of all cards
Cards - of Type Array of Cards, sized 52.
Card - Has two properties
CardFaceValue
CardFaceType
In the constructor of the Deck class run a loop within a loop. The outer loop will run for 4 times for each of the CardFaceType enumeration, and the inner loop will run for 13 times for cards 1-10, J, Q, K.
With these loops iterate through the enumeration values and add cards to your Deck.
This is just a quick draft of what I envision
You'll need the card class first.
Public Class Card
Private cSuit As String
Private cValue As Integer
Public Property suit() As String
Get
Return cSuit
End Get
Set(ByVal value As String)
cSuit = value
End Set
End Property
Public Property value() As Integer
Get
Return cValue
End Get
Set(ByVal value As Integer)
value = cValue
End Set
End Property
Public Sub New(ByVal TheSuit As String, ByVal TheValue As Integer)
cSuit = TheSuit
cValue = TheValue
End Sub
Then you can make a new object for each card and add it to the deck collection.
Dim Deck As New List(Of Card)
Dim Suit As String = "Spade"
Dim Value As Integer = 11
Dim AceOfSpades As New Card(Suit, Value)
Deck.Add(AceOfSpades)

VB: Problems with using variable from another class + what to do with not used interface`s functions

I have a problem with getting variable from another class and cannot understand what to do with interface`s functions which have already existed in another class.
What I have:
Form where clicking on a button I should see reversed string:
(I want to call pooraja.StringReverse which is below)
Private Sub btnPoora1_Click(sender As System.Object, e As System.EventArgs) _
Handles btnPoora1.Click
'Dim text As PrjTekstiPooraja.ITeisendused = New PrjTekstiPooraja.CtekstiPooraja
Dim text As PrjTekstiPooraja.ITeisendused = New PrjTekstiPooraja.CtekstiPooraja
Dim pooraja As PrjTekstiPooraja.ITeisendused = New PrjTekstiPooraja.CAlgrotimilinePooraja
text.strText = txtSisendTekst.Text
txtValjundTekst1.Text = pooraja.stringReverse
text.intStart = 1
text.intEnd = Len(txtSisendTekst.Text)
ascFSymbol.Text = text.ascFirstSymbol
ascLSymbol.Text = text.ascLastSymbol()
End Sub
CtekstiPooraja:
(Thiss class will be used to store data.Under data I mean strPooratavText. Data will be used in CAlgoritmilinePooraja)
Public Class CtekstiPooraja
Implements ITeisendused
Public intStartSymbol As Integer
Public intEndSymbol As Integer
Public strPooratavText As String
Private Property intEnd As Integer Implements ITeisendused.intEnd
Get
Return intEndSymbol
End Get
Set(ByVal value As Integer)
intEndSymbol = value
End Set
End Property
Private Property intStart As Integer Implements ITeisendused.intStart
Get
Return intStartSymbol
End Get
Set(ByVal value As Integer)
intStartSymbol = value
End Set
End Property
Public Function pooraText() As String Implements ITeisendused.pooraText
Return StrReverse(strPooratavText)
End Function
Public Property strText As String Implements ITeisendused.strText
Get
Return strPooratavText
End Get
Set(ByVal value As String)
strPooratavText = value
MsgBox(strPooratavText)
End Set
End Property
Public Sub teisendaText(ByRef strSisendText As String) Implements ITeisendused.teisendaText
strPooratavText = StrReverse(strSisendText)
End Sub
Public Function ascFirstSymbol() As String Implements ITeisendused.ascFirstSymbol
Return Asc(GetChar(strPooratavText, intStartSymbol))
End Function
Public Function ascLastSymbol() As String Implements ITeisendused.ascLastSymbol
Return Asc(GetChar(strPooratavText, intEndSymbol))
End Function
Public Function stringReverse() As String Implements ITeisendused.stringReverse
Return Nothing
End Function
End Class
CAlgrotimilinePooraja:
(This class will be called by form button. There I need to use stringReverse function with data from CtekstiPooraja. The problem is that everywhere is used the same interface and there is some functions and procedures from this interface which isnt necessary. I dont know what value should return these unused functions/procedures. Just using "return Nothing or return 0/ "" is bad idea, may be there is possible somehow referenceto to CTekstiPooraja functions/procedures variables")
Public Class CAlgrotimilinePooraja
Implements ITeisendused
Private x As New PrjTekstiPooraja.CtekstiPooraja
Public Function stringReverse() As String Implements ITeisendused.stringReverse
MsgBox(x.strPooratavText)
Dim i As Integer = 0
Dim j As Integer
Dim characters(j) As Char
Dim newString(j) As Char
characters = x.strPooratavText.ToCharArray()
newString = x.strPooratavText.ToCharArray()
Do While i <= j - 1
newString(i) = characters(j - 1)
newString(j - 1) = characters(i)
i += 1
j -= 1
Loop
Return newString
End Function
Public Function ascFirstSymbol() As String Implements ITeisendused.ascFirstSymbol
Return x.ascFirstSymbol()
End Function
Public Function ascLastSymbol() As String Implements ITeisendused.ascLastSymbol
Return Nothing
End Function
Public Property intEnd As Integer Implements ITeisendused.intEnd
Get
Return x.intEndSymbol
End Get
Set(ByVal value As Integer)
End Set
End Property
Public Property intStart As Integer Implements ITeisendused.intStart
Get
Return x.intStartSymbol
End Get
Set(ByVal value As Integer)
End Set
End Property
Public Function pooraText() As String Implements ITeisendused.pooraText
Return x.pooraText()
End Function
Public Property strText As String Implements ITeisendused.strText
Get
Return x.strPooratavText
End Get
Set(ByVal value As String)
End Set
End Property
Public Sub teisendaText(ByRef strSisendText As String) Implements ITeisendused.teisendaText
x.strPooratavText = StrReverse(strSisendText)
End Sub
End Class
MyInterface:
Public Interface ITeisendused
Property intStart As Integer
Property intEnd As Integer
Property strText As String
Function pooraText() As String
Function ascFirstSymbol() As String
Function ascLastSymbol() As String
Function stringReverse() As String
Sub teisendaText(ByRef strSisendText As String)
End Interface
I cannot understand how to get variable strPooratavText from CTekstiPooraja to CAlgrotimilinePooraja. Usually that instancewhich I create worked but not now. And I cannot understand what to do with already existed function and procedures in CAlgoritmilinePooraja when the same function and procedures has in another class. Maybe, it is possible to reference them somehow to existed functions/procedures in CTekstiPooraja? Could you explain me how to id, already tired to surf Internet to find a solution for it, have already try a lot.
Well, I think you have a fundamental problem with understanding interfaces. They describe data and behavior, it should be extremely rare to want to implement part of an interface.
That said, if you do want to implement part of an interface, instead of returning bogus data, throw an exception for behavior you don't implement.
Your specific problem is that CAlgoritmilinePooraja works on an instance of CtekstiPooraja, but it creates a new instance instead of using an existing one. Add
Sub New(incomingX as CtekstiPooraja)
x = incomingX
End Sub
to CAlgoritmilinePooraja. And then in your event, use....
Dim text As PrjTekstiPooraja.CtekstiPooraja = New PrjTekstiPooraja.CtekstiPooraja
text.strText = txtSisendTekst.Text
Dim pooraja As PrjTekstiPooraja.ITeisendused = New PrjTekstiPooraja.CAlgrotimilinePooraja(text)
That is the minimum change to your design that gets what you want to happen to happen but it's problably not what you should do. Other than implementing strReverse, CtekstiPooraja seems to be what you want, CAlgrotimilinePooraja looks to do just one thing, the actual string reversal.
I would move the implementation of strReverse into CtekstiPooraja, and then eliminate CAlgrotimilinePooraja.
PS I would try to stick to English for class names as well as functions and variables.

How to convert a string of key/value pairs to HashTable or Dictionary or?

In VB.NET, how can I convert the following string into some kind of key/value type such as a Hashtable, Dictionary, etc?
"Name=Fred;Birthday=19-June-1906;ID=12345"
I want to extract Birthday or ID without having to split the string into an array.
EDIT: I'd prefer not to split the string into an array in case the format of the string changes later. I don't have control over the string. What if someone switches the order around or adds another element?
I’m currently unable to test this, lacking a VB compiler, but the following solution should also work, and it has the advantage of not requiring an explicit loop. It uses the Linq method ToDictionary and two nested Split operations:
Dim s = "Name=Fred;Birthday=19-June-1906;ID=12345"
Dim d = s.Split(";"c).Select(Function (kvp) kvp.Split("="c)) _
.ToDictionary( _
Function (kvp) kvp(0), _
Function (kvp) kvp(1))
First, we split on the outer delimiter (i.e. the semi-colon). From the resulting array, we select by splitting again, this time on =. The resulting array of arrays is converted to a dictionary by specifying that the first item is to become the key and the second is to become the value (the identifier kvp stands for “key-value pair”).
Since I can’t check the exact VB syntax and the above may contain subtle errors, here is the equivalent C# code (tested for correctness):
var s = "Name=Fred;Birthday=19-June-1906;ID=12345";
var d = s.Split(';').Select(kvp => kvp.Split('='))
.ToDictionary(kvp => kvp[0], kvp => kvp[1]);
Not sure why you don't want to split it. If you're sure there won't be any extra = or ; then you could just do:
Dim s As String = "Name=Fred;Birthday=19-June-1906;ID=12345"
Dim d As New Dictionary(Of String, String)
For Each temp As String In s.Split(";"c)
Dim index As Int32 = temp.IndexOf("="c)
d.Add(temp.Substring(0, index), temp.Substring(index + 1))
Next
Which might not be beautiful, but is very easy to understand.
input.Split(";"c) returns an array of key/value:
{ "Name=Fred", "Birthday=19-June-1906" , "ID=12345" }
so pair.Split("="c) returns { "Name", "Fred" } etc
If you want an alternative to doing a String.Split; there is always Regular Expressions as an alternative:
Dim map As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim match As Match = Regex.Match("Name=Fred;Birthday=19-June-1906;ID=12345", "(?<Name>[^=]*)=(?<Value>[^;]*);?")
While (match.Success)
map.Add(match.Groups("Name").Value, match.Groups("Value").Value)
match = match.NextMatch()
End While
The regular expression itself could be beefed up to better handle whitespace between key/value's and pair's but you hopefully get the idea. This should only pass through the string once to build up a string dictionary of keys and values.
Dim persSeparator as string=";"
Dim keyValSeparator as string="=";
Dim allPersons As New Dictionary(Of String, Person)
Dim str As String = "Name=Fred;Birthday=19-June-1906;ID=12345"
Dim parts As New List(Of String)(str.Split(persSeparator.ToCharArray)) 'why dont want you to split this string??
Dim person As New Person
For Each part As String In parts
Dim keyValue() As String = part.Split(keyValSeparator.toCharArray())
Select Case keyValue(0).ToUpper
Case "ID"
person.ID = keyValue(1)
Case "NAME"
person.Name = keyValue(1)
Case "BIRTHDAY"
person.BirthDay= keyValue(1)
End Select
Next
If Not allPersons.ContainsKey(person.ID) Then
allPersons.Add(person.ID, person)
End If
Public Class Person
Private _name As String
Private _birthday As String
Private _id As String = String.Empty
Public Sub New()
End Sub
Public Sub New(ByVal id As String)
Me._id = id
End Sub
Public Sub New(ByVal id As String, ByVal name As String)
Me._id = id
Me._name = name
End Sub
Public Sub New(ByVal id As String, ByVal name As String, ByVal birthday As String)
Me._id = id
Me._name = name
Me._birthday = birthday
End Sub
Public Property ID() As String
Get
Return Me._id
End Get
Set(ByVal value As String)
Me._id = value
End Set
End Property
Public Property Name() As String
Get
Return Me._name
End Get
Set(ByVal value As String)
Me._name = value
End Set
End Property
Public Property BirthDay() As String
Get
Return Me._birthday
End Get
Set(ByVal value As String)
Me._birthday = value
End Set
End Property
Public Overrides Function Equals(ByVal obj As Object) As Boolean
If TypeOf obj Is Person AndAlso Not obj Is Nothing Then
Return String.Compare(Me._id, DirectCast(obj, Person).ID) = 0
Else : Return False
End If
End Function
End Class
If you were just wanting to extract the birthday and ID from the string and place as a value pair in some sort of dictionary, for simplicity I would use regular expressions and then a generic dictionary (of string, valuepair structure). Something like this:
Imports System.Text.RegularExpressions
Imports System.Collections.Generic
Sub Main()
Dim Person As New Dictionary(Of String, ValuePair)
Dim s As String = "Name=Fred;Birthday=19-June-1906;ID=12"
Dim r As Regex = New Regex("Name=(.*);Birthday=(.*);ID=(.*$)")
Dim m As Match = r.Match(s)
Person.Add(CStr(m.Groups(1).Value), _
New ValuePair(CDate(m.Groups(2).Value), CInt(m.Groups(3).Value)))
Console.WriteLine(Person("Fred").Birthday.ToString)
Console.WriteLine(Person("Fred").ID.ToString)
Console.Read()
End Sub
Friend Structure ValuePair
Private _birthday As Date
Private _ID As Int32
Public ReadOnly Property ID() As Int32
Get
Return _ID
End Get
End Property
Public ReadOnly Property Birthday() As Date
Get
Return _birthday
End Get
End Property
Sub New(ByVal Birthday As Date, ByVal ID As Int32)
_birthday = Birthday
_ID = ID
End Sub
End Structure