简体   繁体   English

此代码段有什么问题? 它给我类型声明错误

[英]What is wrong with this code snippet? Its giving me type declaration error

I am trying to convert this code into VB but it is giving me this error. 我正在尝试将此代码转换为VB,但它给了我这个错误。

CONVERSION ERROR: Code could not be converted. 转换错误:无法转换代码。 Details: 细节:

-- line 1 col 8: invalid TypeDecl -第1行第8列:无效的TypeDecl

Please check for any errors in the original code and try again. 请检查原始代码中是否有任何错误,然后重试。

  public Exam GetExamByExamID(int ExamID)
                {
             Exam myExam = new Exam(0,"",0,"","");  
              for(Exam exam1 : ExamArray)
            if(Exam.ExamID==ExamID)
              {
                 myExam.ExamID = exam1.ExamID;
                 myExam.ExamTitle = exam1.ExamTitle;
                     myExam.CreditHours = exam1.CreditHours;
                     myExam.Description = exam1.Description;
                         myExam.PrerequisiteExam = exam1.PrerequisiteExam;

              }
                  return myExam;

I changed my code to this but it is still the same error.. 我将代码更改为此,但仍然是相同的错误。

public Exam GetExamByExamID(int ExamID)
                    {
                 Exam myExam = new Exam(0,"",0,"","");  
                  for(Exam exam1 : ExamArray)
                if(Exam.ExamID==ExamID)
                  {
                     myExam.ExamID = exam1.ExamID;
                     myExam.ExamTitle = exam1.ExamTitle;
                         myExam.CreditHours = exam1.CreditHours;
                         myExam.Description = exam1.Description;
                             myExam.PrerequisiteExam = exam1.PrerequisiteExam;

                  }
                      return myExam;
                       }

What exactly do I need to change? 我到底需要更改什么? Please help. 请帮忙。

ok I changed again but same error..it says error in Line 1.. 确定,我再次更改,但相同的错误..它说在第1行中的错误。

public Exam GetExamByExamID(int ExamID)
                    {
                 Exam myExam = new Exam(0,"",0,"","");  
                  for(Exam exam1 : ExamArray)
                if(exam1.ExamID==ExamID)
                  {
                     myExam.ExamID = exam1.ExamID;
                     myExam.ExamTitle = exam1.ExamTitle;
                         myExam.CreditHours = exam1.CreditHours;
                         myExam.Description = exam1.Description;
                             myExam.PrerequisiteExam = exam1.PrerequisiteExam;

                  }
                      return myExam;
                       }

I think you should post the entire class instead of just the snippet. 我认为您应该发布整个课程,而不只是片段。 Anyways, I will have the first dig at it. 无论如何,我将首先进行挖掘。

Exam.ExamID and other Exam.* 

Are you sure they refer to a variable? 您确定他们引用了变量吗? If you are referring to fields of this class, you should use this.ExamID and so on 如果要引用此类的字段,则应使用this.ExamID

If not, this is most likely your error. 如果没有,这很可能是您的错误。

This is just a guess but try changing 这只是一个猜测,但请尝试更改

public Exam GetExamByExamID(int ExamID)

to

public Exam GetExamByExamID(int id)

along with changing 随着变化

if(exam1.ExamID==ExamID)

to

if(exam1.ExamID==id)

The error you are getting from the code converter is because you have not included the class Exam to be converted so the converter error is saying "invalid Type Declaration" the first time it encounters the unknown class Exam . 您从代码转换器得到的错误是因为您没有包括要转换的类Exam ,因此转换器错误在它第一次遇到未知类Exam时说“无效的类型声明”。

this might work for you: 这可能对您有用:

 Public Function GetExamByExamID(ExamID As Integer) As Exam
    Dim myExam As New Exam(0, "", 0, "", "")
    For Each exam1 As exam In ExamArray
        If exam1.ExamID = ExamID Then
            With myExam
                .ExamID = exam1.ExamID
                .ExamTitle = exam1.ExamTitle
                .CreditHours = exam1.CreditHours
                .Description = exam1.Description
                .PrerequisiteExam = exam1.PrerequisiteExam
            End With
            Return myExam
        End If
    Next
    Return Nothing
End Function

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

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