11 Replies - 907 Views - Last Post: 16 May 2011 - 07:23 AM Rate Topic: -----

#1 scolty  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Generics "as" operator

Posted 15 May 2011 - 08:45 AM

evening, im currently readin a book which states the following code when using the as operator for interfaces:

ArrayList val = new ArrayList();
IComparable val1 = val as IComparable;
if (val1 == null)
{
    Console.WriteLine("val doesnt support IComparable");
}




Am i correct in assuming that "val1" would have to be declared above as a type that supports the IComparable interface already??

Thanks for your time

This post has been edited by scolty: 15 May 2011 - 09:08 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Generics "as" operator

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Generics "as" operator

Posted 15 May 2011 - 08:54 AM

According to MSDN, you can only convert reference type expressions.

And the reference type expressions are:
http://msdn.microsof...=VS.100%29.aspx

Within those are interfaces. So you seem to be on the clear. :)

Also check out this thread:
http://www.hanselman...nInterface.aspx

This post has been edited by Sergio Tapia: 15 May 2011 - 09:00 AM

Was This Post Helpful? 0
  • +
  • -

#3 scolty  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Re: Generics "as" operator

Posted 15 May 2011 - 09:17 AM

it says in the book ArrayList doesnt support IComparable, but assuming id screwed up and thought it did, would i have to declare val1 above as a type that does support IComparable?

ie int val1[];

as he later states that if i modify the code to:

int val = 5;

then the conversion works.

hope this makes sense?
thanks fro taking the time to reply

This post has been edited by scolty: 15 May 2011 - 09:18 AM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Generics "as" operator

Posted 15 May 2011 - 09:46 AM

What book are you reading that is even bothering with ArrayLists?
Was This Post Helpful? 2
  • +
  • -

#5 scolty  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Re: Generics "as" operator

Posted 15 May 2011 - 10:44 AM

beginning c# 3.0 an introduction to object orientated programming

so, am i correct above or have i missed the point the author was getting at?
Was This Post Helpful? 0
  • +
  • -

#6 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Generics "as" operator

Posted 15 May 2011 - 01:01 PM

Hi,

An ArrayList doesn't implement IComparable (either directly, or indirectly), so that cast will fail.

The Int32 structure (for which 'int' is an alias) does implement IComparable, and so the cast will succeed.


The object your trying to cast (operand before the 'as') doesn't have to implement IComparable directly however; it can inherit from another class (or implement another interface), which themselves implement IComparable, and the cast would still work.


Basically, as long as IComparable is somewhere in the inheritance chain above the object your trying to cast (either the object implements it directly, or one of the object's ancesters implements it), the cast should always work :)

This post has been edited by CodingSup3rnatur@l-360: 15 May 2011 - 02:46 PM

Was This Post Helpful? 0
  • +
  • -

#7 scolty  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Re: Generics "as" operator

Posted 15 May 2011 - 01:07 PM

Last question.

IComparable val1 = val as IComparable;



isnt a declaration for val1 right? ie i still would need to state the below somewhere first.

private int[] val1;
// edited due to typo



Thanks again for your time, i really appreciate it!

This post has been edited by scolty: 15 May 2011 - 01:29 PM

Was This Post Helpful? 0
  • +
  • -

#8 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Generics "as" operator

Posted 15 May 2011 - 01:19 PM

It is the definition and declaration of 'val1'. 'val1' is just a variable that you have declared. In this line:

IComparable val1 = val as IComparable; //declaration AND definition of 'val1'




you are declaring a variable called 'val1', and giving it a type of IComparable. You are then assigning to that variable the result of the cast (defining its value).


I'm not sure where the int array comes into it?


I think you are carrying some sort of misunderstanding there actually :) Can you explain why you think the array variable is needed?


Plus, your array declaration syntax is slightly off, it should be like this:

private int[] val1;

This post has been edited by CodingSup3rnatur@l-360: 15 May 2011 - 02:48 PM

Was This Post Helpful? 0
  • +
  • -

#9 scolty  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Re: Generics "as" operator

Posted 15 May 2011 - 01:27 PM

aye, i am a little confused. Basically i thought i needed to declare a variable normally first before i messed with interfaces, ie i didnt see IComparable as a data type which i could declare a variable with (n im not sure if even thats correct lol) I guess i need to go back and re-read that section as i obviously havent understood it.

Thanks for your help though

This post has been edited by scolty: 15 May 2011 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Generics "as" operator

Posted 15 May 2011 - 01:37 PM

With the introduction of REAL generics in C# 2.0, ArrayList should be mentioned as nothing more than a footnote of "here's how we used to have to do this when we wore onions on our belts".
Was This Post Helpful? 0
  • +
  • -

#11 scolty  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Re: Generics "as" operator

Posted 15 May 2011 - 01:45 PM

lol aye but thats nae really solving my misunderstanding atm. Im sure there was a reason he had it in there + the book was published in 08 i believe so its a little old but the best introductory text ive read... bar my misunderstanding of interfaces.
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,980
  • Joined: 08-June 10

Re: Generics "as" operator

Posted 16 May 2011 - 07:23 AM

You can declare variables of interface types. You can't directly instantiate them with a new statement.

IComparable var1; //this works
IComparable var1 = new IComarable(); //this doesn't work


That's a major point of interfaces. To be able to refer to different kinds of objects that share a same behavior with one type. Many, many types of objects implement IComparable (because lots of objects need to be able to be compared). So, if we have a method or block of code that can deal with any type of object that is comparable, we can use IComparables instead of trying to write one for ints, one for doubles, one for strings, one for Cars, etc...

Anyway, since interfaces by themselves have no implementations, that's why you can't create a new instance of one.

If you'd like to conceptually learn more about interfaces, me and another member explain them in great detail here:

http://www.dreaminco...ing-interfaces/

And Jack is correct, ArrayList hasn't been useful since .NET 1.1. So I understand this is a learning exercise, but don't ever bother to use it in a real project. List<T> is a much, much better construct.

This post has been edited by Curtis Rutland: 16 May 2011 - 07:24 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1