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: -
public interface IDeckPosition
-
{
-
...
-
}
-
-
public interface IRobotDeck
-
{
-
...
-
Collection<T> Positions where T: IDeckPosition
-
}
-
-
16 21787
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: -
public interface IDeckPosition
-
{
-
...
-
}
-
-
public interface IRobotDeck
-
{
-
...
-
Collection<T> Positions where T: IDeckPosition
-
}
-
-
-
What about - Collection<IDeckPosition> Positions;
The problem with that is that the following does not work: -
public class RobotDeck : IRobotDeck
-
{
-
private Collection<MyDeckPosition> _positions;
-
-
public Collection<IDeckPosition> Positions
-
{ get return _positions;}
-
}
-
The above throws an error that there is no automatic conversion between the two collections.
Then cast it manually?
If MyDeckPosition inherits from the IDeckPosition class, you chould be able to cast it: -
public class RobotDeck : IRobotDeck
-
{
-
private Collection<MyDeckPosition> _positions;
-
-
public Collection<IDeckPosition> Positions
-
{
-
get
-
{
-
return (Collection<IDeckPosition>)_positions;
-
}
-
}
-
The rules for casting T type collections might be different, but I'm fairly certain it can be done.
The problem with that is that the following does not work: -
public class RobotDeck : IRobotDeck
-
{
-
private Collection<MyDeckPosition> _positions;
-
-
public Collection<IDeckPosition> Positions
-
{ get return _positions;}
-
}
-
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.
I get the same error "cannot convert type x to type y via built in conversion" whether the conversion is automatic or manual.
Does MyDeckPosition inherit/implement from the IDeckPosition interface?
Actually, why not just use the Strong type instead of the interface to begin with?
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.
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.
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.
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: -
public interface IRobotDeck
-
{
-
..
-
Collection<T> Positions where T: IDeckPosition {get;}
-
int MyProperty {get;}
-
}
-
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 - interface IRobotDeck {
-
Collection<IDeckPosition> Positions {
-
get;
-
set;
-
}
-
}
Shouldn't it be like - interface IRobotDeck {
-
Collection<IDeckPosition> Positions {
-
get;
-
set;
-
}
-
}
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?
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.
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.
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.
public interface IRobotDeck<T> where T : IDeckPosition
{
Collection<T> Positions {
get;
set;
}
}
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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#...
|
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...
|
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...
|
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...
|
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...
|
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;
...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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: 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,...
| |