Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing ListBox.SelectedIndex 1

Status
Not open for further replies.

Crystalyzer

Technical User
Mar 28, 2003
218
I have a listbox that is populated by a dataset and I am looking for a way to store the selectedindices so that I can compare then to see what has changed. Based on the changes I want to delete a record (from other data not the list)if it is no longer checked and add a record (to other data) if a new item is checked. If an item stays checked I don't wish to do anything. Could anyone point me in the direction of where I can store the initially selected values for comparison?

Thanks and best regards,
-Lloyd
 
Okay, got home and after running it though the IDE, I'm starting to remember that day in class. I beleive my exact quote was "That's [bleeping] retarded" back then. That was in reference to the difference in how .Net handles primative types and user defined types.

Check out this code sample:
Code:
Public Class Form1
    Inherits System.Windows.Forms.Form

'Windows gen code

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim x1 As New ThisClass
        Dim y1 As New ThisClass

        Dim x As Integer = 0
        Dim y As Integer = 0

        x1.Value = 0
        y1.Value = 0

        DoThingByVal(x1)
        DoThingByRef(y1)

        DoThingByVal(x)
        DoThingByRef(y)

        MsgBox("Custom Class: " & Environment.NewLine & _
               " X: " & x1.Value & "   Y: " & y1.Value & Environment.NewLine & _
               "Primative Types: " & Environment.NewLine & _
               " X: " & x & "   Y: " & y)
    End Sub

    Public Sub DoThingByVal(ByVal Value As ThisClass)
        Value.Value += 1
    End Sub

    Public Sub DoThingByRef(ByRef Value As ThisClass)
        Value.Value += 1
    End Sub


    Public Sub DoThingByVal(ByVal Value As Integer)
        Value += 1
    End Sub

    Public Sub DoThingByRef(ByRef Value As Integer)
        Value += 1
    End Sub
End Class

Public Class ThisClass
    Private m_val As Integer
    Public Property Value()
        Get
            Return m_val
        End Get
        Set(ByVal Value)
            m_val = Value
        End Set
    End Property
End Class
(tested)

Although we do the exact same thing to both sets of variables, the primative types will come out as X: 0 Y:1 while the user defined types will both come out at 1.

And I think I'll stand by my original assessment, that this behavior is infact, [bleeping] retarded.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
<this behavior is infact, [bleeping] retarded.

No argument here! Thanks for testing in .Net, Rick. This is the same way that things behave in VB6, by the way.
 
It shows you a bit of insight on how custom objects actually perform. They are only handling memory address, so even though they are duplicating the value of the variable, that value only actually contains memory addresses to other objects.

What it should (IMO) do is cycle through the tree of memory addresses and contain the entire data object. Of course that would slow and method call that includes a ByVal variable, but atleast the bahavior would be correct/consistant.

Can anyone with 2k5 (framework 2) try this out, see if the behavior is the same.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Could this be described as an example of Shallow copy vs. deep copy?
 
To continue this debate. I'm sitting in an ASP.Net training class and we just happened to touch on Classes vs Structures. One of the things the teacher mentioned was that structs are entirely stacked based where as classes as stack based memory addresses to the heap. Which made me wonder about the ByVal behavior and sure enough, Structs work correctly!

Try this code:
Code:
Public Class Form1
    Inherits System.Windows.Forms.Form

'#Region " Windows Form Designer generated code "

    Private Structure MyStruct1
        Public MyInteger As Integer
    End Structure

    Private Class MyClass1
        Public MyInteger As Integer
    End Class

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As MyStruct1
        x.MyInteger = 1
        DoThing(x)
        MsgBox("Structure ByVal: " & x.MyInteger)

        Dim y As New MyClass1
        y.MyInteger = 1
        DoThing(y)
        MsgBox("Class ByVal: " & y.MyInteger)
    End Sub

    Private Sub DoThing(ByVal AnObject As MyStruct1)
        AnObject.MyInteger += 1
    End Sub

    Private Sub DoThing(ByVal AnObject As MyClass1)
        AnObject.MyInteger += 1
    End Sub
End Class

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top