Solved

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

Posted on 2004-10-30
715 Views
Last Modified: 2012-05-05
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?
0
Question by:James_fl
    9 Comments
     
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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
     
    LVL 96

    Expert Comment

    by:Bob Learned
    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
     

    Author Comment

    by:James_fl
    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
     

    Author Comment

    by:James_fl
    Please disregard the Item() property

    James
    0
     
    LVL 85

    Accepted Solution

    by:
    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
     

    Author Comment

    by:James_fl
    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
     

    Author Comment

    by:James_fl
    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
     

    Author Comment

    by:James_fl
    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
     

    Author Comment

    by:James_fl
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now