as-operator

How to use “Sender” parameter with “As” operator for more then one class at a time?

橙三吉。 提交于 2019-12-09 06:42:43
问题 In Delphi, sometimes we need to do this... function TForm1.EDIT_Click(Sender: TObject); begin (Sender As TEdit).Text := ''; end; ...but sometimes we need to repeat the function with other object class like... function TForm1.COMBOBOX_Click(Sender: TObject); begin (Sender As TComboBox).Text := ''; end; ...because the operator As does not accept flexibility. It must know the class in order to allow the .Text that come after the () . Sometimes the code gets full of similar functions and

How to use “Sender” parameter with “As” operator for more then one class at a time?

无人久伴 提交于 2019-12-03 08:25:12
In Delphi, sometimes we need to do this... function TForm1.EDIT_Click(Sender: TObject); begin (Sender As TEdit).Text := ''; end; ...but sometimes we need to repeat the function with other object class like... function TForm1.COMBOBOX_Click(Sender: TObject); begin (Sender As TComboBox).Text := ''; end; ...because the operator As does not accept flexibility. It must know the class in order to allow the .Text that come after the () . Sometimes the code gets full of similar functions and procedures because we need to do the same thing with similar visual controls that we can't specify. This is

When should you use the as keyword in C#

≯℡__Kan透↙ 提交于 2019-11-30 17:09:40
When you want to change types most of the time you just want to use the traditional cast. var value = (string)dictionary[key]; It's good because: It’s fast It’ll complain if something is wrong (instead of giving object is null exceptions) So what is a good example for the use of as I couldn't really find or think of something that suits it perfectly? Note: Actually I think sometimes there are cases where the complier prevents the use of a cast where as works (generics related?). Use as when it's valid for an object not to be of the type that you want, and you want to act differently if it is.

When should you use the as keyword in C#

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 16:28:13
问题 When you want to change types most of the time you just want to use the traditional cast. var value = (string)dictionary[key]; It's good because: It’s fast It’ll complain if something is wrong (instead of giving object is null exceptions) So what is a good example for the use of as I couldn't really find or think of something that suits it perfectly? Note: Actually I think sometimes there are cases where the complier prevents the use of a cast where as works (generics related?). 回答1: Use as

Why can't I use the as keyword for a struct?

被刻印的时光 ゝ 提交于 2019-11-28 13:16:10
I defined the following struct: public struct Call { public SourceFile caller; public SourceFile callee; public Call(SourceFile caller, SourceFile callee) { this.caller = caller; this.callee = callee; } } Later, I assign it to the Tag property of another object: line.Tag = new Call(sf1, sf2); But when I try to retrieve the Tag property like so, Call call = line.Tag as Call; Visual Studio gives the following compile-time error: The operator as must be used within a reference type or nullable type What is the meaning of that? And how can I solve it? A struct is a value type, so it cannot be used

Why can't I use the as keyword for a struct?

别来无恙 提交于 2019-11-27 07:34:12
问题 I defined the following struct: public struct Call { public SourceFile caller; public SourceFile callee; public Call(SourceFile caller, SourceFile callee) { this.caller = caller; this.callee = callee; } } Later, I assign it to the Tag property of another object: line.Tag = new Call(sf1, sf2); But when I try to retrieve the Tag property like so, Call call = line.Tag as Call; Visual Studio gives the following compile-time error: The operator as must be used within a reference type or nullable