Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have a structure that i try to case in background worker, but no success i get
private Jobs as list(of jobitems)
Structure JobItem
        Dim Index As Integer
        Dim Url As String
        Dim Status As String
End Structure


i get this error

TryCast' operand must be reference type, but is a value type


in background worker

 Private Sub DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
        Dim worker As BackgroundWorker = TryCast(sender, BackgroundWorker)
        Dim workItem As String = TryCast(e.Argument, String)

        If worker IsNot Nothing AndAlso workItem IsNot Nothing Then
            'Process the work item here.
            Dim jb As New JobItem
            jb.Status = "Starting" & Jobs(CInt(workItem)).Url
            worker.ReportProgress(CInt((Jobs.Count / 100) * urlIndex), jb)
        End If
    End Sub


Private Sub ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs)

 Dim jb As JobItem = TryCast(e.UserState, JobItem)

End Sub


how do i fix this,

thanks
Posted

1 solution

Hi, you have to use CType instead of TryCast. TryCast expects the parameters that you have passed to of Reference Type but JobItem is a Structure which is a Value type and hence the error. Which is not the case with CType.

This should work now:
VB
Private Sub ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs)
 Dim jb As JobItem = CType(e.UserState, JobItem)
End Sub


:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900