Problem with an indexer -- please help...

  • Thread starter Thread starter almurph
  • Start date Start date
A

almurph

Hi,


I have a class called "Tree" which contains several exposed
properties and an indexer. The indexer is defined to be as follows:


***** BEGIN CODE *****

private Tree[] mBranch;

public Tree this[int index]
{
get
{
return mBranch[index];
}
set
{
mBranch[index] = value;
}
}


**** END CODE ****


The system is giving me a wrning under mBranch that says:
"Tree.mBranch' is never assigned to, and will always have its default
value null"


When i try to send somethign down to the indexer, like this:

Node = ATree;

I get the error message "Object reference not set to an instance of
an object." as mBranch is indeed null.

Just wonderting is there any ways around this. I would appreciate any
suggestions/comments/ideas/calternatives/improvements/code-sampels
that you may be able to offer.

Thanks,
Al.
 
Hi,

When You write this
private Tree[] mBranch;
it means that the instances of the class will have an instance variable that
is a reference to an array of Tree objects, but does _not_ creates the array
itself. You have to do it Yourself (, for example in the constructor:
m_mBranch = new Tree[/*size of array*/];)

Hope You find this useful.
-Zsolt
 
Hi,

When You write this
    private Tree[] mBranch;
it means that the instances of the class will have an instance variable that
is a reference to an array of Tree objects, but does _not_ creates the array
itself. You have to do it Yourself (, for example in the constructor:
m_mBranch = new Tree[/*size of array*/];)

Hope You find this useful.
-Zsolt




I have a class called "Tree" which contains several exposed
properties and an indexer. The indexer is defined to be as follows:
***** BEGIN CODE *****
private Tree[] mBranch;
public Tree this[int index]
{
get
       {
       return mBranch[index];
       }
       set
       {
       mBranch[index] = value;
       }
}
**** END CODE ****
The system is giving me a wrning under mBranch that says:
"Tree.mBranch' is never assigned to, and will always have its default
value null"
When i try to  send somethign down to the indexer, like this:
Node = ATree;

I get the error message "Object reference not set to an instance of
an object." as mBranch is indeed null.
Just wonderting is there any ways around this. I would appreciate any
suggestions/comments/ideas/calternatives/improvements/code-sampels
that you may be able to offer.
Thanks,
Al.- Hide quoted text -

- Show quoted text -


Hi Zsolt,

Thanks - I guessed as much. Problem is though i'm unsure what size
the array should be. I need some container that I don;t need to
specify the size initially and I can insert objects at certain
positions. Something like a hashtable perhaps.

Problem is though when i implement a hastable my first object
contains the hashtable property and this hashtable contains another
one. Its really tricky to extract the "ith" hashtable if you know what
I mean....
 
Thanks - I guessed as much. Problem is though i'm unsure what size
the array should be. I need some container that I don;t need to
specify the size initially and I can insert objects at certain
positions. Something like a hashtable perhaps.

Assuming you're using Framework 2.0 or above:

private List<Tree> mBranch = new List<Tree>();

(And then go read up on "generics"....)
 
Hi,

Maybe a dictionary could be useful to You. ( Dictionary<int, Tree> , see
Dictionary<TKey,TValue> class :
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx )

-Zsolt


Hi,

When You write this
private Tree[] mBranch;
it means that the instances of the class will have an instance variable
that
is a reference to an array of Tree objects, but does _not_ creates the
array
itself. You have to do it Yourself (, for example in the constructor:
m_mBranch = new Tree[/*size of array*/];)

Hope You find this useful.
-Zsolt




I have a class called "Tree" which contains several exposed
properties and an indexer. The indexer is defined to be as follows:
***** BEGIN CODE *****
private Tree[] mBranch;
public Tree this[int index]
{
get
{
return mBranch[index];
}
set
{
mBranch[index] = value;
}
}
**** END CODE ****
The system is giving me a wrning under mBranch that says:
"Tree.mBranch' is never assigned to, and will always have its default
value null"
When i try to send somethign down to the indexer, like this:
Node = ATree;

I get the error message "Object reference not set to an instance of
an object." as mBranch is indeed null.
Just wonderting is there any ways around this. I would appreciate any
suggestions/comments/ideas/calternatives/improvements/code-sampels
that you may be able to offer.
Thanks,
Al.- Hide quoted text -

- Show quoted text -


Hi Zsolt,

Thanks - I guessed as much. Problem is though i'm unsure what size
the array should be. I need some container that I don;t need to
specify the size initially and I can insert objects at certain
positions. Something like a hashtable perhaps.

Problem is though when i implement a hastable my first object
contains the hashtable property and this hashtable contains another
one. Its really tricky to extract the "ith" hashtable if you know what
I mean....
 
    Thanks - I guessed as much. Problem is though i'm unsure what size
the array should be. I need some container that I don;t need to
specify the size initially and I can insert objects at certain
positions. Something like a hashtable perhaps.

You should only use a hashtable (or rather Dictionary<K,V>) if you
actually have key-value pairs. If you want an ordered indexed list of
items with insertions only at the end, use List<T>. If you also need
to do insertions in the beginning or in the middle, and don't do
indexing by position often (i.e. you usually iterate all elements from
beginning to end) said:
   Problem is though when i implement a hastable my first object
contains the hashtable property and this hashtable contains another
one. Its really tricky to extract the "ith" hashtable if you know what
I mean....

What's tricky about it?
 
Back
Top