# Double Number in List of T

### Question

• How to find the maxima number but not inculding a double number,Exampel 99.

Result must 92 not 99.

thank.

```Public Class Form1

Private C_ As New List(Of String)
Private F_ As New List(Of String)
Private S_ As New List(Of String)

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

For i As Integer = 0 To C_.Count - 1
ListBox1.Items.Add(C_(i) & "   " & F_(i))
Next

For i As Integer = 0 To C_.Count - 1
TextBox1.AppendText(C_(i) & "   " & F_(i) & vbCrLf)
Next

End Sub

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
C_.Add("99")
C_.Add("99")
C_.Add("55")
C_.Add("92")
C_.Add("76")
C_.Add("53")
C_.Add("31")

F_.Add("102")
F_.Add("202")
F_.Add("205")
F_.Add("322")
F_.Add("504")
F_.Add("601")
F_.Add("306")

End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim Mx As Integer = C_.Max

For i As Integer = 0 To C_.Count - 1
If C_(i) = Mx Then TextBox2.AppendText(Mx & "   " & F_(i) & vbCrLf)
Next

End Sub
End Class```

• Edited by Tuesday, March 21, 2017 2:10 PM add
Tuesday, March 21, 2017 2:09 PM

### Answers

• Hello everybody

```Option Strict On

Public Class Form1

Private C_ As New List(Of String) From {"99", "87", "71", "97", "99"}
Private MAXIMUM As String

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MAXIMUM = (From c As String In C_
Where Integer.TryParse(c, New Integer) _
And c.Length = 2 _
And c(0) <> c(1)
Select c).Max
End Sub
End Class```

Luc

• Marked as answer by Wednesday, March 22, 2017 12:03 PM
Tuesday, March 21, 2017 4:50 PM
• mip,

Have a look at this. It's a bit crude but hopefully it'll do what you want:

```Option Strict On
Option Explicit On
Option Infer Off

Public Class Form1
Private Sub Form1_Load(sender As System.Object, _
e As System.EventArgs) _
Handles MyBase.Load

Dim C() As Integer = _
{99, 99, 55, 92, 76, 53, 31}

Dim tempList As New List(Of Integer)

For Each i As Integer In C
Dim s As String = i.ToString

If s.Length = 2 Then
If Not s(0) = s(1) Then
tempList.Add(i)
End If
Else
tempList.Add(i)
End If
Next

MessageBox.Show(String.Format("Maximum: {0}{1}Minimum: {2}", _
tempList.Max, vbCrLf, tempList.Min))

Stop

End Sub
End Class```

"A problem well stated is a problem half solved.” - Charles F. Kettering

• Marked as answer by Wednesday, March 22, 2017 12:03 PM
Tuesday, March 21, 2017 2:29 PM
• This one fore goes the assertion of string to integer.

```Dim items As New List(Of String) From
{
"99", "55", "99", "92",
"1", "109", "76", "88",
"53", "31", "200"
}

Dim result = Array.ConvertAll(items.Where(
Function(item) item.Length <= 2).Distinct.ToArray,
Function(item) CInt(item)) _
.OrderByDescending(Function(item) item) _
.Skip(1).Take(1).FirstOrDefault

MessageBox.Show(\$"{result}")
```

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator

• Marked as answer by Wednesday, March 22, 2017 12:03 PM
Tuesday, March 21, 2017 11:27 PM

### All replies

• mip,

Have a look at this. It's a bit crude but hopefully it'll do what you want:

```Option Strict On
Option Explicit On
Option Infer Off

Public Class Form1
Private Sub Form1_Load(sender As System.Object, _
e As System.EventArgs) _
Handles MyBase.Load

Dim C() As Integer = _
{99, 99, 55, 92, 76, 53, 31}

Dim tempList As New List(Of Integer)

For Each i As Integer In C
Dim s As String = i.ToString

If s.Length = 2 Then
If Not s(0) = s(1) Then
tempList.Add(i)
End If
Else
tempList.Add(i)
End If
Next

MessageBox.Show(String.Format("Maximum: {0}{1}Minimum: {2}", _
tempList.Max, vbCrLf, tempList.Min))

Stop

End Sub
End Class```

"A problem well stated is a problem half solved.” - Charles F. Kettering

• Marked as answer by Wednesday, March 22, 2017 12:03 PM
Tuesday, March 21, 2017 2:29 PM
• You can not add a number (value) to a list of strings (list of T) means a list of a type which can be any type.

Therefore use

List(of Double)

Be aware you should then add values and not characters strings.

There is by the way not any double in what you show. The double is not the nicest value type to use, in your case it can simple be integer.

Success
Cor

Tuesday, March 21, 2017 3:36 PM
• You can not add a number (value) to a list of strings (list of T) means a list of a type which can be any type.

Therefore use

List(of Double)

Be aware you should then add values and not characters strings.

There is by the way not any double in what you show. The double is not the nicest value type to use, in your case it can simple be integer.

Success
Cor

I missed that he was using strings, not integers.

"A problem well stated is a problem half solved.” - Charles F. Kettering

Tuesday, March 21, 2017 3:38 PM
• Modified:

```Public Class Form1
Private Sub Form1_Load(sender As System.Object, _
e As System.EventArgs) _
Handles MyBase.Load

Dim C_ As New List(Of String) _
From {"99", "99", "55", "92", "76", "53", "31"}

Dim tempList As New List(Of Integer)

For Each s As String In C_
If Integer.TryParse(s, New Integer) Then
If s.Length = 2 Then
If Not s(0) = s(1) Then
tempList.Add(CInt(s))
End If
Else
tempList.Add(CInt(s))
End If
End If
Next

Dim maxString As String = tempList.Max.ToString

Stop

End Sub
End Class```

"A problem well stated is a problem half solved.” - Charles F. Kettering

Tuesday, March 21, 2017 3:44 PM
• Hello everybody

```Option Strict On

Public Class Form1

Private C_ As New List(Of String) From {"99", "87", "71", "97", "99"}
Private MAXIMUM As String

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MAXIMUM = (From c As String In C_
Where Integer.TryParse(c, New Integer) _
And c.Length = 2 _
And c(0) <> c(1)
Select c).Max
End Sub
End Class```

Luc

• Marked as answer by Wednesday, March 22, 2017 12:03 PM
Tuesday, March 21, 2017 4:50 PM
• Hello everybody

```Option Strict On

Public Class Form1

Private C_ As New List(Of String) From {"99", "87", "71", "97", "99"}
Private MAXIMUM As String

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MAXIMUM = (From c As String In C_
Where Integer.TryParse(c, New Integer) _
And c.Length = 2 _
And c(0) <> c(1)
Select c).Max
End Sub
End Class```

Luc

Same as mine but with LINQ.

:)

"A problem well stated is a problem half solved.” - Charles F. Kettering

Tuesday, March 21, 2017 4:55 PM
• Yep,

was just an excuse to say hello

Luc

Tuesday, March 21, 2017 5:13 PM
• Yep,

was just an excuse to say hello

Luc

Good to see you again!

:)

"A problem well stated is a problem half solved.” - Charles F. Kettering

Tuesday, March 21, 2017 5:15 PM
• Bon à vous revoir Lucien

Success
Cor

Tuesday, March 21, 2017 7:22 PM
• Cor,

If you call me Lucien,   .... I'll call you Corneille

:)

Luc

Tuesday, March 21, 2017 7:42 PM
• This one fore goes the assertion of string to integer.

```Dim items As New List(Of String) From
{
"99", "55", "99", "92",
"1", "109", "76", "88",
"53", "31", "200"
}

Dim result = Array.ConvertAll(items.Where(
Function(item) item.Length <= 2).Distinct.ToArray,
Function(item) CInt(item)) _
.OrderByDescending(Function(item) item) _
.Skip(1).Take(1).FirstOrDefault

MessageBox.Show(\$"{result}")
```

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator

