473,624 Members | 2,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array Design

I have an application that has a state class, which basically stores
application information for other forms to access during the execution of
the application.

In state, I store an array which contains a number of positions. Depending
on what the user is doing in the application, this may hold 10 or 20 items.

The array length and items are set by another form in the application, ie.
not the state class.

The trouble is, I'm running into problems with null reference pointers. So
despite checking first off that the array is set and is not empty, i still
have exceptions cropping up.

Should I just wrap this in a try catch block and handle any exceptions, or
is there a better way of doing this?

Many thanks.
Nov 17 '05 #1
7 1422
Adam Suszeck <AS*********@ms n.com> wrote:
I have an application that has a state class, which basically stores
application information for other forms to access during the execution of
the application.

In state, I store an array which contains a number of positions. Depending
on what the user is doing in the application, this may hold 10 or 20 items.

The array length and items are set by another form in the application, ie.
not the state class.

The trouble is, I'm running into problems with null reference pointers. So
despite checking first off that the array is set and is not empty, i still
have exceptions cropping up.

Should I just wrap this in a try catch block and handle any exceptions, or
is there a better way of doing this?


Using a try/catch block is almost certainly the wrong way of going. You
need to understand exactly why you're getting the exceptions first -
and unfortunately, it's hard to help on that front without seeing some
code.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Thanks Jon.

I'll try and explain things a bit better.

The application is fairly complex and uses quite a lot of forms, so I'll
simplify things to focus on the issue i'm having.

When the application starts I initialise a state class, which contains
details about the user and other information. It also contains an array of
members

State Class:
public Member[] members;

Form1 Class:
I ask the user how many members to create. Ie. 10 or 20.
in the case of 10 i initialise the array

this.state.memb ers = new Member[10];

Form2 Class:
This class basically allows the user to view the members that are currently
in the application.
If the array is null, then there are no members and hence no members can be
displayed.

if the members array has been initialised and populated then the members
will be displayed.

The reason i store members in state, is because I need to use the array in
other forms, and it's an easier way of making the data available to other
forms that may require it.

I hope this explains what I'm trying to do.

Regards,
Adam

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Adam Suszeck <AS*********@ms n.com> wrote:
I have an application that has a state class, which basically stores
application information for other forms to access during the execution of
the application.

In state, I store an array which contains a number of positions.
Depending
on what the user is doing in the application, this may hold 10 or 20
items.

The array length and items are set by another form in the application,
ie.
not the state class.

The trouble is, I'm running into problems with null reference pointers.
So
despite checking first off that the array is set and is not empty, i
still
have exceptions cropping up.

Should I just wrap this in a try catch block and handle any exceptions,
or
is there a better way of doing this?


Using a try/catch block is almost certainly the wrong way of going. You
need to understand exactly why you're getting the exceptions first -
and unfortunately, it's hard to help on that front without seeing some
code.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3
Adam Suszeck <AS*********@ms n.com> wrote:
I'll try and explain things a bit better.

The application is fairly complex and uses quite a lot of forms, so I'll
simplify things to focus on the issue i'm having.

When the application starts I initialise a state class, which contains
details about the user and other information. It also contains an array of
members

State Class:
public Member[] members;

Form1 Class:
I ask the user how many members to create. Ie. 10 or 20.
in the case of 10 i initialise the array

this.state.memb ers = new Member[10];

Form2 Class:
This class basically allows the user to view the members that are currently
in the application.
If the array is null, then there are no members and hence no members can be
displayed.

if the members array has been initialised and populated then the members
will be displayed.

The reason i store members in state, is because I need to use the array in
other forms, and it's an easier way of making the data available to other
forms that may require it.

I hope this explains what I'm trying to do.


Yes, but it doesn't explain why you're getting an exception. You'll
have to provide some code for that.

Note that just creating the array doesn't populate it with members -
each element will be null to start with.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Hi,
Do as Jon said, post some code of where you are getting the exception. as
well as the exception Message and/or StackTrace to give a better idea of
where you have the problem

ITOH it's pretty sure you are referencing an element in the array that you
have not initializated.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Adam Suszeck" <AS*********@ms n.com> wrote in message
news:u0******** ******@TK2MSFTN GP09.phx.gbl...
Thanks Jon.

I'll try and explain things a bit better.

The application is fairly complex and uses quite a lot of forms, so I'll
simplify things to focus on the issue i'm having.

When the application starts I initialise a state class, which contains
details about the user and other information. It also contains an array
of members

State Class:
public Member[] members;

Form1 Class:
I ask the user how many members to create. Ie. 10 or 20.
in the case of 10 i initialise the array

this.state.memb ers = new Member[10];

