scorpin Posted October 7, 2003 Posted October 7, 2003 is it possible to arraylist a user defined structure? Quote
Mehyar Posted October 7, 2003 Posted October 7, 2003 Yes sure you can..., if what you mean by a user defined structure a class... Quote Dream as if you'll live forever, live as if you'll die today
Administrators PlausiblyDamp Posted October 7, 2003 Administrators Posted October 7, 2003 Works with either classes or structures Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
scorpin Posted October 7, 2003 Author Posted October 7, 2003 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 Quote
*Experts* Volte Posted October 7, 2003 *Experts* Posted October 7, 2003 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: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 Quote
scorpin Posted October 7, 2003 Author Posted October 7, 2003 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 Quote
*Experts* Volte Posted October 7, 2003 *Experts* Posted October 7, 2003 You don't need to put the Structure *inside* the collection class. Other than that, it looks like it should work. Quote
scorpin Posted October 7, 2003 Author Posted October 7, 2003 where should the structure go? this goes in the same file as the rest of my class, is it inbeaded in or seperate? Quote
*Experts* Volte Posted October 7, 2003 *Experts* Posted October 7, 2003 It should be separate from any other classes. Quote
scorpin Posted October 9, 2003 Author Posted October 9, 2003 when i go to .add in requires a property but i dont know what to do Quote
wyrd Posted October 10, 2003 Posted October 10, 2003 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. // 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); } Quote Gamer extraordinaire. Programmer wannabe.
wyrd Posted October 10, 2003 Posted October 10, 2003 No you can't mix VB and C# within the same source file. Quote Gamer extraordinaire. Programmer wannabe.
*Experts* Volte Posted October 10, 2003 *Experts* Posted October 10, 2003 As long as they are in a separate assembly (separate project) you can, since it's all just .NET IL in the end. Quote
scorpin Posted October 10, 2003 Author Posted October 10, 2003 that also goes for c++? has vb math been improved? Quote
*Experts* Volte Posted October 10, 2003 *Experts* Posted October 10, 2003 You can use C++.NET DLLs, but regular C++ DLLs require you to use P/Invoke to interop with them. Quote
scorpin Posted October 10, 2003 Author Posted October 10, 2003 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? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.