Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从VB到C#do while的运行顺序有区别吗_C#_Vb.net - Fatal编程技术网

从VB到C#do while的运行顺序有区别吗

从VB到C#do while的运行顺序有区别吗,c#,vb.net,C#,Vb.net,因此,在工作中,我将所有旧的VB代码移到了C#上,我遇到了一个问题 在VB代码中,它是 Do While (iterator.NextCourse(course_list, prof_relation, prof_list, index)) course = course_list(index) course_title_id = CType(course(CourseListQueries.GetCourseListCols.CourseTitleId), Integer)

因此,在工作中,我将所有旧的VB代码移到了C#上,我遇到了一个问题

在VB代码中,它是

 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
所以我只想知道,因为在VB中,do while在一行上,它的运行顺序是否与c#方法不同? 我将如何着手修复这种情况,因为c#自上而下运行,并且只有在完成迭代后,它才会进入while方法并设置这些引用

在C#
中,while
也可以位于,因此在您的示例中,这将是:

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...
    }
}

是的,VB代码的运行顺序与问题中的C代码不同。测试发生在VB代码的循环开始和C版本的循环结束处


如果您将C#version更改为
while
循环,而不是
do while
(如@Richard Ev的回答中所示),则它们将执行相同的操作。

do之间的差异。。。边循环边执行。。。循环是检查条件时的循环

在第一种语法中(相当于do{…},而在C#中do{…}),执行一次块,然后检查条件以查看是否应该再次执行块

在<代码>中,边做边做。。。循环block(相当于
while{…}
在C#中)首先检查条件,如果条件为真,则执行代码

因此,在您的例子中,将
while
移动到语句块的顶部将使两个片段等效:

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...
      }
}

您还可以尝试使用if-else中断的无限for循环

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

实际上,在VB中,While也可以在循环结束时运行
for(;;)
{
    if (iterator.NextCourse(course_list, prof_relation,ref prof_list, ref index))
    {
        code....
    }
    else 
        break;
}