C# - 通过 if 条件检查值的数据类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1166008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:48:19  来源:igfitidea点击:

C# - Check datatype of a value via if condtion

c#listboxeventstypesarraylist

提问by

I am binding an ArrayList() to a Listbox control and assigning out an Displaymember and Value on data in the Array. My problem is that I Bind on startup but the Array gets filled after a few function calls. I have code on selectedIndexChanged to check the selectedValue but if the ArrayList is empty it returns an object, once it has data it returns the string I expect. I am still a confused why it runs selectedIndexChanged when the list has no data. Think it may run after I bind the Displaymember but before the value gets assigned:

我将一个 ArrayList() 绑定到一个 Listbox 控件,并为数组中的数据分配一个 Displaymember 和 Value。我的问题是我在启动时绑定,但在几次函数调用后数组被填充。我在 selectedIndexChanged 上有代码来检查 selectedValue 但如果 ArrayList 为空它返回一个对象,一旦它有数据它返回我期望的字符串。我仍然很困惑为什么它在列表没有数据时运行 selectedIndexChanged 。认为它可能会在我绑定 Displaymember 之后但在分配值之前运行:

lbCAT_USER.DataSource = USERS;
// Running here maybe?
lbCAT_USER.DisplayMember = "DisplayString";
// Or Here?
lbCAT_USER.ValueMember = "ID";

Either way my current work around is a try/catch of comparing the SelectedValue to a string and trying to rerun the function.

无论哪种方式,我当前的工作都是尝试/捕获将 SelectedValue 与字符串进行比较并尝试重新运行该函数。

Simple workaround is maybe a way to check the datatype prior to the if statement? Any ideas of suggestions could be very helpful. Thanks

简单的解决方法可能是在 if 语句之前检查数据类型的一种方法?任何建议的想法都可能非常有帮助。谢谢

采纳答案by Will Eddins

You have two ways to check this:

您有两种方法可以检查这一点:

string value = list.SelectedValue as string;

// The cast will return null if selectedvalue was not a string.
if( value == null ) return; 

//Logic goes here.

If you just want to do the comparison:

如果您只想进行比较:

if( list.SelectedValue is string )
{
   // ...
}

回答by galaktor

If your question is how to check the datatype of a value via if-condition (title!), then here you go (example: check if value is of type 'string'):

如果您的问题是如何通过 if 条件(标题!)检查值的数据类型,那么您可以开始(例如:检查值是否为“字符串”类型):

if(value.GetType().Equals(typeof(string)))
{
   ...
}

EDIT: This is not the cleanest way to do it. Check Guard's answer for more sophisticated ways to check types. Using 'GetType().Equals' as I do is more precise than 'is' or 'as', since 'value' must be exactly of the type 'string'. 'is' and 'as' will work even if 'value' is a subclass of the checked type. This is irrelevant though when comparing with type 'string', since you cannot inherit from string.

编辑:这不是最干净的方法。检查 Guard 的答案以获取更复杂的检查类型的方法。像我一样使用“GetType().Equals”比“is”或“as”更精确,因为“value”必须完全是“string”类型。即使 'value' 是所检查类型的子类,'is' 和 'as' 也会起作用。尽管与类型“字符串”相比,这无关紧要,因为您不能从字符串继承。

回答by Brandon Wood

To handle the case where value is null:

处理值为空的情况:

if (typeof(value) == typeof(string))
{
    ...
}

回答by Lee D

I believe you can also use:

我相信你也可以使用:

if (value is string)
{
}

to check the type of a variable in C#.

在 C# 中检查变量的类型。

回答by Hath

Yes, they all call OnSelectedIndexChanged() event

是的,它们都调用 OnSelectedIndexChanged() 事件

you can check this by placing break points on the the OnSelectedIndexChanged handler and stepping through.

您可以通过在 OnSelectedIndexChanged 处理程序上放置断点并逐步执行来检查这一点。

You could un-hook the OnSelectedIndexChanged event when you do the data binding

您可以在进行数据绑定时取消挂接 OnSelectedIndexChanged 事件

this.ListBox.SelectedIndexChanged -= OnSelectedIndexChangedHandler;

this.ListBox.DataSource = dataSource;
this.ListBox.ValueMember = "ID"
this.ListBox.DisplayMember = "Name"

this.ListBox.SelectedIndexChanged += OnSelectedIndexChangedHandler;

Or just check the value like others have suggested on the OnSelectedIndexChanged event.

或者只是像其他人在 OnSelectedIndexChanged 事件中建议的那样检查值。

Doing both can't hurt

两者都做不会伤害