Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-30
9
Medium Priority
?
743 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
Comment
Question by:James_fl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
9 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12453940
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
ID: 12454296
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
ID: 12454470
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:James_fl
ID: 12454488
Please disregard the Item() property

James
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 12454913
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
ID: 12455232
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
ID: 12455248
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
ID: 12455277
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
ID: 12455727
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

636 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