473,246 Members | 1,257 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,246 software developers and data experts.

Strange problem withCollectionBase

Hello!

Assume I have this inheritance see below.
I know that a much better alternative is to use generics but I just want to
know how things works.

According to the documentation in CollectionBase for member List it return
an IList.
In the class Int16Collection I check by using method Test what this List in
CollectionBase is actually refering to and
it's refering to Int16Collection.
So according to that and the documenation we should have this kind of
situation.
IList List = refering to my instance of Int16Collection which is the same as
the keyword this.

So because this List is actually refering to an instance of Int16Collection
I should be able to use intellisense on this List which should display Foo
as a possible member to use but
as a matter of fact it doesn't if I cast to Int16Collection this Foo is
available.

But I can't understand this if List is actually refering to an instance to
Int16Collection
this Foo should be display when the intellisense is used on List.

public class Int16Collection : CollectionBase
{
public void Test()
{
Console.Writeln(List.GetType()); //It writes out Int16Collection
}

public void Foo()
{

}
}

I would be very glad is someone can explain this to me ?

//Tony

Jun 29 '08 #1
6 1056
Tony Johansson <jo*****************@telia.comwrote:
Assume I have this inheritance see below.
I know that a much better alternative is to use generics but I just want to
know how things works.

According to the documentation in CollectionBase for member List it return
an IList.
In the class Int16Collection I check by using method Test what this List in
CollectionBase is actually refering to and
it's refering to Int16Collection.
The List property of CollectionBase always returns "this" (i.e. the
same instance).
But I can't understand this if List is actually refering to an instance to
Int16Collection
this Foo should be display when the intellisense is used on List.
No, because the *compile-time* type of the List property is "IList".
Intellisense is based on the knowledge at compile-time.

Here's another example:

object o = "Hello"
int i = o.Length;

That won't compile, because even though o actually refers to a string
at execution time, the compiler only know that it's of type "object"
which doesn't have a Length property.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 29 '08 #2
Hello!!

If I have the construction below and I check the type for List by using the
CheckTypeOnList method
this methods WriteLine will display Int16Collection so here it must mean
that List is refering to class Int16Collection.
But according to you it should return *this* which is CollectionBase.
Have I missed something here when you wrote
The List property of CollectionBase always returns "this" (i.e. the
same instance).


public class Int16Collection : CollectionBase
{
public CheckTypeOnList()
{
Console.Wroteln(List.GetType());
}

}
//Tony

"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:MP*********************@msnews.microsoft.com. ..
Tony Johansson <jo*****************@telia.comwrote:
>Assume I have this inheritance see below.
I know that a much better alternative is to use generics but I just want
to
know how things works.

According to the documentation in CollectionBase for member List it
return
an IList.
In the class Int16Collection I check by using method Test what this List
in
CollectionBase is actually refering to and
it's refering to Int16Collection.

The List property of CollectionBase always returns "this" (i.e. the
same instance).
>But I can't understand this if List is actually refering to an instance
to
Int16Collection
this Foo should be display when the intellisense is used on List.

No, because the *compile-time* type of the List property is "IList".
Intellisense is based on the knowledge at compile-time.

Here's another example:

object o = "Hello"
int i = o.Length;

That won't compile, because even though o actually refers to a string
at execution time, the compiler only know that it's of type "object"
which doesn't have a Length property.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com

Jun 29 '08 #3
Tony Johansson <jo*****************@telia.comwrote:
If I have the construction below and I check the type for List by using the
CheckTypeOnList method
this methods WriteLine will display Int16Collection so here it must mean
that List is refering to class Int16Collection.
That's the execution-time type.
But according to you it should return *this* which is CollectionBase.
No, because you're calling it on Int16Collection. Change your code to

Console.WriteLine(this.GetType())

and you'll see Int16Collection as well.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 29 '08 #4
Hello!

Have I misunderstood this.
If I use List.GetType it must mean that what is List refering to at
execution type and here
I get Int16Collection as an answer.
Is that correct?

So in that case what do you mean with this "The List property of
CollectionBase always returns "this" (i.e. the
same instance)." ?

public class Int16Collection : CollectionBase
{
public CheckTypeOnList()
{
Console.Wroteln(List.GetType());
}
}
//Tony
"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:MP*********************@msnews.microsoft.com. ..
Tony Johansson <jo*****************@telia.comwrote:
>If I have the construction below and I check the type for List by using
the
CheckTypeOnList method
this methods WriteLine will display Int16Collection so here it must mean
that List is refering to class Int16Collection.

That's the execution-time type.
>But according to you it should return *this* which is CollectionBase.

No, because you're calling it on Int16Collection. Change your code to

Console.WriteLine(this.GetType())

and you'll see Int16Collection as well.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com

Jun 29 '08 #5
Tony Johansson <jo*****************@telia.comwrote:
Have I misunderstood this.
If I use List.GetType it must mean that what is List refering to at
execution type and here
I get Int16Collection as an answer.
Is that correct?
Yes.
So in that case what do you mean with this "The List property of
CollectionBase always returns "this" (i.e. the
same instance)." ?
I mean that the List property is implemented as:

protected IList List
{
get
{
return this;
}
}

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 29 '08 #6
Hello!

This may seem contradiction for me!

According to you is List in CollectionBase implemented something like the
below which you sent me. So the referenced type for List that will be
returned at execution time must be CollectionBase. The compile type for List
is IList
But when I looked at it by using method Test in Int16Collection(see below) I
get that the referenced type at execution type is Int16Collection.
So the referenced type from CollectionBase for List is not the same as the
referenced type for List when I use GetType in method Test.
What is it that I miss here ?

< protected IList List
{
get
{
return this;
}
}
public class Int16Collection : CollectionBase
{
public void Test()
{
Console.Writeln(List.GetType()); //It writes out Int16Collection
}
}

//Tony
"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:MP*********************@msnews.microsoft.com. ..
Tony Johansson <jo*****************@telia.comwrote:
Have I misunderstood this.
If I use List.GetType it must mean that what is List refering to at
execution type and here
I get Int16Collection as an answer.
Is that correct?

Yes.
So in that case what do you mean with this "The List property of
CollectionBase always returns "this" (i.e. the
same instance)." ?

I mean that the List property is implemented as:

protected IList List
{
get
{
return this;
}
}

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com

Jun 30 '08 #7

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

Similar topics

2
by: Arthur | last post by:
I've come across some strange xml, that I need to deal with, it looks like this:- <root> <foo attr="1">Some random strange text. <bar attr="2">blar</bar> <bar attr="3">blar blar</bar> <bar...
2
by: Paul Drummond | last post by:
Hi all, I am developing software for Linux Redhat9 and I have noticed some very strange behaviour when throwing exceptions within a shared library. All our exceptions are derived from...
2
by: Olaf | last post by:
I have a frameset page witch contains the myFuc() function. The function is accessed from a page in one of the frames in the frameset. An example is shown below. <input...
7
by: M O J O | last post by:
Hi, I'm developing a asp.net application and ran into a strange css problem. I want all my links to have a dashed underline and when they are hovered, it must change to a solid line. Sounds...
25
by: Neil Ginsberg | last post by:
I have a strange situation with my Access 2000 database. I have code in the database which has worked fine for years, and now all of a sudden doesn't work fine on one or two of my client's...
0
by: unknown | last post by:
Hi, I am developing an online book store with shopping cart. My shopping cart is represented as a Xml server control and I am using an XSLT to render it at the client side. I am using an...
0
by: Kris Vanherck | last post by:
yesterday i started getting this strange error when i try to run my asp.net project: Compiler Error Message: CS0006: Metadata file 'c:\winnt\microsoft.net\framework\v1.1.4322\temporary asp.net...
11
by: Martin Joergensen | last post by:
Hi, I've encountered a really, *really*, REALLY strange error :-) I have a for-loop and after 8 runs I get strange results...... I mean: A really strange result.... I'm calculating...
20
by: SpreadTooThin | last post by:
I have a list and I need to do a custom sort on it... for example: a = #Although not necessarily in order def cmp(i,j): #to be defined in this thread. a.sort(cmp) print a
14
by: blumen | last post by:
Hi all, I'm a newbie in VB.Net Programming.. Hope that some of you can help me to solve this.. I'm working out to read,parse and save textfile into SQL Server. The textfile contains thousands...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.