Search code examples
c#vb.netcode-conversion

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.

CONVERSION ERROR: Code could not be converted. Details:

-- line 1 col 8: invalid 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..

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

Solution

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

    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