PC Review


Reply
Thread Tools Rate Thread

adding one element to a fixed array []

 
 
damian@gcoders.com
Guest
Posts: n/a
 
      19th Feb 2007
This is my first time posting so I hope i'm in the right place.
I have a project which uses an xsd.exe generated .cs file containing
the class definitions for each of my schema's XML elements.
I am using this stucture in memory to maintain my data, but I have
come across the problem that becuase it uses a fixed array [] and not
an arraylist, it is a bit tricky to manage.
I could modify the generated .cs file but the schema is large and if
the schema changes in the future it would mean reworking it, this does
not seem like the best solution.
So at the moment I have a function like this to add an element to the
array: It does not seem like the most elegant solution. I was
wondering if anyone else had come across this problem. Note
that ..ServiceTable is a property so i can't send it as a ref.

private void NewService(object sender, EventArgs e)
{
ServiceType[] s = ESGMain.ESG.ServiceTable;
Array.Resize(ref s, s.Length + 1);
ESGMain.ESG.ServiceTable = s;
s[s.Length - 1] = new ServiceType();
s[s.Length - 1].serviceID = newServiceID;
}

thanks in advance

 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      19th Feb 2007
An array is always of a fixed length. If you want to use a longer array, you
must build a new array and copy all of the elements from the original array
into it, which is what Array.Resize does.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

I had the same problem once. Fixed it using the same solution.

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> This is my first time posting so I hope i'm in the right place.
> I have a project which uses an xsd.exe generated .cs file containing
> the class definitions for each of my schema's XML elements.
> I am using this stucture in memory to maintain my data, but I have
> come across the problem that becuase it uses a fixed array [] and not
> an arraylist, it is a bit tricky to manage.
> I could modify the generated .cs file but the schema is large and if
> the schema changes in the future it would mean reworking it, this does
> not seem like the best solution.
> So at the moment I have a function like this to add an element to the
> array: It does not seem like the most elegant solution. I was
> wondering if anyone else had come across this problem. Note
> that ..ServiceTable is a property so i can't send it as a ref.
>
> private void NewService(object sender, EventArgs e)
> {
> ServiceType[] s = ESGMain.ESG.ServiceTable;
> Array.Resize(ref s, s.Length + 1);
> ESGMain.ESG.ServiceTable = s;
> s[s.Length - 1] = new ServiceType();
> s[s.Length - 1].serviceID = newServiceID;
> }
>
> thanks in advance
>



 
Reply With Quote
 
damian@gcoders.com
Guest
Posts: n/a
 
      19th Feb 2007
thanks. Glad to know i'm not doign completely the wrong thing

 
Reply With Quote
 
TheSteph
Guest
Posts: n/a
 
      20th Feb 2007
Maybe you can use the ArrayList object....


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> This is my first time posting so I hope i'm in the right place.
> I have a project which uses an xsd.exe generated .cs file containing
> the class definitions for each of my schema's XML elements.
> I am using this stucture in memory to maintain my data, but I have
> come across the problem that becuase it uses a fixed array [] and not
> an arraylist, it is a bit tricky to manage.
> I could modify the generated .cs file but the schema is large and if
> the schema changes in the future it would mean reworking it, this does
> not seem like the best solution.
> So at the moment I have a function like this to add an element to the
> array: It does not seem like the most elegant solution. I was
> wondering if anyone else had come across this problem. Note
> that ..ServiceTable is a property so i can't send it as a ref.
>
> private void NewService(object sender, EventArgs e)
> {
> ServiceType[] s = ESGMain.ESG.ServiceTable;
> Array.Resize(ref s, s.Length + 1);
> ESGMain.ESG.ServiceTable = s;
> s[s.Length - 1] = new ServiceType();
> s[s.Length - 1].serviceID = newServiceID;
> }
>
> thanks in advance
>



 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      20th Feb 2007
I used to fight this battle regularly when using the SOAP/WSE proxies;
if you are doing something similar, then note that WCF allows you
(easily) to either use generated classes (as before), or use a shared
entity assembly on both sides of the wire; this means that your
collections stay collections, etc, and you get to use your component
model on both sides.

Of course, it means you need to be stricter about deciding which logic
goes in which classes (i.e. the shared entity classes shouldn't
contain database code; that should be in a server-only adapter), but
it helps. It also means that you can use the same validation at both
client and server (which is a good thing) while also allowing for the
possibility of ad-hoc callers via generated proxies.

Marc


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array problem: Key words-Variant Array, single-element, type mismatch error davidm Microsoft Excel Programming 7 11th Jun 2011 12:01 AM
Rules for element-by-element product in array multiplication Paul Microsoft Excel Programming 2 22nd Mar 2008 11:42 PM
Array problem: Key words-Variant Array, single-element, type mismatch error davidm Microsoft Excel Programming 1 8th Nov 2005 04:21 AM
Re: Excel2000: Replace array element with element from another array Harlan Grove Microsoft Excel Worksheet Functions 0 10th Sep 2003 07:28 PM
Re: Excel2000: Replace array element with element from another array Peo Sjoblom Microsoft Excel Worksheet Functions 0 10th Sep 2003 02:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:09 AM.