C
C#3y ago
TSJN

✅ Type Casting

Hi, Just wondering how I would type cast the object 'control' to the type 'telerikslider'?
12 Replies
JimmahDean
JimmahDean3y ago
var foo = (TelerikSlider<>)control; you'd want the variable to be initialized outside of the foreach though
Monsieur Wholesome
use pattern matching
if(control is TelerikSlider<> telerikControl)
{

}
if(control is TelerikSlider<> telerikControl)
{

}
JimmahDean
JimmahDean3y ago
TelerikSlider<> foo;
foreach(...)
{
foo = (TelerikSlider<>)control;
}
TelerikSlider<> foo;
foreach(...)
{
foo = (TelerikSlider<>)control;
}
ish i had no idea you could do this this is excellent don't monkaS me 😛
TSJN
TSJNOP3y ago
Thanks for the help. Learned 2 different ways. Even better!
TheBoxyBear
TheBoxyBear3y ago
Won't work for a generic without a type
TSJN
TSJNOP3y ago
As in the pattern matching method?
TheBoxyBear
TheBoxyBear3y ago
You can get a Type object with typeof without a generic but if you want to cast in any way, you need the generic Not sure even == typeof(TelerikSlider<>) would work as no object can be of that type You need the type that goes in <>, unless there's a base TelerikSlider type
TSJN
TSJNOP3y ago
That's what ultimately led me to here as every example is with a generic
TheBoxyBear
TheBoxyBear3y ago
Check the metadata or docs to see if it derives from TelerikSlider and if so, use that If not, you'll need to make the context itself generic and use T in TelerikSlider In that latter scenario, the caller is responsible of providing the generic type Though not sure if you can do that in cshtml
TSJN
TSJNOP3y ago
Turns out it needed an argument in the angle brackets of the type. Question now is, how do I store/save the value to the SavedValue field in the UserSetting class?
TheBoxyBear
TheBoxyBear3y ago
Yes, that's the generic type
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?