We help IT Professionals succeed at work.

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

Tom Knowlton
Tom Knowlton asked
on
101 Views
Last Modified: 2018-10-15
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

Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

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.
CERTIFIED EXPERT
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
Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

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
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Tom KnowltonWeb developer

Author

Commented:
Thank you!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions