Estoy trabajno este codigo y me da error en cols y no descubro el porque ? ya que lo declaro ??
Error 2 'cols' no está declarado. Puede que esté inaccesible debido a su nivel de protección.
Public Class Form1
Dim maxo As Integer = 7
Dim valuesInta As IEnumerable(Of Integer)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim valuesInt As IEnumerable(Of Integer) =
{0, 1, 2, 3, 4, 5, 6, 9}
Dim pairs As IEnumerable(Of KeyValuePair(Of Integer, IEnumerable(Of Integer))) =
Form1.GetPairs(valuesInt, maxValue:=80)
For Each pair As KeyValuePair(Of Integer, IEnumerable(Of Integer)) In pairs
ListBox1.Items.Add(String.Format("Key: {0,-4} Values: {1}", pair.Key, String.Join(", ", pair.Value)))
Next pair
MsgBox(String.Join(", ", pairs(0).Value))
MsgBox(String.Join(", ", pairs(1).Value))
MsgBox(String.Join(", ", pairs(2).Value))
MsgBox(String.Join(", ", pairs(3).Value))
Dim M1 As IEnumerable(Of Integer) = pairs(0).Value.ToList
Dim juntos2 As List(Of Integer) = M1
juntos2.Sort()
Me.ListBox2.Items.AddRange((From value As Integer In M1).Cast(Of Object).ToArray)
Dim M2 As IEnumerable(Of Integer) = pairs(1).Value.ToList
Dim juntos3 As List(Of Integer) = M2
juntos3.Sort()
'Me.ListBox2.Items.AddRange((From value As Integer In M1).Cast(Of Object).ToArray)
Dim M3 As IEnumerable(Of Integer) = pairs(2).Value.ToList
Dim juntos4 As List(Of Integer) = M3
juntos4.Sort()
Dim M4 As IEnumerable(Of Integer) = pairs(3).Value.ToList
Dim juntos5 As List(Of Integer) = M4
juntos5.Sort()
End Sub
Public Shared Function GetPairs(ByVal col As IEnumerable(Of Integer),
Optional ByVal maxValue As Integer = Integer.MaxValue) As IEnumerable(Of KeyValuePair(Of Integer, IEnumerable(Of Integer)))
If (col.Max >= 10) Then
Throw New ArgumentException(paramName:="col", Message:="El valor máximo de la colección debe ser un valor inferior a 10.")
Else
Return From value As Integer In col
Select New KeyValuePair(Of Integer, IEnumerable(Of Integer))(
key:=value,
value:=From index As Integer In col
Where (index <> value) AndAlso (CInt(value & index) <= maxValue)
Select CInt(value & index))
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cols As IEnumerable(Of IEnumerable(Of String)) =
From col As IEnumerable(Of Integer) In {juntos2, juntos3, juntos4, juntos5}.Distinct.ToList Where (col.Count() = maxo)
Group By String.Join("", col).AsEnumerable
Into(Group)
Select From value As Integer In Group.First.Distinct
Select value.ToString(format:="0#")
Me.ListBox2.Items.AddRange((From cols As IEnumerable(Of String) In cols Select String.Join(", ", col)).ToArray) [color=red]<-----ACA ME DA EL ERROR[/color]
' MsgBox(cols.Count)
End Sub
End Class
se supone que ya lo declaro y en otro codigo si me funciona bien
luis
SOLUCIONADO :silbar:
Public Class Form1
Dim maxo As Integer = 7
Dim valuesInta As IEnumerable(Of Integer)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim valuesInt As IEnumerable(Of Integer) =
{0, 1, 2, 3, 4, 5, 6, 9}
Dim pairs As IEnumerable(Of KeyValuePair(Of Integer, IEnumerable(Of Integer))) =
Form1.GetPairs(valuesInt, maxValue:=80)
For Each pair As KeyValuePair(Of Integer, IEnumerable(Of Integer)) In pairs
ListBox1.Items.Add(String.Format("Key: {0,-4} Values: {1}", pair.Key, String.Join(", ", pair.Value)))
Next pair
MsgBox(String.Join(", ", pairs(0).Value))
MsgBox(String.Join(", ", pairs(1).Value))
MsgBox(String.Join(", ", pairs(2).Value))
MsgBox(String.Join(", ", pairs(3).Value))
Dim M1 As IEnumerable(Of Integer) = pairs(0).Value.ToList
Dim juntos2 As List(Of Integer) = M1
juntos2.Sort()
'Me.ListBox2.Items.AddRange((From value As Integer In M1).Cast(Of Object).ToArray)
Dim M2 As IEnumerable(Of Integer) = pairs(1).Value.ToList
Dim juntos3 As List(Of Integer) = M2
juntos3.Sort()
'Me.ListBox2.Items.AddRange((From value As Integer In M1).Cast(Of Object).ToArray)
Dim M3 As IEnumerable(Of Integer) = pairs(2).Value.ToList
Dim juntos4 As List(Of Integer) = M3
juntos4.Sort()
Dim M4 As IEnumerable(Of Integer) = pairs(3).Value.ToList
Dim juntos5 As List(Of Integer) = M4
juntos5.Sort()
Dim cols As IEnumerable(Of IEnumerable(Of String)) =
From col As IEnumerable(Of Integer) In {juntos2, juntos3, juntos4, juntos5}.Distinct.ToList Where (col.Count() = maxo)
Group By String.Join("", col).AsEnumerable
Into Group
Select From value As Integer In Group.First.Distinct
Select value.ToString(format:="0#")
ListBox2.Items.AddRange((From col As IEnumerable(Of String) In cols Select String.Join(", ", col)).ToArray)
MsgBox(cols.Count)
End Sub
Public Shared Function GetPairs(ByVal col As IEnumerable(Of Integer),
Optional ByVal maxValue As Integer = Integer.MaxValue) As IEnumerable(Of KeyValuePair(Of Integer, IEnumerable(Of Integer)))
If (col.Max >= 10) Then
Throw New ArgumentException(paramName:="col", Message:="El valor máximo de la colección debe ser un valor inferior a 10.")
Else
Return From value As Integer In col
Select New KeyValuePair(Of Integer, IEnumerable(Of Integer))(
key:=value,
value:=From index As Integer In col
Where (index <> value) AndAlso (CInt(value & index) <= maxValue)
Select CInt(value & index))
End If
End Function
End Class
Luis