What is wrong with this code snippet? Its giving me type declaration error
-
13-06-2021 - |
Question
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
OTHER TIPS
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
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)