473,394 Members | 1,843 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,394 software developers and data experts.

Strange indexer behaviour

Hello
I have two classes:

public class ScheduleItem
{
private string s_name;

public ScheduleItem(){ s_name="default";}
public string Name{
get{return s_name;}
set{s_name=value;}
}
}

and the second class:

public class Schedules
{
ScheduleItem[] sched;
private int i_lenght;

public Schedules (int howManyItems)
{
sched= new ScheduleItem[howManyItems];
i_lenght = howManyItems;
}
public int Lenght{ get... set...}

public ScheduleItem this[int idx]
{
get... set.. (here I wrote indexer, it works)
}

}
When I put an instance in my application:
Schedule SCH = new Schedule(2);
SCH[0].Name = "Bill Gates";

i got exception.... why? it should work in my opinion....
Nov 16 '05 #1
3 915
DAMAR <DA***@discussions.microsoft.com> wrote:

<snip>
When I put an instance in my application:
Schedule SCH = new Schedule(2);
SCH[0].Name = "Bill Gates";

i got exception.... why? it should work in my opinion....


This has nothing to do with indexers - it's just normal arrays. When
you create an array, it doesn't populate it with objects. In other
words, your SCH[0] is returning null, which you're then dereferencing,
hence the exception.

You need to actually create an instance of ScheduleItem at some
stage...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Ok, but I thought I had created new instances of ScheduleItem in Schedules'
constructor:
public Schedules (int howManyItems)
{
sched= new ScheduleItem[howManyItems];
i_lenght = howManyItems;
}
What else I misunderstood? SO what it the right way to do this?
Thanks

"Jon Skeet [C# MVP]" wrote:
DAMAR <DA***@discussions.microsoft.com> wrote:

<snip>
When I put an instance in my application:
Schedule SCH = new Schedule(2);
SCH[0].Name = "Bill Gates";

i got exception.... why? it should work in my opinion....


This has nothing to do with indexers - it's just normal arrays. When
you create an array, it doesn't populate it with objects. In other
words, your SCH[0] is returning null, which you're then dereferencing,
hence the exception.

You need to actually create an instance of ScheduleItem at some
stage...

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

Nov 16 '05 #3
Ok, I must be blind....

Schedules schedules = new Schedules();
....
for(int i=0;i<schedules .Lenght;i++) schedules[i] = new ScheduleItem();
It works
Thanks, Jon

"DAMAR" wrote:
Ok, but I thought I had created new instances of ScheduleItem in Schedules'
constructor:
public Schedules (int howManyItems)
{
sched= new ScheduleItem[howManyItems];
i_lenght = howManyItems;
}
What else I misunderstood? SO what it the right way to do this?
Thanks

"Jon Skeet [C# MVP]" wrote:
DAMAR <DA***@discussions.microsoft.com> wrote:

<snip>
When I put an instance in my application:
Schedule SCH = new Schedule(2);
SCH[0].Name = "Bill Gates";

i got exception.... why? it should work in my opinion....


This has nothing to do with indexers - it's just normal arrays. When
you create an array, it doesn't populate it with objects. In other
words, your SCH[0] is returning null, which you're then dereferencing,
hence the exception.

You need to actually create an instance of ScheduleItem at some
stage...

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

Nov 16 '05 #4

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

Similar topics

14
by: Nikhil Patel | last post by:
Hi all, Is it possible to have more than one indexer in a class. If not, are there any good alternative ways to achieve similar functionality. Thanks... -Nikhil
1
by: Iulian Ionescu | last post by:
I have the following problem. I derived a class from ArrayList to provide some additional events I need. Then I derived 2 different classes from this one and each of them defines an indexer that...
1
by: I am dog | last post by:
Hi , I use VS 2005 beta1 to do some thing as follows. declare a Dictionary<ulong,DelegateTyep> dictionary = new ... and use dictionary object such as dictionary += aDelegateTyepObject;
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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...

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.