Solved

Class Instantiation for multiple arrays

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get the web content with different encodings 2 44
Code works but it's slow 28 70
C# Gridview 1 28
Problem with SelectList in .NET MVC application 1 11
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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 …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

13 Experts available now in Live!

Get 1:1 Help Now