473,507 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting type of an object member in ArrayList

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
4 7070
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2472
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
5
1710
by: He Shiming | last post by:
Hi, I have a question regarding memory consumption of class/object member functions. Say that I have: class A { char szAbc; char* getString(); }
3
3613
by: Marco | last post by:
Howdy! Given that: Type type = Type.GetType("System.Windows.Forms.Button"); Question: How do I obtain a Button object by only knowing the type object. In other words, how do I fire the...
7
1804
by: WildHare | last post by:
If I have a class and I add it to an ArrayList and then want to access that class using using the index operator (e.g. ArrayList) the ArrayList returns a type "Object". I can cast the return to...
8
14847
by: Bassem | last post by:
Hi to all... Thanks for the help... I want to get the value of the column of dataset into array, but I get this bug. "Cannot apply indexing with to an expression of type 'object'" when biuld....
4
1904
by: Dixon | last post by:
wats the Diff Between Setting an object to NULL and calling the Dispose() method for that object?
13
25827
by: Fredrik Strandberg | last post by:
Hi! I receive an object as a System.Object argument to a method. I need to check if the object is a Type object or not (that is, not a specific type, but if the object is a type object in...
5
1525
by: garyusenet | last post by:
Console.WriteLine("Current OS: {0} ", Environment.OSVersion) I'm trying to find out what .OSVersion is. It returns an object which details the OS. I know it's not a method, because it doesnt...
5
3155
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a...
6
1964
by: Deckarep | last post by:
I want to be able to pass in a function a string say: "TextBox" Then I need a way to convert that string representation into a Type object so i can search through some controls and check their...
0
7223
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
7111
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
7319
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,...
1
7031
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
7485
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...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.