您好,我正在开发一个非常小的Windows应用程序,试图再次进入事物的摇摆。我遇到的问题是,当没有检查单选按钮时,我试图抛出异常,以便出现一个消息框告诉用户选择一个单选按钮。但是,我无法记住抛出什么异常或如何实现它。我已经提供了一个小代码片段供您查看我是如何做到这一点的。我打算在if语句之后添加一个else语句,但是不记得要抛出什么。该应用程序是一个非常基本的小型计算器。
if ((bool)Addition.IsChecked.HasValue && Addition.IsChecked.Value)
{
calcualteAdditionSum(val1, val2);
}
if ((bool)Subtract.IsChecked.HasValue && Subtract.IsChecked.Value)
{
calculateSubtractSun(val1, val2);
}
if ((bool)Remainder.IsChecked.HasValue && Remainder.IsChecked.Value)
{
calculateRemainderSun(val1, val2);
}
if ((bool)Multiply.IsChecked.HasValue && Multiply.IsChecked.Value)
{
calculateMultiplySum(val1, val2);
}
if ((bool)Division.IsChecked.HasValue && Division.IsChecked.Value)
{
calculateDivisionSum(val1, val2);
}
}
catch
{
MessageBox.Show("Please ensure you have selected a checkBox\nand provided a number in the textboxes");
}
答案 0 :(得分:3)
你不必抛出异常或抓住任何东西。只需创建一个bool
变量,并在过程开始时将其初始化为false
。如果选择了任何按钮,此变量将存储信息。在每个true
块内将其值更改为if
,并在结尾处检查其状态。如果它仍然是false
,请显示一条消息。
修改强>
基本上,当发生不好的事情时,你应该抛出异常。我不会将选择任何单选按钮归类为坏事。
答案 1 :(得分:1)
我会将第2,第3,第4和第5个{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE ConstraintKinds #-}
import GHC.TypeLits
newtype Field (n :: Symbol) v = Field { unField :: v } deriving Show
data Person1 = Person1
{ _age :: Field "age" Int
, _name :: Field "name" String
}
data Person2 = Person2
{ _age' :: Field "age" Int
, _name' :: Field "name" String
, _lib' :: Field "lib" String
}
deriving instance Show Person1
deriving instance Show Person2
data Label (l :: Symbol) = Get
class Has a l b | a l -> b where
from :: a -> Label l -> b
instance Has Person1 "age" Int where
from (Person1 a _) _ = unField a
instance Has Person1 "name" String where
from (Person1 _ a) _ = unField a
instance Has Person2 "age" Int where
from (Person2 a _ _) _ = unField a
instance Has Person2 "name" String where
from (Person2 _ a _) _ = unField a
age :: Has a "age" b => a -> b
age pnt = from pnt (Get :: Label "age")
name :: Has a "name" b => a -> b
name pnt = from pnt (Get :: Label "name")
-- Parameterized constraint kind for "Simon-ness" of a record.
type Simon a = (Has a "name" String, Has a "age" Int)
spj :: Person1
spj = Person1 (Field 56) (Field "Simon Peyton Jones")
smarlow :: Person2
smarlow = Person2 (Field 38) (Field "Simon Marlow") (Field "rts")
catNames :: (Simon a, Simon b) => a -> b -> String
catNames a b = name a ++ name b
addAges :: (Simon a, Simon b) => a -> b -> Int
addAges a b = age a + age b
names :: String
names = name smarlow ++ "," ++ name spj
-- "Simon Marlow,Simon Peyton Jones"
ages :: Int
ages = age spj + age smarlow
-- 94
语句更改为if
语句(除非我错过了某些内容 - 如果他们是单选按钮,那么它似乎不可能一次有多个条件)然后我会在结尾添加一个else if
语句,显示一个消息框,向用户解释选择单选按钮之一。不需要例外。