Array of Objects Initialization problem C#.

S

sk.rasheedfarhan

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,RuleNodes[j]); }
}
}

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

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

Daniel

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.
 
J

Joanna Carter [TeamB]

<[email protected]> a écrit dans le message de (e-mail address removed)...

| 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

The why don't you use a dynamic list ? Use ArrayList in .NET 1.1 or List<T>
in .NET 2.0.

Joanna
 
T

Tim Van Wassenhove

(e-mail address removed) schreef:
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.

To me it seems that you might want to consider a List<T> instead (or
ArrayList).

http://msdn2.microsoft.com/en-us/library/6sh2ey19.aspx

Step1: RuleXMLParser [] RulesProperties;

Step2: RulesProperties = new RuleXMLParser[1000];

RulesProperties = new List<RuleXMLParser(15); // Since you said you
usually need around 10 objects, we'll set the initial size to 15...
Step3: RulesProperties[m_icount] = new RuleXMLParser();

Here you are initializing a new RuleXMLParser.. for the m_icount-1 _th
element in the array... (When you use a List instead, the syntax is
completely identical).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top