Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have this small structure that i use only to manage the contents of some comboboxes instead of a database, by reading from an XML file, but each time the index of another combobox is changed, the contents of the whole combobox gets cleared and refilled, obviously with new structure objects.

At doing this, i know i am removing all the structures from the comboboxes item's list, but the objects are not disposed, as far as i know.

I've created an structure because I read that a structure is good when you are going to instantiate too many small classes, because they are faster at stacking in memory...
In any case im not very familiarized with the structures and also i was trying the new knowledge, so i want to know both things:

Should I make the structure disposable or the GC does it automatically, and also; is this a good use for a structure, or is better to make it a class?

VB
Friend Structure FileRef

    Private _FileName, _Extension, _Path As String

    Public Property Display As String

    Public ReadOnly Property Path As String
        Get
            Return Me._Path
        End Get
    End Property
    Public ReadOnly Property FileName As String
        Get

            Return Me._FileName
        End Get
    End Property
    Public ReadOnly Property Extension As String
        Get


            Return Me._Extension
        End Get
    End Property

    Public Sub New(ByVal path As String)

        Me._Path = path
        Me._Extension = System.IO.Path.GetExtension(Me._Path)
        Me._FileName = System.IO.Path.GetFileName(Me._Path)
    End Sub
    Public Sub New(ByVal path As String, ByVal AutoDisplay As Boolean)

        Me._Path = path
        Me._Extension = System.IO.Path.GetExtension(Me._Path)
        Me._FileName = System.IO.Path.GetFileName(Me._Path)
        If AutoDisplay = True Then _
          Me.Display = System.IO.Path.GetFileNameWithoutExtension(Me.Path)
    End Sub
    Public Sub New(ByVal path As String, ByVal display As String)

        Me._Path = path
        Me._Extension = System.IO.Path.GetExtension(Me._Path)
        Me._FileName = System.IO.Path.GetFileName(Me._Path)
        Me.Display = display
    End Sub

    Public Overrides Function ToString() As String

        If Me.Display = Nothing Then Return Me.Path Else Return Me.Display
    End Function

End Structure


I use it like this:

VB
Private Sub cboCollection_SelectedIndexChanged(sender As Object, e As EventArgs) _
                                    Handles cboCollection.SelectedIndexChanged

        If cboCollection.SelectedIndex >= 0 Then

            Dim SelItem As FileRef = DirectCast _
                                    (cboCollection.SelectedItem, FileRef)

            cboCollection.Items.Clear()
            cboImage.SelectedIndex = -1

            Using fs As New FileStream(SelItem.Path,_
                          FileMode.Open, FileAccess.Read, FileShare.Read)
                Using xmlR As XmlReader = XmlReader.Create(fs)
                   
                    Try 

                    'Code that reads the xml 
                    'and creates in iteration the FileRef objects
                    'to then add them 1 by 1 to another combobox

                    Catch ex As Exception

                    'Error management
                    End Try
                End Using
            End Using
        End If
End Sub


Thanks in advance and sorry if i had bad english.

PS. I wrote in VisualBasic but answers in C# are also very welcome.
Posted
Updated 10-May-14 15:49pm
v2

1 solution

First of all, you should understand the difference between GC action (finalization and reclaiming managed memory) and finalization. Disposal (System.IDisposable) is a separate mechanism which can be used to clean-up everything. It is often used to dispose unmanaged resource, but it is not the only purpose. Basically, if you don't use unmanaged memory or other resources and you don't know what else the disposal is needed for, chance are, you don't need disposal at all.

As to structures vs classes, Solution 1 is quite inaccurate. [EDIT] After out discussion, its author has removed it. [END EDIT]

You can easily compromise performance using structures instead of classes, and the opposite situation is always possible. Also, you should understand that members of reference types can be of value types, and member of structures (value types) can be of reference types. The question "which one to choose" is simply wrong. Instead of asking such questions, you need to get in-depth understanding of reference and value types. The particular decision depends on many factors, starting from your goals. In my experience, people usually screw up things with structures the way it could be fixed by changing a structure to a class, but this is not related to performance and absolutely does not mean that you should ever ignore the possibility of using structures. You should clearly understand what does what. If you had some particular concerns about it, I would gladly answer.

—SA
 
Share this answer
 
v2
Comments
DamithSL 11-May-14 2:17am    
5+
Sergey Alexandrovich Kryukov 11-May-14 2:24am    
Thank you very much.
—SA
Elighne 11-May-14 2:49am    
Thank you.

I have still much to learn about how reference and value types work exactly, and how they differ one from another so i can choose better (I'm an independent student -of software development-, btw), but as far as i could understand, i thought using a structure was a good choice, and probably i would take your word and ask you every single doubt comming across my mind about the reference and value types, but surely they are still too many, so i will investigate further and later i'll ask again (although some links would not be bad, eh)

While in the other subject, you answer quite good and enough for me, so i thank you again.
Sergey Alexandrovich Kryukov 11-May-14 10:11am    
Decide in every single case and "ask you every single doubt" are different things. As you do have concerns, I would welcome you to ask some questions about the choice related to the topic of your question is some particular case, where you have doubt. If it bring you to understanding how working with value and reference types translates into the decision making process, it would be the best for you. In all cases, after considering of few decisions based on understanding how it works, you experience will let you make such decisions with easy. Note that there are cases when the choice between class and struct is non-critical, so either on would be good enough.

Good luck, call again.

—SA
Joezer BH 12-May-14 3:19am    
5ed!

BTW Sergey - Congratulations on the Author score (you just hit the 10K mark)
:thumb:

Cheers

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900