473,394 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Custom List casting

Hi all,
I have a custom made list (MyList) that inherits from CollectionBase. Is it possible to prohibit the casting of that list into other objects? I would like to get an error at design-time.

For example,

Dim myList As New MyList
Dim yourList As New IList = CType(MyList, IList) <---- prohibit this

Thanks
Goran Djuranovic
May 12 '06 #1
4 2053
No, that is not going to happen. Because MyList is an IList - that is perfectly legal. I don't see how you can make the compiler think it is not.

However, that exact line you typed in will be prohibited anyway, because you can't instantiate a variable with New, and yet assign it a value. Not to mention IList is an interface and thus has no constructor and is not something you can instantiate.
"Goran Djuranovic" <go**************@newsgroups.nospam> wrote in message news:O4**************@TK2MSFTNGP05.phx.gbl...
Hi all,
I have a custom made list (MyList) that inherits from CollectionBase. Is it possible to prohibit the casting of that list into other objects? I would like to get an error at design-time.

For example,

Dim myList As New MyList
Dim yourList As New IList = CType(MyList, IList) <---- prohibit this

Thanks
Goran Djuranovic
May 12 '06 #2
"Goran Djuranovic" <go**************@newsgroups.nospam> schrieb:
I have a custom made list (MyList) that inherits from CollectionBase. Is it
possible to prohibit the casting of that list into other objects? I would
like to get an error at design-time.

For example,

Dim myList As New MyList
Dim yourList As New IList = CType(MyList, IList) <---- prohibit this


No, you cannot avoid this. That's how subtyping and interface
implementation are intended to work and that's why interfaces are used :-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

May 12 '06 #3
Thanks both for you comments. The original problem was to create a custom type-safe list of some objects (f.e., Cars), and have it be able to accept only Car objects. But, it looks like that list could be cast(ed) into IList and then passed Object type, which would make it fail at run time. I know type-safe lists are implemented in VB 8, but thought the same coulb be done in VB 7.

Goran Djuranovic
"Goran Djuranovic" <go**************@newsgroups.nospam> wrote in message news:O4**************@TK2MSFTNGP05.phx.gbl...
Hi all,
I have a custom made list (MyList) that inherits from CollectionBase. Is it possible to prohibit the casting of that list into other objects? I would like to get an error at design-time.

For example,

Dim myList As New MyList
Dim yourList As New IList = CType(MyList, IList) <---- prohibit this

Thanks
Goran Djuranovic
May 15 '06 #4
Well, right after I posted my previous post, I ran into this:

"You can bypass the "Typed interface" of the collection very simply.. All you need to do is use the IList interface which allows .Add, .Insert etc for type object.." As I said in my previous post.

This can be prevented by Overriding OnInsert and OnSet. Both of these methods allow you the opertunity of raising an exception if the incorrect datatype
is passed in through the IList interface...

It just makes the whole colelction a little safer...

Eddie de bear
MCSD "

Will try it tomorrow.
GD

May 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
1
by: Weston Weems | last post by:
I've got a collection of objects say fruit. and fruit has a fairly decent heirachial structure not just flat props. I've got my repeater to bind to my fruit collection just fine, but what I want...
0
by: SMFX | last post by:
Okay, this should be a simple thing, but I can't seemto find the right documentation on it. I created a custom class that inherits from a well known class. For example: Class MyString Inherits...
7
by: John Grandy | last post by:
My ASP.NET Web Service project has a Web Method that returns an array filled with instances of a custom class. The custom class is defined in a Class Library that is included in the web-service...
2
by: a | last post by:
The problem: I want to allow an administrator (user) to create a list of teachers, each teacher in turn has a list of classes, each class has a list of students. There's an article at...
8
by: a | last post by:
I'm trying to save data from a custom object into the profile object, but it is not structured the way that I want. I'm trying to get the custom object to serialize as xml to a Profile object...
5
by: =?Utf-8?B?cGFnYXRlcw==?= | last post by:
Hello All, I am sure that I am just overlooking something, but here's something I can't quite get right... I want to be able to get the value of a parameter of an unknown custom attribute at...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...
0
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...

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.