ArrayList Problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All
Check the following code
ClassOne{object objOne;}
ClassTwo{object objTwo;
ClassOne objClassOne;
Now i wud like the know the best way to assign memory to the objClassOne member
1. Assign it in the constructor: Pros: No error if u try to access it by mistake. Cons: Memory is assigned imm
2. Assign it outside like: ClassTwo objTwo; objTwo.objClassOne = new ClassOne(); Pros: Memory is assigned only when it is required. Cons: error if u try to access it by mistake
Please help. Thanx in advance
 
faktujaa,

I think that it is up to what you want the expected behavior to be, as
well what kind of design you want to have. If it is feasable that
objClassOne could be null, then when you want to use it, I would check to
see if it is null or not, and then use the class. Of course, if you want to
have some sort of dummy version there so that you don't have to perform null
checks, then I would assign it in the constructor. Depending on the layout
of ClassOne, the memory impact could be small, but it would still be a
memory impact nonetheless.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

faktujaa said:
Hi All,
Check the following code:
ClassOne{object objOne;}
ClassTwo{object objTwo;
ClassOne objClassOne;}
Now i wud like the know the best way to assign memory to the objClassOne member.
1. Assign it in the constructor: Pros: No error if u try to access it by
mistake. Cons: Memory is assigned imm.
2. Assign it outside like: ClassTwo objTwo; objTwo.objClassOne = new
ClassOne(); Pros: Memory is assigned only when it is required. Cons: error
if u try to access it by mistake.
 
Hi faktujaa,

Don't forget that you can use a property. That's what the properties are
for.
ClassTwo
{
object objTwo;
ClassOne objClassOne;

public ClassOne ObjClassOne
{
get
{
if(objClassOne == null) objClassOne = new ClassOne();
return objClassOne;
}
}
}

You nevet use the object itself you always use the property
Pros: You create the object when you need it.
Cons: Yeach time you get the object the property check if it is null. But I
don't think its a big problem.

--
HTH
B\rgds
100 [C# MVP]

faktujaa said:
Hi All,
Check the following code:
ClassOne{object objOne;}
ClassTwo{object objTwo;
ClassOne objClassOne;}
Now i wud like the know the best way to assign memory to the objClassOne member.
1. Assign it in the constructor: Pros: No error if u try to access it by
mistake. Cons: Memory is assigned imm.
2. Assign it outside like: ClassTwo objTwo; objTwo.objClassOne = new
ClassOne(); Pros: Memory is assigned only when it is required. Cons: error
if u try to access it by mistake.
 
Hi faktujaa,

If you need strongly typed collection go for your own. You can inherit from
CollectionBase this will save a lot of typing and reinventing the weel.
Using your own collection will make your constructor strictly typed and you
won't be able to pass wrong collection parameter. If you don't need that
safety you can simply go with ArrayList.
If you are going to keep value types in the collection you should go with
your own and internaly you should use arrays instead of ArrayList in order
not to have boxing going on all the time.

--
HTH
B\rgds
100 [C# MVP]

faktujaa said:
Hi,
Thanx for the reply. Yeah i feel this is the best way to do it bcoz its
better to check each time we assign memory to the object. Also i can assign
the memory when it is needed.
Now one more question:
If in the same example ClassTwo is not an object but a collection of
objects then is it better to implement your own custom list class having an
arraylist object/inheriting from IList interface or simply use arraylist in
the class to represent the collection of objects.
 
Back
Top