PC Review


Reply
Thread Tools Rate Thread

Array of Objects Initialization problem C#.

 
 
sk.rasheedfarhan@gmail.com
Guest
Posts: n/a
 
      21st Jan 2007
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();
}

 
Reply With Quote
 
 
 
 
Daniel
Guest
Posts: n/a
 
      21st Jan 2007
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.


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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();
> }
>



 
Reply With Quote
 
Joanna Carter [TeamB]
Guest
Posts: n/a
 
      21st Jan 2007
<(E-Mail Removed)> a écrit dans le message de news:
(E-Mail 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

--
Joanna Carter [TeamB]
Consultant Software Engineer


 
Reply With Quote
 
Tim Van Wassenhove
Guest
Posts: n/a
 
      21st Jan 2007
(E-Mail 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;


List<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).


--
Tim Van Wassenhove <url:http://www.timvw.be/>
 
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
Problem instantiating an array of objects or array of class instances raylopez99 Microsoft C# .NET 3 17th Sep 2007 07:16 PM
initialization of an array???? =?Utf-8?B?QmFyYg==?= Microsoft Excel Programming 3 13th Jul 2007 12:51 PM
Objects initialization =?Utf-8?B?Wm9ycm8yNg==?= Microsoft C# .NET 1 27th Jul 2005 05:06 AM
Problem with Object containing array of objects in Visual Basic .NET Jamie Noble Microsoft VB .NET 4 11th Mar 2004 05:15 PM
Problem with array initialization using Activator.CreateInstance Terry Microsoft C# .NET 4 16th Sep 2003 03:44 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:37 PM.