C
C#2y ago
TSJN

✅ Type Casting

Hi, Just wondering how I would type cast the object 'control' to the type 'telerikslider'?
12 Replies
JimmahDean
JimmahDean2y 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
JimmahDean2y 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
TSJNOP2y ago
Thanks for the help. Learned 2 different ways. Even better!
TheBoxyBear
TheBoxyBear2y ago
Won't work for a generic without a type
TSJN
TSJNOP2y ago
As in the pattern matching method?
TheBoxyBear
TheBoxyBear2y 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
TSJNOP2y ago
That's what ultimately led me to here as every example is with a generic
TheBoxyBear
TheBoxyBear2y 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
TSJNOP2y 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
TheBoxyBear2y ago
Yes, that's the generic type
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server