Re: Array of Objects Initialization problem C#.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi

Very hard to understand your problem as i presume english is not your first
language. i think you are saying that you are initialising an array to 1000
because you know what you are filling it with will always be less than that.
But you want to add to the array as you need without prior initialisation?

if so....you have two choices, an array list or a List<>.

with list you do this

List<myObject> myList = new List();

then to add an object you do this

myList.Add(myObject);

with an ArrayList it is very simple as well. So look up arraylists and lists
and you will solve your issue.

P.S. C# allows for using unmanaged (unsafe) code where you can use pointers
in more of a c++ manner. But you don't need this anyway.


<sk.rasheedfarhan@xxxxxxxxx> wrote in message
news:1169379879.296575.97110@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi ,
Here I am new user to C#, my problem is I have to use dynamic Array of
objects. But I heard C# don't support ptrs (using managed code C#
support).

In short i initialized objects of 1000 and I am using is upto 10 or
less. Because of that I find 990 reset of them as un initialized
objects, when I extract the information from the Object it will throw
an exception also and I feel unnecessarily I am wasting of memory.
So I need dynamic allocation of the objects so can any body give me
some samples or suggestion then it will be very help full

Brief discussion of my problem I declare array of Object in 1ST Step
and Initialized in 2nd Step. What I am doing in My 3rd Step I don't
know.

When I am parsing array of Objects I find so many un initialized
objects( as per my step2 means 1000). but actually I storing the data
in object RulesProperties is very less in step3 i.e m_icount.


public class RuleXMLParser
{

Step1: RuleXMLParser [] RulesProperties;
public RuleXMLParser()
{
//Do some initialization
}
public void InitializeRules(XmlNodeList RuleNodes)
{
Step2: RulesProperties = new RuleXMLParser[1000];
for(int i = 1; i < RuleNodes.Count; i++)
{
for(int j = i+1; j< RuleNodes.Count -1; j++)
{
CompareRuleNodes(RuleNodes[i],RuleNodes[j]); }
}
}

}
private void CompareRuleNodes(XmlNode RuleNodes1,XmlNode RuleNodes2)
{

Step3: RulesProperties[m_icount] = new RuleXMLParser();
}



.



Relevant Pages

  • Re: Please Help: Script Logic/Multidimensional Array
    ... var inputs = new Array; ... Given that fileQueuehas just been initialised as an array, ... I presume this stems from your initialising the array with a length - ... Your problems stem from that. ...
    (comp.lang.javascript)
  • Re: strcpy - my implementation
    ... char arrc; ... Since you're initialising an array of integers, ... memset version would simply be wrong. ...
    (comp.lang.c)
  • Re: strcpy - my implementation
    ... Since you're initialising an array of integers, ... If the type were non-integer (e.g. pointer, or floating point, or struct ... integer type can safely be initialized with memset, ...
    (comp.lang.c)
  • RE: initialising a list of variables
    ... initialising a list of variables ... while I can see why - one array initialised by another array of the ... It's open to user error quite easily by not specifying enough on the right ... Or is it just that I'm getting too spoilt by Perl? ...
    (perl.beginners)