473,763 Members | 8,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.mytest record rec = new QCLoader.mytest record();
rec.disksetitem[0] = new QCLoader.diskse titem(); //CRASH
rec.disksetitem[0].model="foobar" ;
}
catch ( Exception e)
{
Console.WriteLi ne(e.Message);
}
Nov 15 '05 #1
2 4460

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.disksetite m[0] = new
QCLoader.diskse titem(); ", 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.diskse titem[5];
rec.disksetitem[0] = new QCLoader.diskse titem();

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: AcNl8IEYLeqHihH HRGWFLQuyji/2xQ==
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| Path: cpmsftngxa06.ph x.gbl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1772 82
| NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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.mytest record rec = new QCLoader.mytest record();
| rec.disksetitem[0] = new QCLoader.diskse titem(); //CRASH
| rec.disksetitem[0].model="foobar" ;
| }
| catch ( Exception e)
| {
| Console.WriteLi ne(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.mytest record rec = new QCLoader.mytest record();
//here you should add statement as following
//num is number of elements that the array has.

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

Nov 15 '05 #3

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

Similar topics

2
1380
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 form. This is the form <html> <head></head>
6
1329
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 possible that a persons loans one or more books. All right, let's say that we have a person ("Name: James; Age: 25") that loans 2 books ("Title: My first book; ISBN: 23213 & Title: My last book; ISBN: 89899").
48
3520
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# and the CLR?" Anders' reply included this comment: "The concept of an immutable object is very useful, but it's just up to the author to say that it's immutable."
5
15489
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
1245
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 SOAP header, and the other class is the main class containing all the web methods. Besides, the project contains two references pointing at two normal DLLs built also with VS .NET 2003. This references are needed because the web methods use them in
8
1863
by: Lüpher Cypher | last post by:
Hi, Suppose we have a hierarchical class structure that looks something like this: Object | +-- Main | +-- Object1
3
2637
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 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...
11
3162
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 = 0; $row < count($arr); $row++) {
4
7385
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
10148
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9938
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,...
0
9823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7368
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
5270
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
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
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.