让我知道如何在c#中使用这个Err。这是VB代码:
If Len(sPart1) <> PART1_LENGTH Then
Err.Raise(vbObjectError, , "Part 1 must be " & PART1_LENGTH)
ElseIf Not IsNumeric(sPart1) Then
Err.Raise(vbObjectError, , "Part 1 must be numeric")
答案 0 :(得分:3)
答案 1 :(得分:2)
假设您询问的是语法,而不是特定的类:
throw new SomeException("text");
答案 2 :(得分:2)
首先,让我们将其转换为现代VB代码:
If sPart1.Length <> PART1_LENGTH Then
Throw New ApplicationException("Part 1 must be " & PART1_LENGTH)
ElseIf Not IsNumeric(sPart1) Then
Throw New ApplicationException("Part 1 must be numeric")
End If
然后C#翻译是直截了当的:
int part;
if (sPart1.Length != PART1_LENGTH) {
throw new ApplicationException("Part 1 must be " + PART1_LENGTH.ToString());
} else if (!Int32.TryParse(sPart1, out part)) {
throw new ApplicationException("Part 1 must be numeric")
}
答案 3 :(得分:1)
将Err.Raise
替换为
throw new Exception("Part 1 must be numeric");
答案 4 :(得分:0)
我知道在C#和VB.NET中都应使用异常,但是为了后代,可以在C#中使用ErrObject。
将OP程序的完整示例程序转换为C#:
using Microsoft.VisualBasic;
namespace ErrSample
{
class Program
{
static void Main(string[] args)
{
ErrObject err = Information.Err();
// Definitions
const int PART1_LENGTH = 5;
string sPart1 = "Some value";
int vbObjectError = 123;
double d;
if (sPart1.Length != PART1_LENGTH)
err.Raise(vbObjectError, null, "Part 1 must be " + PART1_LENGTH);
else if (!double.TryParse(sPart1, out d))
err.Raise(vbObjectError, null, "Part 1 must be numeric");
}
}
}