473,397 Members | 2,099 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,397 software developers and data experts.

C# How to declare a generic property with a constraint?

I'm trying to declare a property that is a generic and has a constraint and having no luck. Is it possible?

I want something like the following:
Expand|Select|Wrap|Line Numbers
  1. public interface IDeckPosition
  2. {
  3. ...
  4. }
  5.  
  6. public interface IRobotDeck
  7. {
  8. ...
  9.   Collection<T> Positions where T: IDeckPosition
  10. }
  11.  
  12.  
Nov 19 '07 #1
16 21787
Shashi Sadasivan
1,435 Expert 1GB
I'm trying to declare a property that is a generic and has a constraint and having no luck. Is it possible?

I want something like the following:
Expand|Select|Wrap|Line Numbers
  1. public interface IDeckPosition
  2. {
  3. ...
  4. }
  5.  
  6. public interface IRobotDeck
  7. {
  8. ...
  9.   Collection<T> Positions where T: IDeckPosition
  10. }
  11.  
  12.  
  13.  
What about
Expand|Select|Wrap|Line Numbers
  1. Collection<IDeckPosition> Positions;
Nov 19 '07 #2
The problem with that is that the following does not work:

Expand|Select|Wrap|Line Numbers
  1. public class RobotDeck : IRobotDeck
  2. {
  3.   private Collection<MyDeckPosition> _positions;
  4.  
  5.   public Collection<IDeckPosition> Positions
  6.   { get return _positions;}
  7. }
  8.  
The above throws an error that there is no automatic conversion between the two collections.
Nov 19 '07 #3
Plater
7,872 Expert 4TB
Then cast it manually?
If MyDeckPosition inherits from the IDeckPosition class, you chould be able to cast it:
Expand|Select|Wrap|Line Numbers
  1. public class RobotDeck : IRobotDeck
  2. {
  3.   private Collection<MyDeckPosition> _positions;
  4.  
  5.   public Collection<IDeckPosition> Positions
  6.   { 
  7.     get
  8.     { 
  9.       return (Collection<IDeckPosition>)_positions;
  10.     }
  11. }
  12.  
The rules for casting T type collections might be different, but I'm fairly certain it can be done.
Nov 19 '07 #4
r035198x
13,262 8TB
The problem with that is that the following does not work:

Expand|Select|Wrap|Line Numbers
  1. public class RobotDeck : IRobotDeck
  2. {
  3.   private Collection<MyDeckPosition> _positions;
  4.  
  5.   public Collection<IDeckPosition> Positions
  6.   { get return _positions;}
  7. }
  8.  
The above throws an error that there is no automatic conversion between the two collections.
I think your problem is that you are trying to declare an attribute in an interface.
Interfaces can contain properties along with Events, methods and indexers but they cannot contain attributes.
Nov 19 '07 #5
I get the same error "cannot convert type x to type y via built in conversion" whether the conversion is automatic or manual.
Nov 19 '07 #6
Plater
7,872 Expert 4TB
Does MyDeckPosition inherit/implement from the IDeckPosition interface?
Actually, why not just use the Strong type instead of the interface to begin with?
Nov 19 '07 #7
I think your problem is that you are trying to declare an attribute in an interface.
Interfaces can contain properties along with Events, methods and indexers but they cannot contain attributes.
It's definitely not an attribute.
Nov 19 '07 #8
r035198x
13,262 8TB
It's definitely not an attribute.
What then do you call it? I'm talking about the Positions "attribute" in the IRobotDeck interface It not a property, event, method or indexer.
... and those are the only things you can declare inside an interface.
Nov 19 '07 #9
Does MyDeckPosition inherit/implement from the IDeckPosition interface?
Yes, It inherits from the interface.

Actually, why not just use the Strong type instead of the interface to begin with?
I have a visual control that I'll use to display the RobotDeck. This control will be in a different assembly, our controls assembly. I did not want to introduce a dependency to our manufacturing assembly.

I may just break out that control into its own assembly and live with the dependency.
Nov 19 '07 #10
What then do you call it? I'm talking about the Positions "attribute" in the IRobotDeck interface It not a property, event, method or indexer.
... and those are the only things you can declare inside an interface.
Sorry. I forgot to add the getter. Does the following make it clearer:

Expand|Select|Wrap|Line Numbers
  1. public interface IRobotDeck
  2. {
  3. ..
  4.   Collection<T> Positions where T: IDeckPosition {get;}
  5.   int MyProperty {get;}
  6. }
  7.  
Nov 19 '07 #11
r035198x
13,262 8TB
Yes, It inherits from the interface.



I have a visual control that I'll use to display the RobotDeck. This control will be in a different assembly, our controls assembly. I did not want to introduce a dependency to our manufacturing assembly.

I may just break out that control into its own assembly and live with the dependency.
Shouldn't it be like
Expand|Select|Wrap|Line Numbers
  1. interface IRobotDeck {
  2.           Collection<IDeckPosition> Positions {
  3.                  get;
  4.                  set;
  5.           }
  6. }
Nov 19 '07 #12
Plater
7,872 Expert 4TB
Shouldn't it be like
Expand|Select|Wrap|Line Numbers
  1. interface IRobotDeck {
  2.           Collection<IDeckPosition> Positions {
  3.                  get;
  4.                  set;
  5.           }
  6. }
Only if you want the object exposed by Positions to be writeable(changable, for things like a collection, you can still .Add()/.Remove() on a GET only property). Read-only properties would only contain the get right?
Nov 19 '07 #13
Only if you want the object exposed by Positions to be writeable(changable, for things like a collection, you can still .Add()/.Remove() on a GET only property). Read-only properties would only contain the get right?
Correct, specifying teh getter means that it is read only. Which is what I want.
Nov 19 '07 #14
r035198x
13,262 8TB
Correct, specifying teh getter means that it is read only. Which is what I want.
So you are still getting the conversion error thing? Why don't you post all your class definitions.
Nov 19 '07 #15
So you are still getting the conversion error thing? Why don't you post all your class definitions.
Sorry about the late response, I got tied up with a couple of other things. I think that I'm going to go with two collections one with the actual objects and then the other will contain the interfaces for the objects. Syncing shouldn't be a problem as the list will be static.
Nov 20 '07 #16
public interface IRobotDeck<T> where T : IDeckPosition
{
Collection<T> Positions {
get;
set;
}
}
Jan 6 '10 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
9
by: Alon Fliess | last post by:
Hi I am trying to write a generic class that instantiates the generic type, but I can not find the correct way to give it the constructor constraint. For example: In C#: class X<T> where...
4
by: Jethro Guo | last post by:
C++ template use constraint by signature,It's very flexible to programmer but complex for complier, and at most time programmer can not get clear error message from complier if error occur. C#...
6
by: Dan Holmes | last post by:
I have a class that i need a constraint of int, string, float or bool. I have tried the following but can't make VS accept it. I read the docs and they showed that any value type can be used...
25
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
3
by: BombDrop | last post by:
I'm working on a system that has 4 different types of client they have mostly the same attributes so I decided to create an interface IClient for them to implement and then just add the differing...
3
by: =?Utf-8?B?R0I=?= | last post by:
I have created a small program that illustrates the problem. I would know how to address the fields that I want to sort on in the greaterThan comparison. Anybody who knows?? using System; ...
4
by: tadmill | last post by:
Hi, Is it possible for a generic list class to use Reflection on the generic type being used to detect a property within that type that refers to the same generic class, only with a different...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
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...
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...
0
agi2029
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,...

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.