-1

MVC 3 razor VB.NET project. I have resorted to manual building a list for a drop down box so I can insure certain values are available in the select list and also to control what the first item is in the list. The below is my code snippet for the part that is giving me problems..

      Dim _courses1 As Integer = db.courses.ToList.Where(Function(r) r.course_day = "Tuesday").Count
        Dim _classes1 As List(Of cours) = db.courses.ToList
        Dim classRef1 As List(Of String)
        If Not _selectedClass0 = "--" Then
            classRef1.Add("--")
        Else
            classRef1.Add(_selectedClass0)
        End If
        For i As Integer = 0 To _courses1 - 1
            For Each item In _classes1.Where(Function(f) f.course_day = "Tuesday")
                Dim _item As cours = item
                classRef1.Add(_item.course_ref)
            Next
        Next
        ViewBag.tue1 = classRef1

The _selectedClass0 is just a string that gets set earlier... The error mentioned happens when it gets to the ClassRef1.Add(_selectedClass0) part of the else statement. Which _selectedClass0 string value at the time of error is "--". I have a feeling it is in how the list is being created but I am not certain... Any ideas???

2 Answers 2

3

You're not initializing classRef1.

 Dim classRef1 As new List(Of String)

Another thing I see is in the first line - I've made the changes I see:

Dim _courses1 As Integer = db.courses.Where(Function(r) r.course_day = "Tuesday").Count()

You don't need ToList at the beginning if all your getting is the count.

3
  • Just remember that any linq method that yields a result should be last.
    – mccow002
    Nov 1, 2011 at 16:34
  • First line can be shortened to: Dim classRef1 As New List(Of string). Easier on the eyes! Nov 1, 2011 at 17:33
  • done! I'm a c# developer, so I don't know the best ways to do VB. I just know that it all compiles down to the same thing.
    – mccow002
    Nov 1, 2011 at 17:38
1

You are declaring classRef1 to be a list of strings here:

Dim classRef1 As List(Of String)

But you're never actually creating an instance using New. I'm not sure about the VB syntax, as I'm a C# developer, but I'd guess you should add the following line right after the declaration:

classRef1 = New List(Of String)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.