-
May 23rd, 2024, 02:42 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Generics Return Type
It's been a minute since I have developed in .NET, but I am a bit puzzled by something and thinking I may be doing something dumb. I have a generic function that can return type T, but I am having trouble with the casting. Here is my function:
Code:
Private ReadOnly Property PaddingConverter As TypeConverter = TypeDescriptor.GetConverter(GetType(Padding))
Public Enum MetadataKey
Margins
...
End Enum
Public Function LoadData(Of T)(key As MetadataKey) As T
Select Case key
Case MetadataKey.Margins
'get previously saved padding that was stored using PaddingConverter.ConvertToString
Dim storedMargins = GetPreviouslySavedMargins()
' THIS WORKS - CObj was a suggestion found on web (otherwise says value type Padding cannot be converted to type T)
If String.IsNullOrWhiteSpace(storedMargins) Then Return CType(CObj(Padding.Empty), T)
Dim bar = CType(PaddingConverter.ConvertFromString(storedMargins), Padding)
' this throws an Invalid Cast exception (the specified cast is invalid) - note that if I turn Option Strict OFF and just return CObj(bar), it works
Return CType(CObj(bar), T)
End Select
End Function
I am not sure why CType(CObj(Padding.Empty), T) works, but the latter conversion does not; they are both converting Padding objects, no? I am basically trying to create a generic function that returns a value that may be one or more types (in this case, just trying to get Padding to work).
Last edited by clarkgriswald; May 28th, 2024 at 06:38 PM.
-
May 23rd, 2024, 03:01 PM
#2
Re: Generics Return Type
What happens if you remove the calls to CObj? You shouldn't need to convert to Object to be able to perform a CType.
What is Padding. Empty defined as? There may not be a conversion from a Padding to a T, but if Empty is defined as Nothing or similar that could be converted to a T.
-
May 23rd, 2024, 08:56 PM
#3
Re: Generics Return Type
What does "THIS FAILS" actually mean? What actually happens?
-
May 23rd, 2024, 09:00 PM
#4
Re: Generics Return Type
Also, please explain this line:
Code:
Dim storedMargins = foo()
You're using type inference but you're calling a method that we have no access to, so we can't know the type there. I'm guessing Margin but we should have to guess. Also, what's MetadataKey? You haven't explained how the code at issue fails and we can't test the code you posted because it won't compile. Not great.
-
May 24th, 2024, 10:52 AM
#5
Re: Generics Return Type
Does this make sense as a generic? The function isn't truly generic, as the return is actually fairly specific. After all, the return has to be something that bar can meaningfully be CType'd into, and that isn't just anything, it's Padding (whatever that is). Therefore, the only viable return types is Padding, or something that can be cast to Padding. I would suggest that an interface might be a better approach to this, assuming you have access to Padding and whatever else that bar could viably be. If they all implemented a common interface, then rather than a generic, you could make it a function that returned that interface. It would be both more explicit and more clear. As written, it can't be "whatever" which is what generic usually means.
My usual boring signature: Nothing
 
-
May 28th, 2024, 07:42 AM
#6
Thread Starter
Fanatic Member
Re: Generics Return Type
Sorry for the late reply. I didn't realize I left the tab open, so I wasn't getting any email notifications for the thread. In any event, I updated the code example in original post and included some comments as well.
 Originally Posted by PlausiblyDamp
What happens if you remove the calls to CObj? You shouldn't need to convert to Object to be able to perform a CType.
What is Padding. Empty defined as? There may not be a conversion from a Padding to a T, but if Empty is defined as Nothing or similar that could be converted to a T.
I agree and I didn't use CObj at all until I saw multiple threads suggest to use CObj before casting to T. Padding.Empty is just a padding object consisting of all values of 0.
 Originally Posted by jmcilhinney
Also, please explain this line:
Code:
Dim storedMargins = foo()
You're using type inference but you're calling a method that we have no access to, so we can't know the type there. I'm guessing Margin but we should have to guess. Also, what's MetadataKey? You haven't explained how the code at issue fails and we can't test the code you posted because it won't compile. Not great.
Sorry for missing detail, see updated code above.
 Originally Posted by Shaggy Hiker
Does this make sense as a generic? The function isn't truly generic, as the return is actually fairly specific. After all, the return has to be something that bar can meaningfully be CType'd into, and that isn't just anything, it's Padding (whatever that is). Therefore, the only viable return types is Padding, or something that can be cast to Padding. I would suggest that an interface might be a better approach to this, assuming you have access to Padding and whatever else that bar could viably be. If they all implemented a common interface, then rather than a generic, you could make it a function that returned that interface. It would be both more explicit and more clear. As written, it can't be "whatever" which is what generic usually means.
I think so as this function would return padding, integers, and strings (depending on the enum value passed to the function)
-
May 28th, 2024, 12:02 PM
#7
Re: Generics Return Type
If the return type is dependant on a parameter passed in (not sure if I missed that in your posted code) then I am assuming the generic type (and therefore the return type) would need to match.
Would it not just be easier to have three functions, each with a specific return type instead?
-
May 28th, 2024, 02:00 PM
#8
Thread Starter
Fanatic Member
Re: Generics Return Type
 Originally Posted by PlausiblyDamp
If the return type is dependant on a parameter passed in (not sure if I missed that in your posted code) then I am assuming the generic type (and therefore the return type) would need to match.
Would it not just be easier to have three functions, each with a specific return type instead?
The parameter passed in is just the enum key needed to know which property I am looking to load; so the only thing that is generic is the return type. I could use multiple functions, but thought this was a perfect case to use generics. The weird part is, if Option Infer is off, then returning CObj() of the value works as expected.
-
May 28th, 2024, 04:05 PM
#9
Re: Generics Return Type
 Originally Posted by clarkgriswald
The parameter passed in is just the enum key needed to know which property I am looking to load; so the only thing that is generic is the return type. I could use multiple functions, but thought this was a perfect case to use generics. The weird part is, if Option Infer is off, then returning CObj() of the value works as expected.
Do you have option Strict Off as well?
Returning an Object would allow you to return anything and you are also losing any compile time type checking as well.
-
May 28th, 2024, 06:39 PM
#10
Thread Starter
Fanatic Member
Re: Generics Return Type
 Originally Posted by PlausiblyDamp
Do you have option Strict Off as well?
Returning an Object would allow you to return anything and you are also losing any compile time type checking as well.
In my regular project I have both Strict and Infer ON, but in my sandbox app, I have Infer ON and Strict OFF (misspoke earlier)
-
May 29th, 2024, 08:31 AM
#11
Thread Starter
Fanatic Member
Re: Generics Return Type
I figured out the issue and my apologies for a dumb mistake 
The problem was the call site. I had:
vb.net Code:
dim foo = LoadData(of Padding)(MetadataKey.Margins)
Which looked correct until I realized that there was another Type called Padding and I needed to be more specific:
vb.net Code:
dim foo = LoadData(of Windows.Forms.Padding)(MetadataKey.Margins)
Once I made that change, all worked.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|