Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Class Instantiation for multiple arrays

Posted on 2011-09-26
3
Medium Priority
?
201 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 2000 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

Technology Partners: 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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

971 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