Form2 Class:
This class basically allows the user to view the members that are
currently in the application.
If the array is null, then there are no members and hence no members can
be displayed.

if the members array has been initialised and populated then the members
will be displayed.

The reason i store members in state, is because I need to use the array in
other forms, and it's an easier way of making the data available to other
forms that may require it.

I hope this explains what I'm trying to do.

Regards,
Adam

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Adam Suszeck <AS*********@ms n.com> wrote:
I have an application that has a state class, which basically stores
application information for other forms to access during the execution
of
the application.

In state, I store an array which contains a number of positions.
Depending
on what the user is doing in the application, this may hold 10 or 20
items.

The array length and items are set by another form in the application,
ie.
not the state class.

The trouble is, I'm running into problems with null reference pointers.
So
despite checking first off that the array is set and is not empty, i
still
have exceptions cropping up.

Should I just wrap this in a try catch block and handle any exceptions,
or
is there a better way of doing this?


Using a try/catch block is almost certainly the wrong way of going. You
need to understand exactly why you're getting the exceptions first -
and unfortunately, it's hard to help on that front without seeing some
code.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 17 '05 #5
Yeah, the guys are right, when you do this:

this.state.memb ers = new Member[10];

the this.state.memb ers all ten elements are null. You have initialized
the members array, but not the elements inside it (something like
members[i] = new Member(); )

To avoid null exceptions when you access each member, just do this:

if (members[currentIndex] != null)
{
// this member is not null, let's use it
}

Anywyas, you migth still want to post your code so that we can see for
sure that that is the problem.
Nov 17 '05 #6
Yeah, the guys are right, when you do this:

this.state.memb ers = new Member[10];

the this.state.memb ers all ten elements are null. You have initialized
the members array, but not the elements inside it (something like
members[i] = new Member(); )

To avoid null exceptions when you access each member, just do this:

if (members[currentIndex] != null)
{
// this member is not null, let's use it
}

Anywyas, you migth still want to post your code so that we can see for
sure that that is the problem.
Nov 17 '05 #7
Thanks guys,

I've sorted this problem now.

-Adam

"laimis" <si*****@iit.ed u> wrote in message news:42******** ******@iit.edu. ..
Yeah, the guys are right, when you do this:

this.state.memb ers = new Member[10];

the this.state.memb ers all ten elements are null. You have initialized
the members array, but not the elements inside it (something like
members[i] = new Member(); )

To avoid null exceptions when you access each member, just do this:

if (members[currentIndex] != null)
{
// this member is not null, let's use it
}

Anywyas, you migth still want to post your code so that we can see for
sure that that is the problem.

Nov 17 '05 #8

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

Similar topics

4
3683
by: Christian Hackl | last post by:
I honestly wasn't able to find an answer for this design question using Google and Google Groups, so I apologize if it is asked too frequently :) Anyway: Let's say I have a multidimensional array of the following kind: $people = array(); // maps age and e-mail address to names $people = array(21, "paul@foo.bar"); $people = array(22, "linda@bar.foo"); $people = array(19, "max@foobar.foobar");
4
7283
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving from an interface and only implementing some of it means something is wrong: either the interface specification is wrong e.g. not minimal or the derivation is wrong e.g. the type can't actually honour this contract.
4
2544
by: christiang | last post by:
Hi guys I'd like to sort a multidimensional array that hasn't numerical index, in fact it is like: Array ( => Array ( => mobile => 1 )
12
14436
by: Maxwell2006 | last post by:
Hi, I declared an array like this: string scriptArgs = new string; Can I resize the array later in the code? Thank you, Max
8
12957
by: Jim | last post by:
In a C# project I'm working on for an iterative design application, I need to dispose of a large arrray of a struct object and reinitialize the array between iterations. That is, the user starts a design, and the program creates an array of objects defined as follows: public struct DES_TYPE { public string short_string;
18
4199
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and use my specific constructor to initialize the class. How to do it without using malloc? Something like Point* pt = new Point; I want it to reserve the space for N points only, and not to call default constructor. I dont have a default...
272
14003
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
7
1356
by: d d | last post by:
I have an array of objects that start out looking like this: var ra=; I want to be able to access the array by index number from some code, and by the name property from other code, as if it had been defined like this instead: var ra=; ra = {name:"fred",someproperty:"hello fred"};
5
3787
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
12
2480
by: raylopez99 | last post by:
I have an array that I wish to preserve as "read only". The array holds references to variables myObjects instantiated by new that change all the time. The variables are part of a class that I used ICloneable on, namely "Clone();" (deep and/or shallow copies worked the same for this particular class). Using ICloneable, I am able to successfully make a copy of the variables like so:
0
8249
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
8179
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
8685
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...
0
8633
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
8348
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
7176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2613
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
1493
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.