473,387 Members | 1,465 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.

probs adding objects to array ( simple )

Seems it should be simple. I had problems with a larger
class and the newsgroup suggested something I also tried.
So I pruned my class to see if I could find out what could
be wrong, Still dont know. This snippet is easily dropped
into any project, THANKS

namespace QCLoader
{
public class disksetitem
{
public string model;
}
public class mytestrecord
{
public disksetitem[] disksetitem;
}
}

/// test :
try
{
QCLoader.mytestrecord rec = new QCLoader.mytestrecord();
rec.disksetitem[0] = new QCLoader.disksetitem(); //CRASH
rec.disksetitem[0].model="foobar";
}
catch ( Exception e)
{
Console.WriteLine(e.Message);
}
Nov 15 '05 #1
2 4438

Hi andrew,

In your code, the mytestrecord's disksetitem member variable is a pointer
that points to
an array of disksetitem type items.
But till the executive of "rec.disksetitem[0] = new
QCLoader.disksetitem(); ", rec.disksetitem[0] has
not been created. rec.disksetitem only points to null.
So this sentence will generate an exception of {"Object reference not set
to an instance of an object." }
You should initialize the rec.disksetitem first, such as:
rec.disksetitem=new QCLoader.disksetitem[5];
rec.disksetitem[0] = new QCLoader.disksetitem();

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "andrewcw" <an************@boeing.com>
| Sender: "andrewcw" <an************@boeing.com>
| Subject: probs adding objects to array ( simple )
| Date: Mon, 18 Aug 2003 18:23:32 -0700
| Lines: 29
| Message-ID: <08****************************@phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcNl8IEYLeqHihHHRGWFLQuyji/2xQ==
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:177282
| NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Seems it should be simple. I had problems with a larger
| class and the newsgroup suggested something I also tried.
| So I pruned my class to see if I could find out what could
| be wrong, Still dont know. This snippet is easily dropped
| into any project, THANKS
|
| namespace QCLoader
| {
| public class disksetitem
| {
| public string model;
| }
| public class mytestrecord
| {
| public disksetitem[] disksetitem;
| }
| }
|
| /// test :
| try
| {
| QCLoader.mytestrecord rec = new QCLoader.mytestrecord();
| rec.disksetitem[0] = new QCLoader.disksetitem(); //CRASH
| rec.disksetitem[0].model="foobar";
| }
| catch ( Exception e)
| {
| Console.WriteLine(e.Message);
| }
|

Nov 15 '05 #2

"andrewcw" <an************@boeing.com> wrote in message
news:08****************************@phx.gbl...
Seems it should be simple. I had problems with a larger
class and the newsgroup suggested something I also tried.
So I pruned my class to see if I could find out what could
be wrong, Still dont know. This snippet is easily dropped
into any project, THANKS

namespace QCLoader
{
public class disksetitem
{
public string model;
}
public class mytestrecord
{
public disksetitem[] disksetitem;
}
}

/// test :
try
{
QCLoader.mytestrecord rec = new QCLoader.mytestrecord();
//here you should add statement as following
//num is number of elements that the array has.

disksetitem = new QCLoader.disksetitem[num];
rec.disksetitem[0] = new QCLoader.disksetitem(); //CRASH
rec.disksetitem[0].model="foobar";
}
catch ( Exception e)
{
Console.WriteLine(e.Message);
}

Nov 15 '05 #3

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

Similar topics

2
by: Dynamo | last post by:
Hi all Further to my earlier posting and the advice recieved I have now modified my code but it still doesn't work. I am trying to upload a file from my local PC to my remote server by using a...
6
by: Arjen | last post by:
Hello, Let's say that we have an array "persons" with person objects. We also have an array with "books" with book objects. We have from a single book only one copy. This means that it is...
48
by: Andrew Quine | last post by:
Hi Just read this article http://www.artima.com/intv/choices.html. Towards the end of the dicussions, when asked "Did you consider including support for the concept of immutable directly in C#...
5
by: Troy | last post by:
Hello, I have a dumb question. I have two array objects of type double with 12 elements in each object. I'd like to add the two objects but dont know how to. Any ideas? thanks
0
by: MIGUEL | last post by:
Hi all! Be patient because what I'm going to explain all of you it's more than very strange. I've developed a webservice project that contains two classes. One of them is going to act as a...
8
by: Lüpher Cypher | last post by:
Hi, Suppose we have a hierarchical class structure that looks something like this: Object | +-- Main | +-- Object1
3
by: sk.rasheedfarhan | last post by:
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...
11
by: dennis.sprengers | last post by:
Consider the following multi-dimensional array: --------------------------- $arr = array( array(3, 5, 7, 9), array(2, 4, 6, 8), array(1, 3, 5, 7) ); function add_arrays($arr) { for ($row =...
4
by: Peskov Dmitry | last post by:
class simple_class { int data; public: simple_class() {data=10;}; simple_class(int val) : data(val){} }; int main() {
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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...

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.