Setting type of an object member in ArrayList

  • Thread starter Thread starter Tamir Khason
  • Start date Start date
T

Tamir Khason

How can I set the type of the object added to ArrayList (type of Array List
Members)

Here is the code:

protected ArrayList tabs = new ArrayList();

public ArrayList Tabs

{

get

{

return tabs;

}

set

{

tabs.Add(value); //The default type is object - how to change it to my
type???

}

}
 
As long as you can put an object into array list item what do you mean by
"set the type". You can put ANY kind of object into array list like
tabs.Add(stringvalue); tabs.Add(myObject) or anything you want. If you want
to know when you get the data what kind of object do you have in ArrayList
you can use Object.GetType (i.e. tabs[index].GetType()) and it will return
you the type of the object stored in ArrayList item.

Also you can hold the type of the object (if you really need it) into the
ArrayList item by defininig a structure or a class like:

public struct ItemStruct
{
public System.Type typeOfObject;
public Object objValue;
}
and insert this structure in the ArrayList

ItemStruct mystruct = new ItemStruct();
mystruct.typeOfObject = myObject.GetType();
mystruct.objValue = myObject;
tabs.Add(mystruct);
 
The problem is other: (maybe I had to explain first)

1) I build user control which on of it's properties should be (Collection)
in my case I prefer ArrayList
2) In DesignTime from inside the form I want to be able to add to this
collection items of my type.
3) While in design mode I click on the property (collection) it pops up
dialog window where I'm able to add items to the array, but when I add it in
the right side of this dialog instead of properties of the item ?I see just
Object with Value of System.Object and graied out.

Question: How should I "explain" to VS properties of m items inside the
collection???

Thank you


Horatiu Ripa said:
As long as you can put an object into array list item what do you mean by
"set the type". You can put ANY kind of object into array list like
tabs.Add(stringvalue); tabs.Add(myObject) or anything you want. If you want
to know when you get the data what kind of object do you have in ArrayList
you can use Object.GetType (i.e. tabs[index].GetType()) and it will return
you the type of the object stored in ArrayList item.

Also you can hold the type of the object (if you really need it) into the
ArrayList item by defininig a structure or a class like:

public struct ItemStruct
{
public System.Type typeOfObject;
public Object objValue;
}
and insert this structure in the ArrayList

ItemStruct mystruct = new ItemStruct();
mystruct.typeOfObject = myObject.GetType();
mystruct.objValue = myObject;
tabs.Add(mystruct);

--
Horatiu Ripa
Tamir Khason said:
How can I set the type of the object added to ArrayList (type of Array List
Members)

Here is the code:

protected ArrayList tabs = new ArrayList();

public ArrayList Tabs

{

get

{

return tabs;

}

set

{

tabs.Add(value); //The default type is object - how to change it to my
type???

}

}
 
1) I build user control which on of it's properties should be (Collection)
in my case I prefer ArrayList

It would be better if you wrote your own collection class instead. You
can use an ArrayList internally if you prefer that.



Mattias
 
Hmmm. For that your "get" should be strong typed instead of returning an
object. I don't think it is possible, at least not in this manner
--
Horatiu Ripa

Tamir Khason said:
The problem is other: (maybe I had to explain first)

1) I build user control which on of it's properties should be (Collection)
in my case I prefer ArrayList
2) In DesignTime from inside the form I want to be able to add to this
collection items of my type.
3) While in design mode I click on the property (collection) it pops up
dialog window where I'm able to add items to the array, but when I add it in
the right side of this dialog instead of properties of the item ?I see just
Object with Value of System.Object and graied out.

Question: How should I "explain" to VS properties of m items inside the
collection???

Thank you


Horatiu Ripa said:
As long as you can put an object into array list item what do you mean by
"set the type". You can put ANY kind of object into array list like
tabs.Add(stringvalue); tabs.Add(myObject) or anything you want. If you want
to know when you get the data what kind of object do you have in ArrayList
you can use Object.GetType (i.e. tabs[index].GetType()) and it will return
you the type of the object stored in ArrayList item.

Also you can hold the type of the object (if you really need it) into the
ArrayList item by defininig a structure or a class like:

public struct ItemStruct
{
public System.Type typeOfObject;
public Object objValue;
}
and insert this structure in the ArrayList

ItemStruct mystruct = new ItemStruct();
mystruct.typeOfObject = myObject.GetType();
mystruct.objValue = myObject;
tabs.Add(mystruct);

--
Horatiu Ripa
Tamir Khason said:
How can I set the type of the object added to ArrayList (type of Array List
Members)

Here is the code:

protected ArrayList tabs = new ArrayList();

public ArrayList Tabs

{

get

{

return tabs;

}

set

{

tabs.Add(value); //The default type is object - how to change it to my
type???

}

}
 
Back
Top