473,387 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Array of Objects Initialization problem C#.

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();
}

Jan 21 '07 #1
3 2617
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<myObjectmyList = 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**************@gmail.comwrote in message
news:11*********************@m58g2000cwm.googlegro ups.com...
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();
}

Jan 21 '07 #2
<sk**************@gmail.coma écrit dans le message de news:
11*********************@m58g2000cwm.googlegroups.c om...

| 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
Jan 21 '07 #3
sk**************@gmail.com 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<Tinstead (or
ArrayList).

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

Step1: RuleXMLParser [] RulesProperties;
List<RuleXMLParserRulesProperties;

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/>
Jan 21 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
15
by: Jakob Bieling | last post by:
Hi, I am aware of the fact, that the order of construction of global objects is unspecified. But I am in a situation, where I need to guarantee, that one object is created before others (not all...
20
by: K.M. Jr. | last post by:
Hi all, Consider this line - char s = "\0"; Does this initialize all array elements to zero ? Thanks.
15
by: Charles Sullivan | last post by:
Assume I have a static array of structures the elements of which could be any conceivable mixture of C types, pointers, arrays. And this array is uninitialized at program startup. If later in...
11
by: Geoff Cox | last post by:
Hello, I am trying to get a grip on where to place the initialization of two arrays in the code below which was created using Visual C++ 2005 Express Beta 2... private: static array<String^>^...
30
by: questions? | last post by:
say I have a structure which have an array inside. e.g. struct random_struct{ char name; int month; } if the array is not intialized by me, in a sense after I allocated a
10
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
3
by: Scotty | last post by:
I'm a C++ novice and need help figuring out some odd behavior I've encountered. Here's the situation: I create a class and have its constructor store a random number in a private "number"...
31
by: siddhu | last post by:
why can't we have array of references.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.