Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: luis456 en 9 Octubre 2015, 10:41 am



Título: Error en variable ???
Publicado por: luis456 en 9 Octubre 2015, 10:41 am
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.


 
Código
  1. Public Class Form1
  2.  
  3.    Dim maxo As Integer = 7
  4.  
  5.    Dim valuesInta As IEnumerable(Of Integer)
  6.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  7.  
  8.        Dim valuesInt As IEnumerable(Of Integer) =
  9.            {0, 1, 2, 3, 4, 5, 6, 9}
  10.  
  11.        Dim pairs As IEnumerable(Of KeyValuePair(Of Integer, IEnumerable(Of Integer))) =
  12.            Form1.GetPairs(valuesInt, maxValue:=80)
  13.  
  14.        For Each pair As KeyValuePair(Of Integer, IEnumerable(Of Integer)) In pairs
  15.  
  16.            ListBox1.Items.Add(String.Format("Key: {0,-4} Values: {1}", pair.Key, String.Join(", ", pair.Value)))
  17.  
  18.        Next pair
  19.  
  20.        MsgBox(String.Join(", ", pairs(0).Value))
  21.        MsgBox(String.Join(", ", pairs(1).Value))
  22.        MsgBox(String.Join(", ", pairs(2).Value))
  23.        MsgBox(String.Join(", ", pairs(3).Value))
  24.  
  25.  
  26.        Dim M1 As IEnumerable(Of Integer) = pairs(0).Value.ToList
  27.        Dim juntos2 As List(Of Integer) = M1
  28.        juntos2.Sort()
  29.        Me.ListBox2.Items.AddRange((From value As Integer In M1).Cast(Of Object).ToArray)
  30.  
  31.        Dim M2 As IEnumerable(Of Integer) = pairs(1).Value.ToList
  32.        Dim juntos3 As List(Of Integer) = M2
  33.        juntos3.Sort()
  34.  
  35.  
  36.        'Me.ListBox2.Items.AddRange((From value As Integer In M1).Cast(Of Object).ToArray)
  37.        Dim M3 As IEnumerable(Of Integer) = pairs(2).Value.ToList
  38.        Dim juntos4 As List(Of Integer) = M3
  39.        juntos4.Sort()
  40.  
  41.        Dim M4 As IEnumerable(Of Integer) = pairs(3).Value.ToList
  42.        Dim juntos5 As List(Of Integer) = M4
  43.        juntos5.Sort()
  44.  
  45.    End Sub
  46.  
  47.    Public Shared Function GetPairs(ByVal col As IEnumerable(Of Integer),
  48.                                    Optional ByVal maxValue As Integer = Integer.MaxValue) As IEnumerable(Of KeyValuePair(Of Integer, IEnumerable(Of Integer)))
  49.  
  50.        If (col.Max >= 10) Then
  51.            Throw New ArgumentException(paramName:="col", Message:="El valor máximo de la colección debe ser un valor inferior a 10.")
  52.  
  53.        Else
  54.  
  55.            Return From value As Integer In col
  56.                   Select New KeyValuePair(Of Integer, IEnumerable(Of Integer))(
  57.                          key:=value,
  58.                          value:=From index As Integer In col
  59.                                 Where (index <> value) AndAlso (CInt(value & index) <= maxValue)
  60.                                 Select CInt(value & index))
  61.  
  62.  
  63.        End If
  64.  
  65.  
  66.  
  67.  
  68.    End Function
  69.  
  70.    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  71.  
  72.         Dim cols As IEnumerable(Of IEnumerable(Of String)) =
  73.      From col As IEnumerable(Of Integer) In {juntos2, juntos3, juntos4, juntos5}.Distinct.ToList Where (col.Count() = maxo)
  74.         Group By String.Join("", col).AsEnumerable
  75.        Into(Group)
  76.        Select From value As Integer In Group.First.Distinct
  77.             Select value.ToString(format:="0#")
  78.  
  79.        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]
  80.  
  81.        ' MsgBox(cols.Count)
  82.    End Sub
  83. End Class

se supone que ya lo declaro y en otro codigo si me funciona bien

luis




