how to Create array of objects

  • Thread starter Thread starter John Haigh
  • Start date Start date
J

John Haigh

I have the need to create an array of objects. Now this sound fairly trivial
but I can't figure this out.

I have one class called PostingObjectService that has a method GetPostings
where the problem begins (class is below) where in the foreach loop I need
to add Postings to the PostingObject[] array which will be returned by this
method. Then I have the class PostingObject where I am unsure how to create
the array or add to the array of PostingObjects.

Please help.

Thanks, John

PostingObjectService.cs
using System;
using System.Collections;
using Microsoft.ContentManagement.Publishing;

namespace test.CMS.library
{
/// <summary>
/// Summary description for PostingService.
/// </summary>
public class PostingObjectService
{
public PostingObjectService()
{
//
// TODO: Add constructor logic here
//
}
public PostingObject[] GetPostings(string sChannelGuid, DateTime
dtStartDate, DateTime dtEndDate)
{
PostingObject[] oReturnedCol = null;

//Get the current channel (News channel) based on the passed GUID to the
method
Channel aChannel = CMSAPI.GetChannel(sChannelGuid);

PostingCollection oPostCol = aChannel.Postings;
oPostCol.SortByStartDate( false );

foreach ( Posting oPost in oPostCol )
{
// If month has changed
PostingObject oNewMonth = new PostingObject( dtStartDate, dtEndDate );
//oNewMonth.objPosting[0].
//oNewMonth
//oPost.StartDate
}
return oReturnedCol;

}
}
}

PostingObject.cs
using System;
using System.Collections;
using Microsoft.ContentManagement.Publishing;


namespace test.CMS.library
{
/// <summary>
/// Summary description for PostingObject.
/// </summary>
public class PostingObject
{



#region Instance variables
internal Posting[] objPosting = null;


#endregion

#region Constructor
public PostingObject()
{
}

// start date
public PostingObject(DateTime StartDate)
{
// Do I add to the array of Postings here?
objPosting.
}



// start date, end date
public PostingObject(DateTime StartDate, DateTime EndDate)
{
// Do I add to the array of Postings here?
}

#endregion


public String StartDate
{
get
{
if(objPosting != null)
{
return objPosting.StartDate.ToString();
}
else
{
return null;
}


}
}

public String EndDate
{
get
{
if(objPosting != null)
{
return objPosting.ToString();
}
else
{
return null;
}

}
}

} //END class
}// END namespace
 
Is it true that Postings and the PostingObject[] will not be 1:1? If so,
create an arraylist, then copy that list to an array.


public PostingObject[] GetPostings(string sChannelGuid, DateTime
dtStartDate, DateTime dtEndDate)
{
PostingObject[] oReturnedCol = null;
ArrayList ret = new ArrayList();

//Get the current channel (News channel) based on the passed GUID to the
method
Channel aChannel = CMSAPI.GetChannel(sChannelGuid);

PostingCollection oPostCol = aChannel.Postings;
oPostCol.SortByStartDate(false);

foreach (Posting oPost in oPostCol)
{
// ??? If > start date and < end date ???
PostingObject post = new PostingObject( dtStartDate, dtEndDate );
ret.Add(post );
}

oReturnedCol = new PostingObject[ret.Count];
return ret.CopyTo(oReturnedCol, 0);
}

If PostingObject[] is 1:1 with Postings then you can dimension the array
after aChannel.Postings and fill the array in the loop:
public PostingObject[] GetPostings(string sChannelGuid, DateTime
dtStartDate, DateTime dtEndDate)
{
PostingObject[] oReturnedCol = null;
ArrayList ret = new ArrayList();

//Get the current channel (News channel) based on the passed GUID to the
method
Channel aChannel = CMSAPI.GetChannel(sChannelGuid);

PostingCollection oPostCol = aChannel.Postings;
oPostCol.SortByStartDate(false);

oReturnedCol = new PostingObject[oPostCol .Count];
for (int i = 0; i < oPostCol.Count; i++)
{
// ??? If > start date and < end date ???
PostingObject post = new PostingObject( dtStartDate, dtEndDate );
oReturnedCol = post;
}

return oReturnedCol;
}

Obviously I don't know if this will compile, but that should give you a few
ideas.

HTH;
Eric Cadwell
http://www.origincontrols.com
 
Yes, Postings and the PostingObject[] are 1:1.

It does help. I am stuck on the fact that I possibly should have an add
method in the PostingObject class and that my constructors in PostingObject
should call this add method? Does this make sense? My sense is that a
PostingObject needs a Posting, so would I do this in the PostingObject
Constructor?

Thanks for your help.

John
 
You could pass the Posting or Posting[] into the PostingObject constructor.

Sounds like you need a strongly typed collection. Look at inheriting from
CollectionBase.
You may also want to parameterize your call to get the postings to include
the date range. That might simplify things a bit.

HTH;
Eric Cadwell
http://www.origincontrols.com
 
Back
Top