C# public string STATUS { get; set; } 遇到 InProcess,Cancel,Complete时转换为处理中,已取消,已完成
时间: 2024-03-16 07:43:09 浏览: 21
您可以在属性的 set 方法中添加相应的逻辑来实现对 STATUS 属性值的转换。例如:
```
private string _status;
public string STATUS
{
get { return _status; }
set
{
if (value == "InProcess")
{
_status = "处理中";
}
else if (value == "Cancel")
{
_status = "已取消";
}
else if (value == "Complete")
{
_status = "已完成";
}
else
{
_status = value;
}
}
}
```
这里使用了 if/else 语句来判断输入的值,如果是 InProcess、Cancel 或 Complete,则转换为中文状态,否则直接赋值给 _status 变量。注意,这里使用了私有变量来存储转换后的状态,而不是直接在 set 方法中赋值给 STATUS 属性,这是为了避免在 set 方法中引起递归调用。
相关问题
c# 中怎么把string转换为datetime
在C#中,可以使用DateTime.Parse()或DateTime.ParseExact()方法将字符串转换为DateTime类型。这两种方法的区别在于DateTime.Parse()方法可以根据当前系统的区域设置自动解析日期时间格式,而DateTime.ParseExact()方法需要指定日期时间的确切格式。
下面是使用DateTime.Parse()方法将字符串转换为DateTime类型的示例:
```csharp
string dateString = "2022-01-01 10:30:00";
DateTime dateTime = DateTime.Parse(dateString);
```
下面是使用DateTime.ParseExact()方法将字符串转换为DateTime类型的示例:
```csharp
string dateString = "2022-01-01 10:30:00";
string format = "yyyy-MM-dd HH:mm:ss";
DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
```
请注意,如果字符串的格式与指定的格式不匹配,或者字符串无法解析为有效的日期时间,这两种方法都会抛出异常。因此,在转换之前,请确保字符串的格式正确并且可以被成功解析为DateTime类型。
C# JObject.this[string].**get** 返回 null时怎么处理
当C#中的JObject对象通过索引器获取某个key的value时,如果该key不存在,则会返回null。为了避免空引用异常,我们可以在获取value之后进行判断,如果value为null,则进行相应的处理,例如输出错误信息或者进行其他操作。以下是一个示例代码:
```csharp
JObject obj = new JObject();obj["key1"] = "value1";
obj["key2"] = null;
string value1 = obj["key1"]?.ToString(); // value1
string value2 = obj["key2"]?.ToString(); // null
if (value2 == null)
{
Console.WriteLine("key2 does not exist or its value is null.");
// do something else
}
```
在上述代码中,我们使用了null条件运算符(?.)来获取JObject对象中key对应的value,并将其转换为字符串类型。如果value为null,则value2的值也为null。接着,我们使用if语句来判断value2是否为null,如果是,则输出错误信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)