Solved

Class Instantiation for multiple arrays

Posted on 2011-09-26
3
194 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 20

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 20

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

763 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