2012-06-11 49 views
0

我想將此代碼轉換爲VB,但它給了我這個錯誤。這段代碼有什麼問題?它給我類型聲明錯誤

轉換錯誤:代碼無法轉換。詳細信息:

- 第1行第8欄:無效TypeDecl

請您在原始代碼的任何錯誤,然後再試一次。

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; 

我改變了我的代碼,這一點,但它仍然是同樣的錯誤..

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

究竟是什麼,我需要改變?請幫忙。

好,我又變了,但同樣的error..it說錯誤在第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; 
         } 
+1

見user1440922回答以下。此外,一旦你找到考試並設置myExam,你應該打破你的for循環。 – Josh

+1

只需編輯您的原始代碼,而不是粘貼在稍有不同的新副本中! – Michael

回答

0

你從代碼轉換器得到的錯誤是因爲你沒有包含在類考試轉換,因此轉換器錯誤說「無效的類型聲明」第一次遇到未知的類考試

這可能會爲你工作:

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 
0

這只是一個猜測,但嘗試改變

public Exam GetExamByExamID(int ExamID) 

public Exam GetExamByExamID(int id) 

隨着變化

if(exam1.ExamID==ExamID) 

if(exam1.ExamID==id) 
+0

請檢查我編輯的答案,並建議..謝謝 – Geeko

1

我想你應該張貼整個類,而不是僅僅是片斷的。無論如何,我會先對它進行挖掘。

Exam.ExamID and other Exam.* 

你確定他們引用了一個變量嗎?如果您指的是此類別的字段,則應該使用this.ExamID等等

如果不是,這很可能是您的錯誤。