CType error: Expression is a value and therefore cannot be the target of an assignment.

To convert task to Object this is what I do:
    Dim taskOriginal1 as Task = new task()
    obj1 = Ctype(taskOriginal1, Object)

Another task is converted to Object:
    Dim taskOriginal2 as Task = new task()
    obj2 = Ctype(taskOriginal2, Object)

Then I want to make the TaskOriginal1 to refer to taskOriginal2
    CType(obj1, task) = CType(obj2, task)

But this statement gives an error: Expression is a value and therefore cannot be the target of an assignment.

If I do this instead:
    Dim objTask1 As task = Ctype(obj1, task)
    objTask1 = Ctype(obj2, task)

Three questions:
1. Will objTask1 refer to the taskOriginal2?
2. Why does it gives that error?
3. I thought CType returns a reference. Does it mean I can't change the value of an anonymous reference and why?
James_flAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Hi James,

You don't need a clone method to swap the items in your collection.  Also, your bubble sort is incorrect.  Here is a working example:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private s As New Schedule

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim t As Task
        t = New Task
        t.Start = DateTime.Now().AddDays(2)
        s.add(t)

        t = New Task
        t.Start = DateTime.Now().AddDays(-1)
        s.add(t)

        t = New Task
        t.Start = DateTime.Now()
        s.add(t)

        Debug.WriteLine("Original Order:")
        For Each t In s
            Debug.WriteLine(t.Start.ToString)
        Next

        Debug.WriteLine("Sorted Order:")
        s.sort()
        For Each t In s
            Debug.WriteLine(t.Start.ToString)
        Next
    End Sub
End Class

Public Class Task
    Public Start As DateTime
End Class

Public Class Schedule
    Inherits System.Collections.CollectionBase

    Public Sub add(ByVal t As Task)
        list.Add(t)
    End Sub

    Public Sub sort()
        'Bubble sort
        For i As Integer = 0 To list.Count - 2
            For j As Integer = i To list.Count - 2
                If CType(list.Item(j), Task).Start > CType(list.Item(j + 1), Task).Start Then
                    'Swapping tasks
                    Dim swapTask As Task = list.Item(j)
                    list.Item(j) = list.Item(j + 1)
                    list.Item(j + 1) = swapTask
                End If
            Next
        Next
    End Sub
End Class
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Two questions:

1) What is Task?
2) Why convert it to Object in the first place?

Why not just do the assignment directly?

    obj1 = obj2
    ' <or>
    taskOriginal1 = taskOriginal2

What is the end goal here?  Why do you need a second reference?

~IM

0
 
Bob LearnedCommented:
Yeah, what are you really trying to do here?  What will all that conversion do for you?  It might be easier to accomplish in a different way, if we understood what the requirement or problem is.

Bob
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
James_flAuthor Commented:
Sorry for not making myself clear, the example that I gave you was just the simplied version of what I want to ask.

I have two classes: schedule and task.
Schedule inherits from System.Collections.CollectionBase and holds a collection of tasks.

Schedule exposes a readonly property called Item:
    Public ReadOnly Property Item(ByVal index As Integer) As task
        Get
            Return CType(list.Item(index), task)
        End Get
    End Property

And schedule also has a sort sub that basically sorts the task from the oldest one to the newest one. This sub just basically does a bubble sort.
    Public Sub sort()
        'Bubble sort
        For i As Integer = 0 To list.Count - 2
            For j As Integer = 0 To list.Count - 2
                If CType(list.Item(j), task).start > CType(list.Item(j + 1), task).start Then
                    'Swapping tasks
                    Dim swapTask As task = CType(list.Item(j), task).Clone

                    'ERROR: Expression is a value and therefore cannot be the target of an assignment.
                    CType(list.Item(j), task) = CType(list.Item(j + 1), task)
                    CType(list.Item(j + 1), task) = swapTask
                End If
            Next
        Next

Note: The clone() method is nothing special, it only does a deep cloning.

My question:
1. Why does VB.Net gives that error?
2. How to swap the item?
3. Maybe I can swap the item by doing this:
                Dim currentTask As task = CType(list.Item(j), task)
                Dim nextTask As task = CType(list.Item(j + 1), task)
                If currentTask.start > nextTask.start Then
                    Dim swapTask As task = currentTask.Clone()
                    currentTask = nextTask
                    nextTask = swapTask
                End If
    But by doing that, will the list.Item(j) reflect the new value? (i.e. Will list.Item(j) refer to list.item(j+1) )?
    I'm afraid that it won't if when assigning the value of currentTask, CType() does not return a reference
    (returns a new task object instead)


James
0
 
James_flAuthor Commented:
Please disregard the Item() property

James
0
 
James_flAuthor Commented:
alright thanks Idle_Mind, I've got to try it now.

Please mind me for asking this stupid question, but hey I'm still a newbie in VB.Net!

Can I know why I don't need to use the clone() method?
Doesn't the list.item(j) [and every method and property] returns a reference to the item, hence if the original item value changes, swapTask (as it also points to the same object) will have the new value?

Can anybody also please tell me why does VB.Net give that error message?

James
0
 
James_flAuthor Commented:
Also I would like to know if indeed CType returns a reference, why can't I assign the value of that anonymous reference to another task object?
0
 
James_flAuthor Commented:
Is this how it works?

   CType(Task1, Object) returns an anonymous_reference to Task1
   anonymous_reference = CType(Task2, Object)

sets Task1 to refer to the same Task2 object

Note: I'm such a newbie, so maybe I get all the concepts wrong.
0
 
James_flAuthor Commented:
ok I've got it, actually VB function only returns a value not a reference.
I'm a C++ guy and I think that's weird and inefficient!

Thanks, Idle_Mind for helping out.
0
All Courses

From novice to tech pro — start learning today.