user structures as arraylist?

Private Structure TransactionHistoryStructure
Public TransactionDate As Date
Public TransactionType As String
Public TransactionAmount As Double
End Structure

'HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
Private TransactionHistory As TransactionHistoryStructure

thats what i have, i want to make the TransactionHistory an array list of TransactionHistoryStructure 's
 
You can make a strongly-typed collection by making a class which inherits CollectionBase and then overriding the Item property, the Add method, and the Remove method (those are the bare minimums - you can override other things too, like the Contains method too) and making their parameters and return values TransactionHistoryStructure instead of just Object.

Here's a sample of a strongly-typed collection which only accepts Product object:
Visual Basic:
Public Class ProductCollection : Inherits CollectionBase
    Public Function Add(ByVal obj As Product) As Integer
        Return Me.List.Add(obj)
    End Function

    Public Sub Remove(ByVal obj As Product)
        If Me.List.Contains(obj) Then Me.List.Remove(obj)
    End Sub

    Default Public Property Item(ByVal index As Integer) As Product
        Get
            Return DirectCast(Me.List.Item(index), Product)
        End Get
        Set(ByVal Value As Product)
            Me.List.Item(index) = Value
        End Set
    End Property
End Class
 
i guess i am a dummy, but i dont quite understand gotta rub my nose in it i guess

this is what i did

Public Class TransactionHistoryStructureCollection : Inherits CollectionBase
Public Structure TransactionHistoryStructure
Public TransactionDate As Date
Public TransactionType As String
Public TransactionAmount As Double
End Structure

Public Function Add(ByVal obj As TransactionHistoryStructure) As Integer
Return Me.List.Add(obj)
End Function

Public Sub Remove(ByVal obj As TransactionHistoryStructure)
If Me.List.Contains(obj) Then Me.List.Remove(obj)
End Sub

Default Public Property Item(ByVal index As Integer) As TransactionHistoryStructure
Get
Return DirectCast(Me.List.Item(index), TransactionHistoryStructure)
End Get
Set(ByVal Value As TransactionHistoryStructure)
Me.List.Item(index) = Value
End Set
End Property
 
You don't need to put the Structure *inside* the collection class. Other than that, it looks like it should work.
 
where should the structure go? this goes in the same file as the rest of my class, is it inbeaded in or seperate?
 
You shouldn't be using structures with ArrayLists. Tons of boxing and unbox is not good. You're better off turning your structure into a class.

In any case, using a strucutre inside an ArrayList is the exact same as using any other thing inside an ArrayList.

C#:
// Your structure. This should really be a class
// if you plan to use it with ArrayLists and not arrays.
public struct MyStruct 
{
   public int Total = 0;
}

// Instantiate an ArrayList.
ArrayList list = new ArrayList();
// Add structure to list.
list.Add(MyStruct);

// Loop through list and print out struct info.
foreach MyStruct s in list {
   Console.Writeline(s.Total);
}
 
As long as they are in a separate assembly (separate project) you can, since it's all just .NET IL in the end.
 
You can use C++.NET DLLs, but regular C++ DLLs require you to use P/Invoke to interop with them.
 
thanks :)


when using c++.net consol programs has anyone noticed a lag when they run? and does anyone know how to make the dos window wait after execution?
 
Back
Top