• Marked as answer by Wednesday, March 22, 2017 12:03 PM
Tuesday, March 21, 2017 11:27 PM
• thank.Cor
Wednesday, March 22, 2017 4:03 AM
• Frank code working good.

But how to show the F_ element contain.

```  Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
Dim tempList As New List(Of Integer)

For Each s As String In C_
If Integer.TryParse(s, New Integer) Then
If s.Length = 2 Then
If Not s(0) = s(1) Then
tempList.Add(CInt(s))
End If
Else
tempList.Add(CInt(s))
End If
End If
Next

For j As Integer = 0 To tempList.Count - 1
Dim maxString As String = tempList.Max.ToString
TextBox4.AppendText(CStr(maxString & F_(j) & vbCrLf))
Next

End Sub```

Wednesday, March 22, 2017 4:05 AM
• Thank Crazypennie,Long time not hear about you.

Good formulation..I ever use for Data like SPSS software.

one question how to show F_ element.

the result is;

92  322

Wednesday, March 22, 2017 4:08 AM
• Thank Karen ,You good in SQL.

How to show F_ elelment

92  322

Wednesday, March 22, 2017 4:09 AM
• mipakteh.

Why the good answer is not 92 601

Luc

Wednesday, March 22, 2017 10:18 AM
• Crazypennie ;

list C_ 92 =List F_ 322

LIst C_ 53 = List F_601

Wednesday, March 22, 2017 10:35 AM
• mipakteh,

we discarded 99 because it is what you called a "double number"

But I am not sure why it is a double number. Is it because both digits are 9 or is it because there are two 99 in the list?

Luc

• Edited by Wednesday, March 22, 2017 11:26 AM
Wednesday, March 22, 2017 11:26 AM
• Frank code working good.

But how to show the F_ element contain.

I'm not sure that I follow your intended logic, but try this and we'll go from there:

```Option Strict On
Option Explicit On
Option Infer Off

Public Class Form1
Private Sub Form1_Load(sender As System.Object, _
e As System.EventArgs) _
Handles MyBase.Load

Dim C_ As New List(Of String)
Dim F_ As New List(Of String)

C_.Add("99")
C_.Add("99")
C_.Add("55")
C_.Add("92")
C_.Add("76")
C_.Add("53")
C_.Add("31")

F_.Add("102")
F_.Add("202")
F_.Add("205")
F_.Add("322")
F_.Add("504")
F_.Add("601")
F_.Add("306")

Dim test1 As Nullable(Of Integer) = GetMax(C_)
Dim test2 As Nullable(Of Integer) = GetMax(F_)

If test1.HasValue AndAlso test2.HasValue Then
MessageBox.Show(String.Format("C_ Max: {0}{1}F_ Max: {2}", _
test1.Value, vbCrLf, _
test2.Value))
End If

Stop

End Sub

Private Function _
GetMax(ByVal str As IEnumerable(Of String)) As Nullable(Of Integer)

Dim retVal As Nullable(Of Integer) = Nothing

If str IsNot Nothing AndAlso str.Count > 0 Then
Dim tempList As New List(Of Integer)

For Each element As String In str
If Integer.TryParse(element, New Integer) Then
Dim proceed As Boolean = True

For i As Integer = 0 To element.Length - 1
If i > 0 Then
If element(i) = element(i - 1) Then
proceed = False
Exit For
End If
End If
Next

If proceed Then
tempList.Add(CInt(element))
End If
End If
Next

If tempList.Count > 0 Then
retVal = tempList.Max
End If
End If

Return retVal

End Function
End Class```

Note that the return value is a Nullable(Of Integer), so be sure that you test that it returned a value before you use it (like I did with the MessageBox).

