Function with different types of arguments
-
Hi, I have a function that populates a list with items of type 'RemoteCustomFieldValue'. Now I would like to extend this function in a way that the list would accept items of different type 'RemoteFieldValue'. I thought this would work like in the snippet but I get the following error in the if-statement: 'The expression of type "System.Collections.Generic.List(Of T)" can never be of type "System.Collections.Generic.List(Of legion.SST.Jira.RemoteCustomFieldValue)".' And another one when adding the item to the list 'The value of type "legion.SST.Jira.RemoteFieldValue" can not be converted to "T"' How can I solve this problem? Perhaps is there another way to do it. Many thanks in advance
Private Sub GetUserInputValues(Of T)(ByRef alRemoteCustomFieldValue As List(Of T))
.... **If TypeOf alRemoteCustomFieldValue Is List(Of RemoteCustomFieldValue) Then ** Dim rcfvUsrInput As RemoteCustomFieldValue = New RemoteCustomFieldValue rcfvUsrInput.customfieldId = ContentCtrl.ID rcfvUsrInput.values = s.ToArray alRemoteCustomFieldValue.Add(rcfvUsrInput) Else Dim rcfvUsrInput As RemoteFieldValue = New RemoteFieldValue rcfvUsrInput.id = ContentCtrl.ID rcfvUsrInput.values = s.ToArray **alRemoteCustomFieldValue.Add(rcfvUsrInput)** End If End If
End Sub
-
Hi, I have a function that populates a list with items of type 'RemoteCustomFieldValue'. Now I would like to extend this function in a way that the list would accept items of different type 'RemoteFieldValue'. I thought this would work like in the snippet but I get the following error in the if-statement: 'The expression of type "System.Collections.Generic.List(Of T)" can never be of type "System.Collections.Generic.List(Of legion.SST.Jira.RemoteCustomFieldValue)".' And another one when adding the item to the list 'The value of type "legion.SST.Jira.RemoteFieldValue" can not be converted to "T"' How can I solve this problem? Perhaps is there another way to do it. Many thanks in advance
Private Sub GetUserInputValues(Of T)(ByRef alRemoteCustomFieldValue As List(Of T))
.... **If TypeOf alRemoteCustomFieldValue Is List(Of RemoteCustomFieldValue) Then ** Dim rcfvUsrInput As RemoteCustomFieldValue = New RemoteCustomFieldValue rcfvUsrInput.customfieldId = ContentCtrl.ID rcfvUsrInput.values = s.ToArray alRemoteCustomFieldValue.Add(rcfvUsrInput) Else Dim rcfvUsrInput As RemoteFieldValue = New RemoteFieldValue rcfvUsrInput.id = ContentCtrl.ID rcfvUsrInput.values = s.ToArray **alRemoteCustomFieldValue.Add(rcfvUsrInput)** End If End If
End Sub
Even if you could have the TypeOf ... is ... condition you were trying for, the code inside that if block will still fail. Just because you have made a check to ensure that the contents of a variable is a certain type, the compiler will still treat the variable as if it is the base type. You would need to put something like this:
Private Sub GetUserInputValues(Of T)(ByRef alRemoteCustomFieldValue As List(Of T))
....
If TypeOf alRemoteCustomFieldValue Is List(Of RemoteCustomFieldValue) Then
'make a variable of the type just checked for and cast the original to it
Dim customList As List(Of RemoteCustomFieldValue) = _
CType(alRemoteCustomFieldValue, List(Of RemoteCustomFieldValue)
Dim rcfvUsrInput As RemoteCustomFieldValue = New RemoteCustomFieldValue
rcfvUsrInput.customfieldId = ContentCtrl.ID
rcfvUsrInput.values = s.ToArray
'use the cast variable
customList.Add(rcfvUsrInput)
Else
....The problem stems mainly from the fact that generic classes cannot be converted based on the type parameter (eg:
List(of Derived)
cannot be converted toList(of Base)
). You can however create a specialized version of the function for when T is RemoteCustomFieldValue.Private Sub GetUserInputValues(alRemoteCustomFieldValue As List(of RemoteCustomFieldValue))
'special code for this type
End SubHowever, if you have a class
SpecializedFieldValue
that derives fromRemoteCustomFieldValue
, and you have aList(of SpecializedFieldValue)
, the more generic version of the function would be called. Also, unless you intend to completely replace the list in the code that calls the function (not just Clear and Add new items), you should probably pass ByVal instead of ByRef.