-6
private void RectangleTapped(object sender, TappedRoutedEventArgs e)
{
     RedRectangle.Fill.Opacity = 1.0;
     GreenRectangle.Fill.Opacity = 1.0;
     BlueRectangle.Fill.Opacity = 1.0;
     YellowRectangle.Fill.Opacity = 1.0;

     var rectangle =sender as Rectangle;
     if (rectangle != null)
     {
         rectangle.Fill.Opacity =0.25;            
     }
}

What does the var rectangle=sender as Rectangle mean ?

This code works with var Rectangle=sender as Rectangle too.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
Sajjad
  • 68
  • 1
  • 6

3 Answers3

5

An event’s sender is just passed to the event handler as an object. Now when that event is raised, you usually know what kind of sender you can expect (since you set up the event handler yourself), but the method still requires an object type.

Now the as is a type conversion that tries to convert the object into that type, but returns null if the type is not compatible. So in this case, you have this:

var rectangle = sender as Rectangle;

There are two possibilities:

  1. sender is a type that can be assigned to a Rectangle, in which case, rectangle will contain a reference to the same object but typed as a Rectangle instead of just object
  2. sender is of some other type, in which case rectangle will be null, which is caught in the following check.
poke
  • 369,085
  • 72
  • 557
  • 602
2

The as operator attempts to convert the parameter to the requested type, returning null if the conversion/cast fails. (MSDN)

So the code you provided is checking if sender is a Rectangle object (or a derived type). It then checks for null before using the converted variable, which is always good practice when using as.

Note that the second code just assigns to a different variable name, though using a class name as a variable name is strongly discouraged.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
2

You have some really, really basic concepts you're attempting to understand. I'll attempt to break the question apart with some clarification and add to the two exceptional answers you've received.

Cast:

The conceptual notion of casting is incredibly important, as this concept is imperative in modification of your data. Your specifically asking about:

Explicit conversions (casts): Explicit conversions require a cast operator. Casting is required when information might be lost in the conversion, or when the conversion might not succeed for other reasons. Typical examples include numeric conversion to a type that has less precision or a smaller range, and conversion of a base-class instance to a derived class.

In your example, your implementing the as Rectangle. For the sake of simplicity I'm going to use an easier data type. The as cast when it converts it won't create the following error:

  • Invalid Cast Exception

Instead it returns a null when it fails, which is a clean way to fail silently. Which meets particular needs in several instances where you don't want an Exception to break your user flow.

Example (A):

int valid = 0;
string number = valid as string;

Example (B):

string valid = 1;
int number = valid as string;

Example (C):

string valid = "valid";
int? number = valid as int?;

All three of those examples are valid, however in Example (C) you'll notice two items:

  • int? - Allows a null for an int.
  • It also isn't breaking your application, as the failed cast will return null.

The other approach for casting would be to apply (int) or (string) to explicitly force the data to that type. So it will either succeed and or throw exception. For example:

Example (D):

string invalid = "Fail"
int? number = (int?)invalid;

That would cause the Invalid Cast Exception to be thrown. Example (C) and Example (D) are two simple comparisons for the difference between cast.

Hopefully this provides better information for you.

Greg
  • 11,302
  • 2
  • 48
  • 79