Passing Arrays

Passing an array from the UI to an object

Page 1 of 1

4 Replies - 3152 Views - Last Post: 09 October 2007 - 12:34 PM Rate Topic: -----

#1 aceofspades686   User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Passing Arrays

Posted 09 October 2007 - 12:19 AM

Honestly, I'll probably feel silly for asking this after I get some sleep, but here goes.

I'm currently working on a personal project for my anime club that will simplify our member record, attendance, and fee tracking by saving it to an XML file. My problem is this, I have 3 classes that I need to pass an array to as a property, and I'm honestly not sure where to even start with this one. Two of the classes are there just to hold information for a certain aspect (attendance and dues respectively) and one is a class that writes the XML file.

Just as a bit more info, of the two holding info, one will be a single dimensional array holding integers (member numbers for attendance to be more specific) and the other will be a 2d array that holds decimal values (member numbers and amount paid for fees). The one going to the XML-creator class will actually be 3 different arrays (which probably shouldn't be arrays, I'm honestly a bit rusty with this aspect of VB.NET) holding objects created from 3 other classes (they're then going to be broken down from arrays, to individual objects, then each property of the objects will make up the content of the XML).

As I said, I probably just need to sleep on it, but if someone could point me in the right direction with some code snippets or tutorials it would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Passing Arrays

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Passing Arrays

Posted 09 October 2007 - 12:43 AM

If it were me I wouldnt go this route, I would do some reading on:

Then create 2 custom classes, one that implements IComparable, like such:

<Serializabel()> _
Public Class Members : Implements IComparable
#Region " Class Variables "
	'Property Variables
	Private _iID As Integer
	Private _feesPaid As Double
#End Region

#Region " Class Properties "
Public Property MemberID() As Integer
	Get
		 Return _iID
	End Get
	Set(ByVal value As Integer)
		_iID = value
	End Set
End Property

Public Property FeesPaid() As Double
	Get
		Return _feesPaid
	End Get
	Set(ByVal value As Double)
		_feesPaid = value
	End Set
End Property

#End Region

#Region " Class Methods"
	Public Sub New(ByVal iID As Integer, fees As Double)
		Me._iID = iID
		Me._feesPaid = fees
	End Sub

	Public Sub New()
		Me._iID = 0
		Me._feesPaid = 0.00#
	End Sub

	Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
		If Not (TypeOf obj Is Members) Then
			Throw New ArgumentException
		End If

		Dim f2 As Members = CType(obj, Members)
		Dim cmpl As Integer = Me.ID.CompareTo(f2.Members)
		If Not (cmpl = 0) Then
			Return cmpl
		End If
		Return Me.ID.CompareTo(f2.Members)
	End Function
#End Region
End Class



Then one that Inherits CollectionBase, like so:

<Serializable()> _
Public Class MemberCollection : Inherits CollectionBase
	Public Sub New()
	End Sub

	Public Sub Add(ByVal i As Members)
		Me.InnerList.Add(i)
	End Sub

	Default Public Property Info(ByVal idx As Integer) As Members
		Get
			Return CType(Me.InnerList(idx), Members)
		End Get
		Set(ByVal value As Members)
			Me.InnerList(idx) = value
		End Set
	End Property

	Public ReadOnly Property Members() As Collection
		Get
			Dim oCollection As New Collection
			For i As Integer = 0 To Me.InnerList.Count - 1
				oCollection.Add(Me.InnerList.Item(i))
			Next
			Return oCollection
		End Get
	End Property

	Public Function Contains(ByVal i As Members) As Boolean
		For Each i2 As Members In Me.InnerList
			If i2.CompareTo(i) = 0 Then
				Return True
			End If
		Next
		Return False
	End Function

	Public Sub Remove(ByVal i As Members)
		If Not Me.Contains(i) Then
			Exit Sub
		End If

		Dim iCnt As Integer = 0
		While iCnt < Me.InnerList.Count
			Dim i2 As Members = CType(Me.InnerList(iCnt), Members)
			If i2.CompareTo(i) = 0 Then
				Me.RemoveAt(iCnt)
				Exit Sub
			End If
			System.Math.Min(System.Threading.Interlocked.Increment(iCnt), iCnt - 1)
		End While
	End Sub

	Public Function ToArray() As Members()
		Return CType(Me.InnerList.ToArray(GetType(Members)), Members())
	End Function
End Class



When you inherit from CollectionBase it gives you a lot more functionality than just a standard Array, like Contains (checks to ensure the current member isnt already in the list), and Add and Remove, along with the ToArray option.

To use this would be something like:


Dim oMember As New Members()
Dim oList As New MemberCollection()

oMember.ID = 112233
oMember.FeesPaid = 123.00#

If Not oList.Contains(oMember) Then
	 oList.Add(oMember)
End If



Of course when add the members into the collection you would be in a loop, ensuring you got all the members. Since we added the <Serializable()> Tag this can be serialized, and passed from class to class as needed.

That is the way I would do it. Look into the items I suggested, and if you have any trouble with this implementation then post (in this thread, no need to start a new one) and Ill help as much as I can :)
Was This Post Helpful? 0
  • +
  • -

#3 aceofspades686   User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Passing Arrays

Posted 09 October 2007 - 01:17 AM

Thanks for your fast reply, I was actually about to get some sleep in before I saw it and thought I should take a look.

I'm going to take a quick crack at this before I go to bed to make sure I understand how this would work out.

From what I can see, this is working with the fees aspect of the program, and not the others (just to make sure I'm on the same page) and the Members class is used to create an object that contains the member number and the amount paid, then its added to MembersCollection to create the "2d list" (for lack of better terms).

Just wanting to make sure I'm following all this before I try to implement it (I'll read over it tomorrow and understand a little better more than likely, I think my brain just fizzled out on me). Thankfully I'm still in the planning process more or less, so if I can wrap my mind around the concepts then I should be able to use it.
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Passing Arrays

Posted 09 October 2007 - 05:46 AM

You can actually add as many properties as you like to the Members Class, I simply used ID and FeesPaid as an example. Once populated it would be better than a 2 dimensional array of the same data, and offer much more flexibility and future scalability to your project, along with more functionality than offered from a simple array.
Was This Post Helpful? 0
  • +
  • -

#5 aceofspades686   User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Passing Arrays

Posted 09 October 2007 - 12:34 PM

Alright, I was actually right about one thing, I understood it a lot better after having slept for a bit, and I see what you mean about this being more flexible than using arrays. Thanks for the help and I'll post more in this thread if I run into another snag.

EDIT: After sitting down and reading into IComparable and CollectionBase I realized that you actually helped me with another problem that I hadn't even hit yet with the member records. Thanks a bunch for the help!

This post has been edited by aceofspades686: 09 October 2007 - 08:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1