Link to home
Start Free TrialLog in
Avatar of James_fl
James_fl

asked on

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?
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

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

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
Avatar of James_fl
James_fl

ASKER

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
Please disregard the Item() property

James
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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?
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.
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.