Unfamiliar C# syntax for class instantiation / initialization - DataContext?

Tom Knowlton
Tom Knowlton used Ask the Experts™
on
Not familiar with this syntax.  Is this instantiating an instance of the class?  Why is there no "new" keyword?

InspectionsByTasksViewModel vm = (DataContext as InspectionsByTasksViewModel);

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi Tom,

If you would post the entire code snippet it would have been easier to explain. By the looks of it, this is not initialization, this is casting.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/as

It will try to cast the DataContext as a type of InspectionsByTasksViewModel.

Biggest advantage or disadvantage of as keyword is, that the as operator is like a cast operation. But, if the conversion isn't possible, as returns null instead of raising an exception.

Regards,
Chinmay.
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
By the looks of it, this is not initialization,
Well, vm is being initialized...it's just being initialized based on what you described  ;)
Tom KnowltonWeb developer

Author

Commented:
You're correct; I did not give much surrounding code or context.  Here is a screenshot of an example where this is taking place.

I should have mentioned that this was inside of a XAML application, and apparently "DataContext" belongs to the FrameworkElement, which apparently is part of XAML (I'm brand new to XAML).

I've just never seen initialization of a class instance done via casting?

more context
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
I am not sure if you have used VB.Net or some other language but VB.Net does have a similar operator

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/trycast-operator
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
InspectionsByTasksViewModel vm is just the declaration of a variable. A variable can point to any previously instantiated data, or the data can be "new'd" up inline. In your case, the variable is used to hold whatever is in the DataContext--maybe that's a InspectionsByTasksViewModel and maybe it's LibraryBook...who knows? DataContext isn't strongly typed, which means it can hold anything. The author of the code is using the safe-cast operator as to ensure that no exception is encountered. You can see in the very next line the author checks that vm is not equal to null. This is due to the behavior of as--as Chinmay already mentioned:  If the thing on the left of as can be cast as the thing on the right of as, then the cast succeeds; otherwise, null is assigned to the variable on the left of the equals sign.

See:  https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/as
Tom KnowltonWeb developer

Author

Commented:
Thank you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial