I am still new to C# and I've been struggling with various issues on arrays. I've got an array of metadata objects (name value pairs) and I would like to know how to create only the number of "InputProperty" objects that I truly need. In this loop I've arbitrarily set the number of elements to 20 and I try to bail out when the entry becomes null but the web service on the receiving end of this does not like any null elements passed to it:
private Update BuildMetaData(MetaData[] nvPairs) { Update update = new Update(); InputProperty[] ip = new InputProperty[20]; // how to make this "dynamic" int i; for (i = 0; i < nvPairs.Length; i++) { if (nvPairs[i] == null) break; ip[i] = new InputProperty(); ip[i].Name = "udf:" + nvPairs[i].Name; ip[i].Val = nvPairs[i].Value; } update.Items = ip; return update; }
In summary, say I only have 3 namevalue pairs in the above input array? Rather than allocate 20 elements for the array called ip, how can code this so ip is only as big as it needs to be. The update object is passed across another webservice so serialization is important (i.e. I can't use namevaluecollection, etc.).
p.s. Is the only way to followup on a posted question through the "add comments" facility?
Arrays are static so you won't be able to change it's size. You'll need to create the linked list data structure.
To determine the size of your array in bytes, you can use the sizeof operator: int a[17]; size_t n = sizeof(a); On my computer, ints are 4 bytes long, so n is 68. To determine the number of elements in the array, we can divide the total size of the array by the size of the array element.
Basically, the length of an array is the total number of the elements which is contained by all the dimensions of that array. Property Value: This property returns the total number of elements in all the dimensions of the Array. It can also return zero if there are no elements in the array.
The simple answer is that you cannot do this. Once an array has been created, its size cannot be changed. Instead, an array can only be "resized" by creating a new array with the appropriate size and copying the elements from the existing array to the new one.
InputProperty[] ip = new InputProperty[nvPairs.Length];
Or, you can use a list like so:
List<InputProperty> list = new List<InputProperty>(); InputProperty ip = new (..); list.Add(ip); update.items = list.ToArray();
Another thing I'd like to point out, in C# you can delcare your int variable use in a for loop right inside the loop:
for(int i = 0; i<nvPairs.Length;i++ { . . }
And just because I'm in the mood, here's a cleaner way to do this method IMO:
private Update BuildMetaData(MetaData[] nvPairs) { Update update = new Update(); var ip = new List<InputProperty>(); foreach(var nvPair in nvPairs) { if (nvPair == null) break; var inputProp = new InputProperty { Name = "udf:" + nvPair.Name, Val = nvPair.Value }; ip.Add(inputProp); } update.Items = ip.ToArray(); return update; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With