Solved

Class Instantiation for multiple arrays

Posted on 2011-09-26
3
191 Views
Last Modified: 2012-05-12
Hi all

Given the class below..
I need to create an instance of it and populate it with data.

I.e
Invoice varInvoice = new Invoice

varInvoice(FileType varXML, int varTransactionID, byte Data  )

given that byte is read from file..  and Given that I need to create and array type  Invoices[] varInvoices,
i.e Invoices[] will contain list of different invoices.. 1..n invoices..

Thanks in Advnace
public partial class Invoice {
        
        private string fileTypeField;
        
        private string transactionIDField;
        
        private byte[] dataField;
        
        /// <remarks/>
        public string FileType {
            get {
                return this.fileTypeField;
            }
            set {
                this.fileTypeField = value;
            }
        }
        
        /// <remarks/>
        public string TransactionID {
            get {
                return this.transactionIDField;
            }
            set {
                this.transactionIDField = value;
            }
        }
        
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(DataType="base64Binary")]
        public byte[] Data {
            get {
                return this.dataField;
            }
            set {
                this.dataField = value;
            }
        }
    }

Open in new window

0
Comment
Question by:ZURINET
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
ID: 36599861
I'm not sure I understand your question in whole.  You just want to instantiate a list of the class?

Just use the List<T> generic:

List<Invoice> invoiceList = new List<Invoice>();

Your constructor could assign the values:

public Invoice( string fileType, string transID, byte[] data )
{
   this.fileTypeField = fileType;
   this.transactionIDField = transID,
   this.dataField = data;
}

You would have to instantiate the byte[] data and fill it before instantiating the class with the constructor.

To fill in all the invoices, just loop them:

for ( int i = 0; i < somecount; i++ )
{
   // Get the FileType, TransID, data, etc...
   Invoice inv = new Invoice( fileType,  transID, data );
   if ( !invoiceList.Contains( inv ) )
   {
      invoiceList.Add( inv );
   }
}

That will fill in your List (typed array) of invoices.  Is this what you're looking for?
0
 

Author Comment

by:ZURINET
ID: 36599893

Hi Dan7el
Given that I have a requirement to use array i.e Invoices []

How would that look like?

Thanks
0
 
LVL 19

Expert Comment

by:Daniel Van Der Werken
ID: 36600702
Well, you'd have to know how many elements of the array you needed before hand, or have some mechanism for "dynamically" allocating new elements for each array.  That can be done of course, it's just a tad messy.

Given the same construcor:

int numElements = 60;
Invoice[] invoices = new Invoice[60];

for( int index = 0; index < numElements; index++ )
{
   // again, get the data and stuff.
   invoices[index] = new Invoice( fileType, transID, data );
}
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now