By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,212 Members | 1,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,212 IT Pros & Developers. It's quick & easy.

Setting type of an object member in ArrayList

P: n/a
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???

}

}
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:#U**************@TK2MSFTNGP11.phx.gbl...
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???

}

}

Nov 15 '05 #2

P: n/a
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" <un****@businessco.us> wrote in message
news:uF**************@TK2MSFTNGP10.phx.gbl...
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" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:#U**************@TK2MSFTNGP11.phx.gbl...
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???

}

}


Nov 15 '05 #3

P: n/a
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #4

P: n/a

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" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
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" <un****@businessco.us> wrote in message
news:uF**************@TK2MSFTNGP10.phx.gbl...
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" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:#U**************@TK2MSFTNGP11.phx.gbl...
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???

}

}



Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.