【问题标题】:Run time errors in code converted from VB to C# [duplicate]从 VB 转换为 C# 的代码中的运行时错误 [重复]
【发布时间】:2016-08-01 12:17:22
【问题描述】:

我在VB中有这段代码

 Private Function ReadProfileViewPlotOptions(ByVal SavePath As String) As ProfileViewOptionsType
            Dim FileName As String
            Dim filenumber As Short
            Dim InInt As Integer
            FileName = System.IO.Path.Combine(SavePath, "cfgpropt.sys")
            If Not System.IO.File.Exists(FileName) Then
                With ReadProfileViewPlotOptions
.ViewConcave = CBool(GetSetting(My.Application.Info.Title, "ProfileViewPlotOptions", "ViewConcave", CStr(1)))
-----

我把它转成C#这样

private static Mold_Power_Suite.Model.FrontEndStructures.PlanViewOptionsType ReadPlanViewPlotOptions(string SavePath)
        {
            var title = ((AssemblyTitleAttribute)System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false)[0]).Title;
            Mold_Power_Suite.Model.FrontEndStructures.PlanViewOptionsType functionReturnValue = default(Mold_Power_Suite.Model.FrontEndStructures.PlanViewOptionsType);
            string FileName = null;
            short filenumber = 0;
            int InInt = 0;
            FileName = System.IO.Path.Combine(SavePath, "cfgplopt.sys");
            if (!System.IO.File.Exists(FileName))
            {
                functionReturnValue.ViewConcave = Convert.ToBoolean(Interaction.GetSetting(title, "PlanViewPlotOptions", "ViewConcave", Convert.ToString(1)));
-----
}

在运行时,我的代码正在中断

functionReturnValue.ViewConcave = Convert.ToBoolean(Interaction.GetSetting(title, "PlanViewPlotOptions", "ViewConcave", Convert.ToString(1)));

编译器显示的错误是“Format Exception Unhandled”。String 未被识别为有效的布尔值。”

我哪里错了?

【问题讨论】:

标签: c# vb.net winforms


【解决方案1】:

当未找到设置时GetSetting 返回默认值"1",即包含值1string。这不能通过Convert 转换为bool。但由于GetSetting 的最后一个参数必须是string,您可以使用

Convert.ToBoolean(Interaction.GetSetting(title, "PlanViewPlotOptions", "ViewConcave", Convert.ToString(true)));

或者只是

Convert.ToBoolean(Interaction.GetSetting(title, "PlanViewPlotOptions", "ViewConcave", "true"));

【讨论】:

  • 我有一个声明是 Convert.ToBoolean(Interaction.GetSetting(title, "ProfileViewPlotOptions", "ViewEdge", Convert.ToString(0)) ;我应该使用 Convert.ToString(false)这个?
  • 是的,0代表false,但是你可以只使用“false”。
【解决方案2】:

采用字符串的方法Convert.ToBoolean 期望输入为:

包含 Boolean.TrueString 或 Boolean.FalseString 值的字符串。

TrueString 是“True”,FalseString 是“False”。

如果GetSettings 返回的字符串不是这些,那么该方法将引发 FormatException。

您需要将GetSettings 的返回值分配给一个变量,然后确保其格式正确或自己进行真/假测试。但是,您将 Convert.ToString(1) 作为默认值传递,因此如果该设置不存在,您将返回字符串“1”,它既不是“True”也不是“False”。

将默认值改为“真”:

functionReturnValue.ViewConcave = Convert.ToBoolean(Interaction.GetSetting(title, "PlanViewPlotOptions", "ViewConcave", "True"));

【讨论】:

    【解决方案3】:

    你可以替换这一行

    functionReturnValue.ViewConcave = 
        Convert.ToBoolean(Interaction.GetSetting(title, "PlanViewPlotOptions", "ViewConcave", Convert.ToString(1)));
    

    以下内容:

    functionReturnValue.ViewConcave = 
        Convert.ToBoolean(Convert.ToInt32(Interaction.GetSetting(title, "PlanViewPlotOptions", "ViewConcave", "1")));
    

    因为接受整数的Convert.ToBoolean 重载将正确1 转换为false,而接受字符串 的重载将无法转换@987654326 @ 因为这必须是"True"/"False"

    因此,提供您的 GetSetting 将返回一个可以转换为 Int32 的字符串,这将起作用

    【讨论】:

    • 恐怕我说这不是正确的方法。感谢您的意见:)
    • 如果GetSetting 返回"True""False" 怎么办?我认为它确实......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    相关资源
    最近更新 更多