Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Function with different types of arguments

Function with different types of arguments

Scheduled Pinned Locked Moved Visual Basic
helpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TeachesOfPeaches
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • T TeachesOfPeaches

      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

      G Offline
      G Offline
      Gideon Engelberth
      wrote on last edited by
      #2

      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 to List(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 Sub

      However, if you have a class SpecializedFieldValue that derives from RemoteCustomFieldValue, and you have a List(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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups