简体   繁体   中英

is there a difference in run order VB to C# do-while

So at work i am moving all our old VB code to C# and i encountered a problem.

In the VB Code it is

 Do While (iterator.NextCourse(course_list, prof_relation, prof_list, index))
    course = course_list(index)

    course_title_id = CType(course(CourseListQueries.GetCourseListCols.CourseTitleId), Integer)
    course_id = CType(course(CourseListQueries.GetCourseListCols.CourseId), Integer)

    prof_name_list = String.Empty

    For Each prof As DataRowView In prof_list
       If (Not IsDBNull(prof(CourseListQueries.GetCourseProfsCols.ProfId))) Then
          last_name = CType(prof(CourseListQueries.GetCourseProfsCols.LastName), String)
          first_name = CType(prof(CourseListQueries.GetCourseProfsCols.FirstName), String)
              If (IsDBNull(prof(CourseListQueries.GetCourseProfsCols.PreferredName))) Then
                 preferred_name = first_name
              Else
                 preferred_name = CType(prof(CourseListQueries.GetCourseProfsCols.PreferredName), String)
              End If

           prof_name = preferred_name.Substring(0, 1) & ". " & last_name

              If (prof_name_list <> String.Empty) And (prof_name <> String.Empty) Then
                  prof_name_list &= " / "
              End If

           prof_name_list &= prof_name
        End If
  Next

which works fine it works while the method returns bool, but that method is passed by ref. so it links the relation of 2 DataTables.

in C# that code is like

do
{
    code...
    foreach (DataRowView  prof In prof_list) /* crashes here with null object reference*/
      {
          code...
      }
}while(iterator.NextCourse(course_list, prof_relation,ref prof_list, ref index));

so i just want to know, Because in VB the do-while is physically on one line does it run in a different order than the c# method? How would i go about mending this situation because c# runs top to bottom and only once it's done an iteration does it enter the while method and set those references.

The difference between Do ... Loop While and Do While ... Loop is when the condition is checked.

In the first syntax (equivalent to do { ... } while in C#) the block is executed once, THEN the condition is checked to see if the block should be executed again.

In a Do While ... Loop block (equivalent to while { ... } in C#) the condition is checked first , then the code is executed if the condition is true.

So in your case moving the while to the top of the statement block would make the two fragments equivalent:

while(iterator.NextCourse(course_list, prof_relation,ref prof_list, ref index))
{
    code...
    foreach (DataRowView  prof In prof_list) /* crashes here with null object reference*/
      {
          code...
      }
}

In C# while can also go at the top of the loop , so in your example this would be:

while (iterator.NextCourse(course_list, prof_relation,ref prof_list, ref index))
{
    // code...
    foreach (DataRowView  prof In prof_list) /* crashes here with null object reference*/
    {
        // code...
    }
}

Yes, the VB code runs in a different order than the C# code in your question. The test is happening at the beginning of the loop in your VB code and at the end of the loop in the C# version.

If you change your C# version to a while loop instead of a do while (as in @Richard Ev's answer), they will do the same thing.

you could also try an infinite for loop with a if else break

for(;;)
{
    if (iterator.NextCourse(course_list, prof_relation,ref prof_list, ref index))
    {
        code....
    }
    else 
        break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM