NOTE: This is a COMMENT...not a QUESTION:
For the newbies, I've been messing around with strongly typed ArrayLists so
I don't have to type so many "DirectCast ". The following works great where
"myClass" is the classs that each ArrayList Element will contain:
dim myarraylist as typedArrayList = new typedArrayList
Public Class typedArrayList
Inherits ArrayList
Default Public Shadows Property Item(ByVal Index As Integer) As myClass
Get
Return DirectCast(MyBa se.Item(Index), myClass)
End Get
Set(ByVal Value As myclass)
MyBase.Item(Ind ex) = Value
End Set
End Property
End Class
You can then reference any property in myclass in your code instead of using
direct cast to cast the arraylist element to myClass:
myPropertyValue = myarraylist(0). myProperty
--
Dennis in Houston 5 1591
Dennis,
Of course the danger of doing this is: dim myarraylist as typedArrayList = new typedArrayList
Dim al As ArrayList = typedArrayList
al.Item is no longer typed! Which is a major danger of using Shadows! Two
major places where Shadows are useful are Version Control & overriding
Attributes on non Overridable methods. (Post if you would like more
information on these uses of Shadows).
I would recommend starting with CollectionBase to create a strongly typed
ArrayList. To absolutely ensure a strongly typed CollectionBase derived
class you may need to override CollectionBase. OnValidate & the other On*
methods of CollectionBase.
NOTE: This is a comment on your comment ;-)
Hope this helps
Jay
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:8F******** *************** ***********@mic rosoft.com... NOTE: This is a COMMENT...not a QUESTION:
For the newbies, I've been messing around with strongly typed ArrayLists so I don't have to type so many "DirectCast ". The following works great where "myClass" is the classs that each ArrayList Element will contain:
dim myarraylist as typedArrayList = new typedArrayList Public Class typedArrayList Inherits ArrayList Default Public Shadows Property Item(ByVal Index As Integer) As myClass Get Return DirectCast(MyBa se.Item(Index), myClass) End Get Set(ByVal Value As myclass) MyBase.Item(Ind ex) = Value End Set End Property End Class
You can then reference any property in myclass in your code instead of using direct cast to cast the arraylist element to myClass:
myPropertyValue = myarraylist(0). myProperty
-- Dennis in Houston
Understand your comments but I'm using this in my own program and I can avoid
the "dim al as arraylist = TypedArrayList. My method is quick, dirty and
works well for my internal programming.
"Jay B. Harlow [MVP - Outlook]" wrote: Dennis, Of course the danger of doing this is:
dim myarraylist as typedArrayList = new typedArrayList
Dim al As ArrayList = typedArrayList
al.Item is no longer typed! Which is a major danger of using Shadows! Two major places where Shadows are useful are Version Control & overriding Attributes on non Overridable methods. (Post if you would like more information on these uses of Shadows).
I would recommend starting with CollectionBase to create a strongly typed ArrayList. To absolutely ensure a strongly typed CollectionBase derived class you may need to override CollectionBase. OnValidate & the other On* methods of CollectionBase.
NOTE: This is a comment on your comment ;-)
Hope this helps Jay
"Dennis" <De****@discuss ions.microsoft. com> wrote in message news:8F******** *************** ***********@mic rosoft.com... NOTE: This is a COMMENT...not a QUESTION:
For the newbies, I've been messing around with strongly typed ArrayLists so I don't have to type so many "DirectCast ". The following works great where "myClass" is the classs that each ArrayList Element will contain:
dim myarraylist as typedArrayList = new typedArrayList Public Class typedArrayList Inherits ArrayList Default Public Shadows Property Item(ByVal Index As Integer) As myClass Get Return DirectCast(MyBa se.Item(Index), myClass) End Get Set(ByVal Value As myclass) MyBase.Item(Ind ex) = Value End Set End Property End Class
You can then reference any property in myclass in your code instead of using direct cast to cast the arraylist element to myClass:
myPropertyValue = myarraylist(0). myProperty
-- Dennis in Houston
Dennis, My method is quick, dirty and
Is my point ;-)
works well for my internal programming.
As long as your programming is truly "internal" to you only. As soon as
someone inherits your "internal" code (such as a reader of this thread) and
uses the statement I showed....
Also I hope you understand why it can be dangerous. I would consider adding
a TODO on usages of Shadows such as yours, so anyone inheriting the code
realizes the risk...
Just a thought
Jay
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:09******** *************** ***********@mic rosoft.com... Understand your comments but I'm using this in my own program and I can avoid the "dim al as arraylist = TypedArrayList. My method is quick, dirty and works well for my internal programming.
"Jay B. Harlow [MVP - Outlook]" wrote:
Dennis, Of course the danger of doing this is:
> dim myarraylist as typedArrayList = new typedArrayList
Dim al As ArrayList = typedArrayList
al.Item is no longer typed! Which is a major danger of using Shadows! Two major places where Shadows are useful are Version Control & overriding Attributes on non Overridable methods. (Post if you would like more information on these uses of Shadows).
I would recommend starting with CollectionBase to create a strongly typed ArrayList. To absolutely ensure a strongly typed CollectionBase derived class you may need to override CollectionBase. OnValidate & the other On* methods of CollectionBase.
NOTE: This is a comment on your comment ;-)
Hope this helps Jay
"Dennis" <De****@discuss ions.microsoft. com> wrote in message news:8F******** *************** ***********@mic rosoft.com... > NOTE: This is a COMMENT...not a QUESTION: > > For the newbies, I've been messing around with strongly typed > ArrayLists > so > I don't have to type so many "DirectCast ". The following works great > where > "myClass" is the classs that each ArrayList Element will contain: > > dim myarraylist as typedArrayList = new typedArrayList > Public Class typedArrayList > Inherits ArrayList > Default Public Shadows Property Item(ByVal Index As Integer) As > myClass > Get > Return DirectCast(MyBa se.Item(Index), myClass) > End Get > Set(ByVal Value As myclass) > MyBase.Item(Ind ex) = Value > End Set > End Property > End Class > > You can then reference any property in myclass in your code instead of > using > direct cast to cast the arraylist element to myClass: > > myPropertyValue = myarraylist(0). myProperty > > -- > Dennis in Houston
I'm with Jay on this as well. Even though your code is "internal" and only
being used by you, wouldn't you much rather grow as a developer and learn a
far better way to do what you are trying to do? After all, your code is
suffering the hit (all be it negligeable) of doing a Direct Cast every
single time you extract a value from the list, it's just hidden away out of
sight.
--
Pete Wright
Author of ADO.NET Novice to Pro for Apress www.petewright.org
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:uH******** ******@TK2MSFTN GP10.phx.gbl... Dennis, My method is quick, dirty and Is my point ;-)
works well for my internal programming. As long as your programming is truly "internal" to you only. As soon as someone inherits your "internal" code (such as a reader of this thread) and uses the statement I showed....
Also I hope you understand why it can be dangerous. I would consider adding a TODO on usages of Shadows such as yours, so anyone inheriting the code realizes the risk...
Just a thought Jay
"Dennis" <De****@discuss ions.microsoft. com> wrote in message news:09******** *************** ***********@mic rosoft.com... Understand your comments but I'm using this in my own program and I can avoid the "dim al as arraylist = TypedArrayList. My method is quick, dirty and works well for my internal programming.
"Jay B. Harlow [MVP - Outlook]" wrote:
Dennis, Of course the danger of doing this is:
> dim myarraylist as typedArrayList = new typedArrayList
Dim al As ArrayList = typedArrayList
al.Item is no longer typed! Which is a major danger of using Shadows! Two major places where Shadows are useful are Version Control & overriding Attributes on non Overridable methods. (Post if you would like more information on these uses of Shadows).
I would recommend starting with CollectionBase to create a strongly typed ArrayList. To absolutely ensure a strongly typed CollectionBase derived class you may need to override CollectionBase. OnValidate & the other On* methods of CollectionBase.
NOTE: This is a comment on your comment ;-)
Hope this helps Jay
"Dennis" <De****@discuss ions.microsoft. com> wrote in message news:8F******** *************** ***********@mic rosoft.com... > NOTE: This is a COMMENT...not a QUESTION: > > For the newbies, I've been messing around with strongly typed > ArrayLists > so > I don't have to type so many "DirectCast ". The following works great > where > "myClass" is the classs that each ArrayList Element will contain: > > dim myarraylist as typedArrayList = new typedArrayList > Public Class typedArrayList > Inherits ArrayList > Default Public Shadows Property Item(ByVal Index As Integer) As > myClass > Get > Return DirectCast(MyBa se.Item(Index), myClass) > End Get > Set(ByVal Value As myclass) > MyBase.Item(Ind ex) = Value > End Set > End Property > End Class > > You can then reference any property in myclass in your code instead of > using > direct cast to cast the arraylist element to myClass: > > myPropertyValue = myarraylist(0). myProperty > > -- > Dennis in Houston
Dennis,
In my opinion is the biggest problem that you want cout a cout use a list
where a collection is the properiate way to go.
What you now in the same opinion are trying to do is making from a list
class a collection class, where there are so many built in classes for that.
Just my thought,
Cor This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Bill Cohagan |
last post by:
I'm trying to figure out the "best" way to implement a strongly typed
ArrayList. Using inheritance is one approach. It has the advantage that I
only have to write overrides for the Add method and indexer. There are
problems with this however. First the indexer "override" has a different
return type. This doesn't cause a compile time error,...
|
by: Job Lot |
last post by:
I am confused how strongly typed dataset is different from un-typed dataset.
Is there any good link explaining pros and cons of both? Which one should be
used preferably?
|
by: Mark |
last post by:
Just wanted to confirm that my understanding of a strongly typed language is
correct:
1. .NET is a strongly typed language because each variable / field must be
declared a specific type (String, Int, Float, etc...)
2. VB 6 and VBScript were not strongly typed because both allowed you to ...
dim blah
.... which creates a variant.
|
by: David |
last post by:
I have been developing applications with Java for quite a while but I am new
to .NET development. I am trying to learn the ".NET way" of creating Strongly
Typed Objects from a database. The Enterprise Library seems pretty close to
what I am used to in Java but I am not sure how to create strongly-typed
DataSets. I see that I can use the...
|
by: HardBap |
last post by:
I've created a strongly typed DataSet (Customers.xsd) using the xsd.exe
tool. I want to be able to access fields using
ds.Customer.CompanyName.
The problem is when I return this DataSet from a Web Service it adds
another Table to the DataSet that contains the data. I have to access
the data using ds.Tables.Rows.ToString(). This
defeats the...
| |
by: Ian Gore |
last post by:
Hi,
I'm relatively new to VB.NET so I'd be grateful if someone could point out what I don't understand here...
1) Creating a strongly typed collection by inheriting CollectionBase.
This is covered in most of the books I've read. Basically you inherit CollectionBase and then add methods (say Add) that takes your types as a parameter and...
|
by: Dennis |
last post by:
I use the following code for a strongly typed arraylist and it works great.
However, I was wondering if this is the proper way to do it. I realize that
if I want to implement sorting of the arraylist then I have to handle this
with a sort method that uses comparer. I can reference the properties of the
Arraylist directly such as
dim...
|
by: Code Monkey |
last post by:
Silly question maybe, but I've been doing the following for far too
long now:
public static DataTable myDataTable()
{
string sql = @"SELECT column1, column2, column3, column4
FROM myTable";
using (SqlConnection conn = new SqlConnection(websqlconn))
{
SqlCommand cmd = new SqlCommand(sql, conn);
|
by: Rachana |
last post by:
Hi,
I have understood Data Sets but what is meant by typed/untyped/
strongly typed datasets. Can any one explain me or suggest any site/
article, to get these concepts (and their comparisions) cleared?
Thanks,
Rachana
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |