简体   繁体   English

VB到C#的运行顺序有什么区别

[英]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. 所以在工作中,我将所有旧的VB代码都移到了C#上,遇到了问题。

In the VB Code it is 在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

which works fine it works while the method returns bool, but that method is passed by ref. 该方法工作正常,当方法返回布尔值时有效,但是该方法由ref传递。 so it links the relation of 2 DataTables. 因此它链接了2个数据表的关系。

in C# that code is like 在C#中,代码就像

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? 所以我只想知道,因为在VB中do-while实际上在一行上,它的运行顺序与c#方法不同吗? 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. 我将如何解决这种情况,因为c#从上到下运行,并且只有在完成迭代后,它才会输入while方法并设置这些引用。

The difference between Do ... Loop While and Do While ... Loop is when the condition is checked. Do ... Loop WhileDo While ... Loop之间的区别是检查条件的情况。

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. 在第一种语法中(相当于C#中的do { ... } while ),该块执行一次,然后检查条件以查看是否应再次执行该块。

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. Do While ... Loop块中(相当于C#中的while { ... } ), 首先检查条件,然后在条件为true时执行代码。

So in your case moving the while to the top of the statement block would make the two fragments equivalent: 因此,在您的情况下,将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...
      }
}

In C# while can also go at the top of the loop , so in your example this would be: 在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...
    }
}

Yes, the VB code runs in a different order than the C# code in your question. 是的,VB代码以与您问题中的C#代码不同的顺序运行。 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. 该测试在VB代码的循环开始时和C#版本的循环结束时进行。

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. 如果将C#版本更改为while循环而不是do while (如@Richard Ev的回答),则它们将执行相同的操作。

you could also try an infinite for loop with a if else break 您也可以尝试使用if中断的无限for循环

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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