简体   繁体   English

我如何抛出异常从app.config文件的值

[英]How can i throw exception to the value from app.config file

I was said to import sales tax rate from app.config file. 据说我要从app.config文件导入销售税率。 In my app.config file i have two tax value GST and PST. 在我的app.config文件中,我有两个税值GST和PST。 How can i import this string value to my program code plus convert to decimal value and then add it . 我如何将这个字符串值导入到我的程序代码中,再转换为十进制值,然后将其添加。 I have done till and i am receiving an error of throw exception. 我已经做完了,并且收到抛出异常的错误。 How can i throw exception ?? 我如何抛出异常? Thanks for the help in advance. 我在这里先向您的帮助表示感谢。

public decimal SalesTax 
        {
            get
            {
                decimal rateGST = Decimal.Parse(ConfigurationManager.AppSettings["rateGST"]);
                decimal ratePST = Decimal.Parse(ConfigurationManager.AppSettings["ratePST"]);
                return Subtotal * (rateGST + ratePST);
            }
        }

my App.config file is as below 我的App.config文件如下

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>

    <!--Government Sales Tax-->
    <add key ="rateGST" value="5"/>

    <!--Provincial Sales Tax-->
    <add key="ratePST" value="8"/>

  </appSettings>
</configuration>

Try the following 尝试以下

using System;
namespace demo
{
    class Class1
    {
        public decimal Subtotal { get; set; } //= 5;
        public decimal SalesTax
        {
            get
            {
                decimal rateGST = Decimal.Parse(Properties.Settings.Default.rateGST);
                decimal ratePST = Decimal.Parse(Properties.Settings.Default.ratePST);
                decimal result = Subtotal * (rateGST + ratePST);
                return result;
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何添加新的app.Config文件? - How Can I add new app.Config file? 如何更新app.config文件中的值? - How to update an value in app.config file? 我如何编辑app.config文件以使应用程序从同一文件夹附加数据库 - how can i edit the app.config file to let the application attach the database from the same folder Winforms应用程序的app.config的名称是什么? 以及我怎么知道该应用程序已加载该app.config? - what is the name of the app.config of a winforms app? and how can i know that the app loaded that app.config? 如何从app.config获取此配置值? - how to get this config value from app.config? 如何在 Visual Studio Web 2013 中添加 app.config 或 web.config 文件? - How can I add an app.config or web.config file in Visual Studio Web 2013? 在Windows 8上无法访问App.Config的路径。如何更新App.Config? - Access to path denied for App.Config on Windows 8. How can I update App.Config? 我可以在安装程序中生成一个值并将其写入app.config吗? - Can I generate a value in an installer and write it to the app.config? 当密钥需要增加到大约1时,如何通过循环为app.config添加值? - How can I add value to app.config with loop for, when key need to be increase about value 1? 从app.config文件读取值时出现问题 - Problems while reading value from app.config file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM