473,770 Members | 4,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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++)
{
CompareRuleNode s(RuleNodes[i],RuleNodes[j]); }
}
}

}
private void CompareRuleNode s(XmlNode RuleNodes1,XmlN ode RuleNodes2)
{

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

Jan 21 '07 #1
3 2638
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<myObjectmy List = new List();

then to add an object you do this

myList.Add(myOb ject);

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.comwro te in message
news:11******** *************@m 58g2000cwm.goog legroups.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++)
{
CompareRuleNode s(RuleNodes[i],RuleNodes[j]); }
}
}

}
private void CompareRuleNode s(XmlNode RuleNodes1,XmlN ode RuleNodes2)
{

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

Jan 21 '07 #2
<sk************ **@gmail.coma écrit dans le message de news:
11************* ********@m58g20 00...legro ups.com...

| 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<RuleXMLPar serRulesPropert ies;

Step2: RulesProperties = new RuleXMLParser[1000];
RulesProperties = new List<RuleXMLPar ser(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
7672
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 = (data_type**)malloc(widht*height*sizeof(data_type)+ height* sizeof(data_type*)); //allocate individual addresses for row pointers. Now that I am moving to C++,am looking for something by which I can
15
1846
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 others, just all objects of a specified type). I came up with the following solution, but I am not sure, if it does solve my problem: class first {
20
2323
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
4900
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 the program I wish to return this array to its startup state, can this be accomplished by writing binary zeroes to the entire memory block with memset(). E.g., static struct mystruct_st { int x1;
11
2270
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^>^ LHSquestions = gcnew array<String^> {"question 1","question 2"}; private: static array<String^>^ RHSquestions = gcnew array<String^> {"question 1",
30
3351
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
3030
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
3
1680
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" variable. The class also has a "getNumber()" function that returns its stored number. A function in the program creates five objects of this class and five pointers to these objects, the latter of which are stored in a global array. When "getNumber()"...
31
3230
by: siddhu | last post by:
why can't we have array of references.
0
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10008
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.