Título: Re: Error en variable ???
Publicado por: luis456 en 9 Octubre 2015, 15:41 pm
SOLUCIONADO   :silbar:


Código
  1.  
  2.  
  3. Public Class Form1
  4.  
  5.    Dim maxo As Integer = 7
  6.  
  7.    Dim valuesInta As IEnumerable(Of Integer)
  8.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  9.  
  10.        Dim valuesInt As IEnumerable(Of Integer) =
  11.            {0, 1, 2, 3, 4, 5, 6, 9}
  12.  
  13.        Dim pairs As IEnumerable(Of KeyValuePair(Of Integer, IEnumerable(Of Integer))) =
  14.            Form1.GetPairs(valuesInt, maxValue:=80)
  15.  
  16.        For Each pair As KeyValuePair(Of Integer, IEnumerable(Of Integer)) In pairs
  17.  
  18.            ListBox1.Items.Add(String.Format("Key: {0,-4} Values: {1}", pair.Key, String.Join(", ", pair.Value)))
  19.  
  20.        Next pair
  21.  
  22.        MsgBox(String.Join(", ", pairs(0).Value))
  23.        MsgBox(String.Join(", ", pairs(1).Value))
  24.        MsgBox(String.Join(", ", pairs(2).Value))
  25.        MsgBox(String.Join(", ", pairs(3).Value))
  26.  
  27.  
  28.        Dim M1 As IEnumerable(Of Integer) = pairs(0).Value.ToList
  29.        Dim juntos2 As List(Of Integer) = M1
  30.        juntos2.Sort()
  31.        'Me.ListBox2.Items.AddRange((From value As Integer In M1).Cast(Of Object).ToArray)
  32.  
  33.        Dim M2 As IEnumerable(Of Integer) = pairs(1).Value.ToList
  34.        Dim juntos3 As List(Of Integer) = M2
  35.        juntos3.Sort()
  36.  
  37.  
  38.        'Me.ListBox2.Items.AddRange((From value As Integer In M1).Cast(Of Object).ToArray)
  39.        Dim M3 As IEnumerable(Of Integer) = pairs(2).Value.ToList
  40.        Dim juntos4 As List(Of Integer) = M3
  41.        juntos4.Sort()
  42.  
  43.        Dim M4 As IEnumerable(Of Integer) = pairs(3).Value.ToList
  44.        Dim juntos5 As List(Of Integer) = M4
  45.        juntos5.Sort()
  46.  
  47.        Dim cols As IEnumerable(Of IEnumerable(Of String)) =
  48.       From col As IEnumerable(Of Integer) In {juntos2, juntos3, juntos4, juntos5}.Distinct.ToList Where (col.Count() = maxo)
  49.      Group By String.Join("", col).AsEnumerable
  50.        Into Group
  51.      Select From value As Integer In Group.First.Distinct
  52.             Select value.ToString(format:="0#")
  53.  
  54.        ListBox2.Items.AddRange((From col As IEnumerable(Of String) In cols Select String.Join(", ", col)).ToArray)
  55.  
  56.        MsgBox(cols.Count)
  57.  
  58.  
  59.  
  60.  
  61.  
  62.    End Sub
  63.  
  64.    Public Shared Function GetPairs(ByVal col As IEnumerable(Of Integer),
  65.                                    Optional ByVal maxValue As Integer = Integer.MaxValue) As IEnumerable(Of KeyValuePair(Of Integer, IEnumerable(Of Integer)))
  66.  
  67.        If (col.Max >= 10) Then
  68.            Throw New ArgumentException(paramName:="col", Message:="El valor máximo de la colección debe ser un valor inferior a 10.")
  69.  
  70.        Else
  71.  
  72.            Return From value As Integer In col
  73.                   Select New KeyValuePair(Of Integer, IEnumerable(Of Integer))(
  74.                          key:=value,
  75.                          value:=From index As Integer In col
  76.                                 Where (index <> value) AndAlso (CInt(value & index) <= maxValue)
  77.                                 Select CInt(value & index))
  78.  
  79.  
  80.        End If
  81.  
  82.  
  83.  
  84.  
  85.    End Function
  86.  
  87.  
  88. End Class


Luis