"A problem well stated is a problem half solved.” - Charles F. Kettering

Wednesday, March 22, 2017 11:27 AM
• Frank,

He dont want F_max, he wants the element in F_ with the same index as the element max in C_  (So 322)

Also, I am not sure why we discarded 99. We assumed that it was because there are two 9 in 99, but there are also two 99 in the list. So, what mipakteh called a "double number" in his first post is not clear

Luc

• Edited by Wednesday, March 22, 2017 11:35 AM
Wednesday, March 22, 2017 11:34 AM
• Frank,

He dont want F_max, he wants the element in F_ with the same index as the element max in C_  (So 322)

Also, I am not sure why we discarded 99. We assumed that it was because there are two 9 in 99, but there are also two 99 in the list. So, what mipakteh called a "double number" in his first post is not clear

Luc

What if the two lists don't have the same number of elements?

Maybe he wants (but doesn't yet know) a class with two members? That way encapsulation will be on his side.

I'm not following his logic, honestly. ;-)

"A problem well stated is a problem half solved.” - Charles F. Kettering

Wednesday, March 22, 2017 11:37 AM
• Frank L.Smith;

Element F_ is not Max ,just the same line with C_(92)=F_(322).

Result Should be;

C_Max:92

F_ :322

Thank Frank for new code.

I think it solve just put for ;

```    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click

Dim tempList As New List(Of Integer)

For Each i As Integer In C_
Dim s As String = i.ToString

If s.Length = 2 Then
If Not s(0) = s(1) Then
tempList.Add(i)

End If

Else
tempList.Add(i)
End If
Next

For j As Integer = 0 To tempList.Count - 1
If CDbl(C_(j)) = tempList.Max Then TextBox3.AppendText(tempList.Max & "   " & F_(j) & vbCrLf)
Next

End Sub
```

• Edited by Wednesday, March 22, 2017 12:02 PM add
Wednesday, March 22, 2017 11:53 AM
• mip,

Reconsider how you're doing this. For example, try this if you will please:

```Option Strict On
Option Explicit On
Option Infer Off

Public Class Form1
Private Sub Form1_Load(sender As System.Object, _
e As System.EventArgs) _
Handles MyBase.Load

Dim numList As New List(Of Numbers)

With numList
.Add(New Numbers(99, 102))
.Add(New Numbers(99, 202))
.Add(New Numbers(55, 205))
.Add(New Numbers(92, 322))
.Add(New Numbers(76, 504))
.Add(New Numbers(53, 601))
.Add(New Numbers(31, 306))
End With

Dim max As Numbers = Numbers.GetMax(numList)

Stop

End Sub
End Class

Public Class Numbers
Private _leadElement As Integer
Private _secondaryElement As Integer

Public Sub New(ByVal lead As Integer, _
ByVal secondary As Integer)

_leadElement = lead
_secondaryElement = secondary

End Sub

Public Shared Function _
GetMax(ByVal nums As IEnumerable(Of Numbers)) As Numbers

Dim retVal As Numbers = Nothing

If nums IsNot Nothing AndAlso nums.Count > 0 Then
Dim tempList As New List(Of Numbers)

For Each n As Numbers In nums
Dim proceed As Boolean = True

Dim s As String = n.LeadElement.ToString

For i As Integer = 0 To s.Length - 1
If i > 0 Then
If s(i) = s(i - 1) Then
proceed = False
Exit For
End If
End If
Next

If proceed Then
tempList.Add(n)
End If
Next

If tempList.Count > 0 Then
retVal = (From n As Numbers In tempList Order By n.LeadElement).Last
End If
End If

Return retVal

End Function

Public ReadOnly Property LeadElement As Integer
Get
Return _leadElement
End Get
End Property

Public ReadOnly Property SecondaryElement As Integer
Get
Return _secondaryElement
End Get
End Property
End Class```

When it returns, this is what you'll see if you hover your mouse over the variable "max":

"A problem well stated is a problem half solved.” - Charles F. Kettering

Wednesday, March 22, 2017 12:09 PM
• mip,

If you decide to use the class that I posted earlier, then change it to this one (and I'll explain why afterward):

```Option Strict On
Option Explicit On
Option Infer Off

Public Class Form1
Private Sub Form1_Load(sender As System.Object, _
e As System.EventArgs) _
Handles MyBase.Load

Dim numList As New List(Of Numbers)

With numList
.Add(New Numbers(99, 102))
.Add(New Numbers(99, 202))
.Add(New Numbers(55, 205))
.Add(New Numbers(92, 322))
.Add(New Numbers(76, 504))
.Add(New Numbers(53, 601))
.Add(New Numbers(31, 306))
End With

numList.Sort()

Dim max As Numbers = Numbers.GetMax(numList)

Stop

End Sub
End Class

Public Class Numbers
Implements IComparable
Implements IComparable(Of Numbers)

Private _leadElement As Integer
Private _secondaryElement As Integer

Public Sub New(ByVal lead As Integer, _
ByVal secondary As Integer)

_leadElement = lead
_secondaryElement = secondary

End Sub

Public Shared Function _
GetMax(ByVal nums As IEnumerable(Of Numbers)) As Numbers

Dim retVal As Numbers = Nothing

If nums IsNot Nothing AndAlso nums.Count > 0 Then
Dim tempList As New List(Of Numbers)

For Each n As Numbers In nums
Dim proceed As Boolean = True

Dim s As String = n.LeadElement.ToString

For i As Integer = 0 To s.Length - 1
If i > 0 Then
If s(i) = s(i - 1) Then
proceed = False
Exit For
End If
End If
Next

If proceed Then
tempList.Add(n)
End If
Next

If tempList.Count > 0 Then
retVal = (From n As Numbers In tempList Order By n.LeadElement).Last
End If
End If

Return retVal

End Function

Public ReadOnly Property LeadElement As Integer
Get
Return _leadElement
End Get
End Property

Public ReadOnly Property SecondaryElement As Integer
Get
Return _secondaryElement
End Get
End Property

Public Function CompareTo(ByVal obj As Object) As Integer Implements IComparable.CompareTo

If obj Is Nothing Then
Return 1
End If

Dim other As Numbers = Nothing

If TypeOf obj Is Numbers Then
other = DirectCast(obj, Numbers)
Else
Throw New ArgumentException("obj is not a Numbers")
End If

Return CompareTo(other)

End Function

Public Function CompareTo(ByVal other As Numbers) As Integer Implements IComparable(Of Numbers).CompareTo

If other Is Nothing Then
Return 1
End If

Dim result As Integer = _leadElement.CompareTo(other._leadElement)

If result <> 0 Then
Return result
End If

result = Me._secondaryElement.CompareTo(other._secondaryElement)
If result <> 0 Then
Return result
End If

Return result

End Function

Public Overrides Function ToString() As String
Return String.Format("Lead: {0} | Secondary: {1}", _leadElement, _secondaryElement)
End Function
End Class```

This will allow you to use the .Sort method of the list (in my example above, I called it "numList"). I can do that because it now implements the IComparable interface. If I hadn't added that, what do you suppose would happen if you called the .Sort method of the list?

I think the technical definition is "crash and burn". ;-)

Since it now knows how to make the comparison, it likewise also now knows how to sort each instance of the class.

The result is shown below:

Note please that it doesn't eliminate the "doubled" ones - that's the job of the function - but this does allow you to sort of 'see' encapsulation at work here. The list is now sorted in ascending order based on the value of the property "LeadElement".

"A problem well stated is a problem half solved.” - Charles F. Kettering

Wednesday, March 22, 2017 12:55 PM
• Thank Frank, I will try your code...
Wednesday, March 22, 2017 1:00 PM