Calling indexer from within the same (generic) class - c#

public class MyClass<T>
{
public T this[int index]
{
get
{
...
}
set
{
...
}
}
public void MyMethod<T>()
{
int middleIndex = ...;
T value = this[middleIndex ];
...
}
}
The code won't compile because of the statement in MyMethod(). Is there another way of calling the indexer ?
Edit: Modified MyMethod()
Edit2: Compilation error
Error 6 Cannot implicitly convert type 'T [C:\MyClass.cs]' to 'T [C:\MyClass.cs]'
Thanks.

Works fine for me:
public class MyClass<T>
{
public T this[int index]
{
get
{
return default(T);
}
set
{
}
}
public void MyMethod(int index)
{
T value = this[index];
}
}
Admittedly I had to introduce the index parameter into MyMethod, but I'm assuming you were wanting to get the index from somewhere... if that's not what you meant, please clarify.

This works fine for me:
public class MyClass<T>
{
List<T> _items = new List<T>();
public T this[int index]
{
get
{
return _items[index];
}
}
public void MyMethod()
{
T value = this[2];
}
}

Calling the indexer is fine, but it doesn't know which index you want. If you make index a parameter of MyMethod it will work fine.
If you're trying to get the current index or something then you need to store a private variable, wire it up in your indexer and access that.
Your edited code compiles fine...
public class MyClass<T>
{
public T this[int index]
{
get
{
...
}
set
{
...
}
}
public void MyMethod()
{
int middleIndex = ...;
T value = this[middleIndex ];
...
}
}

you're not passing in a value for the index into the method MyMethod - can you post a little more code? It looks like something is missing...

Your offending code is here:
public void MyMethod<T>()
Your class MyClass<T> already has a generic type parameter T, so the generic <T> on MyMethod is unnecessary

Related

c# list of interface with generic items

My question is somewhat similar to
Generic List of Generic Interfaces not allowed, any alternative approaches?
If I have an interface such as
public interface IPrimitive
{
}
public interface IPrimitive<T> : IPrimitive
{
T Value { get; }
}
public class Star : IPrimitive<string> //must declare T here
{
public string Value { get { return "foobar"; } }
}
public class Sun : IPrimitive<int>
{
public int Value { get { return 0; } }
}
Then I have a list
var myList = new List<IPrimitive>();
myList.Add(new Star());
myList.Add(new Sun());
When looping through this list, how do I get the Value property?
foreach (var item in myList)
{
var value = item.Value; // Value is not defined in IPrimitive so it doesn't know what it is
}
I'm not sure how this is possible.
Thanks,
Rob
You can take advantage of dynamic:
foreach (dynamic item in myList)
{
var value = item.Value;
}
The dynamic type enables the operations in which it occurs to bypass compile-time type checking. Instead, these operations are resolved at run time
You could do something like this:
public interface IPrimitive
{
object Value { get; }
}
public interface IPrimitive<T> : IPrimitive
{
new T Value { get; }
}
public class Star : IPrimitive<string> //must declare T here
{
public string Value { get { return "foobar"; } }
object IPrimitive.Value { get { return this.Value; } }
}
public class Sun : IPrimitive<int>
{
public int Value { get { return 0; } }
object IPrimitive.Value { get { return this.Value; } }
}
You're then able to get the value out as an object when you only have IPrimitive.
of course not, your value is going to be of different types..... so you will have to downcast to the real type to get at the different values.
Basically your interface is failing. Its not "A common interface" It's more a "similar interface"
If you don't want to do casting, then you will have to find an interface which is common to both of them.
You can move you Value property to base interface.
public interface IPrimitive
{
object Value { get; }
}
How do you want to procced value in the loop it has different type?

Cast issues between generic list and CollectionDataContract

I have the following collection contract defined:
[CollectionDataContract(Name = "Centres")]
public class Centres : List<Centre>
{}
and the following operation contract defined to return this collection
public Model.Centres GetCentres()
{
List<Centre> allCentres = (from c in Model.Centre.GetCentres()
where c.Visible == true
select c).ToList();
return allCentres
}
But when I run the code I receive an ExplicitCastException. So as far as I can see I'm trying to cast a list of centres (List) into my collection 'Centres' which itself derives from List. Is this possible or by deriving a new object am I creating a new type of list that won't work in this way.
My current work around for this problem is to declare a new instance of Centres and copy all centres into it using a foreach.
The problem is Centres "is a" List<Centre>, List<Centre> is not a Centres.
Despite Centres having no implementation it is still a sub-class of List<Centre>, you could extend your Centres class to have an implicit conversion operator or, perhaps add a constructor to Centres that takes a List<Centre> as a parameter.
Try changing Centres to somthing like ...
[CollectionDataContract(Name = "Centres")]
public class Centres : List<Centre>
{
public static implicit operator Centres(List<Centre> l)
{
Centres newCentres = new Centres();
newCentres.AddRange(l);
return newCentres;
}
}
Then it will allow implicit conversion from List<Centre>.
What you are trying wont work.
If possible you should consider refactoring Centres to a has a List<Centre> more than is a relationship or at least define a constructor that takes an IEnumerable<Centre>
This way you would be able to write:
Centres allCentres = new Centres(from c in Model.Centres.GetCentres()
where c.Visible == true
select c);
Of course it all depends on your specific situation where this might not be a valid solution.
What you can do is create your own implementation of the IList<T> interface that wraps an IList.
It takes more code, but will not be as slow as to copy all the objects:
[CollectionDataContract(Name = "Centres")]
public class Centres : IList<Centre>
{
private IList<Centre> _inner;
private IList<Centre> Inner
{
get
{
if (_inner == null)
_inner = new List<Centre>();
return _inner;
}
}
public Centres(List<Centre> items)
{
_inner = items;
}
#region IList<Centre> Members
public int IndexOf(Centre item)
{
return Inner.IndexOf(item);
}
public void Insert(int index, Centre item)
{
Inner.Insert(index, item);
}
public void RemoveAt(int index)
{
Inner.RemoveAt(index);
}
public Centre this[int index]
{
get
{
return Inner[index];
}
set
{
Inner[index] = value;
}
}
#endregion
#region ICollection<Centre> Members
public void Add(Centre item)
{
Inner.Add(item);
}
public void Clear()
{
Inner.Clear();
}
public bool Contains(Centre item)
{
return Inner.Contains(item);
}
public void CopyTo(Centre[] array, int arrayIndex)
{
Inner.CopyTo(array, arrayIndex);
}
public int Count
{
get { return Inner.Count; }
}
public bool IsReadOnly
{
get { return Inner.IsReadOnly; }
}
public bool Remove(Centre item)
{
return Inner.Remove(item);
}
#endregion
#region IEnumerable<Centre> Members
public IEnumerator<Centre> GetEnumerator()
{
return Inner.GetEnumerator();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return Inner.GetEnumerator();
}
#endregion
}

C#: public method{get{} set{}} question

I'm not entirely sure if I have all the terminology correct so forgive me if I'm wrong. I was wondering if it would be possible to send an argument(s) to the method. Take the following for example.
public item (int index)
{
get { return list[index]; }
set { list[index] = value; }
}
I know that as it is, it will error. What I'm asking is if there is some way to get it working. Any suggestions or should I figure out some way around it?
Thanks in advance.
Try this:
// This returns an instance of type "Foo",
// since I didn't know the type of "list".
// Obviously the return type would need to
// match the type of whatever "list" contains.
public Foo this[int index]
{
get { return list[index]; }
set { list[index] = value; }
}
This is C#'s indexer syntax and it has some limitations (it's not as flexible as VB.NET's parameterful properties) but it does work for your specific example.
As others have shown, you can turn it into an indexer - which can have multiple parameters, by the way.
What you can't do is name an indexer in C#... although you can in VB. So you can't have two indexers, one called Foo and the other called Bar... you'd need to write properties which returned values which were themselves indexable. It's a bit of a pain, to be honest :(
This is called indexer property
public int this [int index]
{
get { return list[index]; }
set { list[index] = value; }
}
I think what you might be looking for is:
public Something this[int index]
{
get
{
return list[index];
}
set
{
list[index] = value;
}
}
For the record, Whilst the other answers are valid, you might also want to consider using the following approach:
public IList<Something> items { get; set; }
This could then be used as follows:
Something item = myFoo.items[1];
The other answers would be used in the following, slightly different, way:
Something item = myFoo[1];
The one you want depends on what exactly you are trying to achieve, which is difficult to determine without seeing the rest of the code.
Besides the indexer that has been mentioned several times now, another possibility is to make a custom class with an indexer and return an instance of it as a property.
Example:
public class IntList
{
public IntList(IEnumerable<int> source)
{
items = source.ToArray();
Squares = new SquareList(this);
}
private int[] items;
// The indexer everyone else mentioned
public int this[int index]
{
get { return items[index]; }
set { items[index] = value; }
}
// Other properties might be useful:
public SquareList Squares { get; private set; }
public class SquareList
{
public SquareList(IntList list)
{
this.list = list;
}
private IntList list;
public int this[int index]
{
get { return list.items[index] * list.items[index]; }
}
}
}
You can use indexator for solving this problem
public object this[string name]
{
get
{
int idx = FindParam(name);
if (idx != -1)
return _params[idx].Value;
throw new IndexOutOfRangeException(String.Format("Parameter \"{0}\" not found in this collection", name));
}
set
{
int idx = FindParam(name);
if (idx != -1)
_params[idx].Value = value;
else
throw new IndexOutOfRangeException(String.Format("Parameter \"{0}\" not found in this collection", name));
}
}

Implement a function as []

I have an array which really is a function, however i would like to use it as an array. I know i can write these
int var { get{return v2;} }
public int this[int v] { get { return realArray[v]; }
but how do i implement a function that like an array? i would like to do something like
public int pal[int i] { get { return i*2; } }
But that get a compile error
error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
In C#, the only possible way to declare a parameterized property is an indexer. However, you could simulate something like that by creating a class that provides an indexer and adding a property of that type to your class:
class ParameterizedProperty<TProperty, TIndex> {
private Func<TIndex, TProperty> getter;
private Action<TIndex, TProperty> setter;
public ParameterizedProperty(Func<TIndex, TProperty> getter,
Action<TIndex, TProperty> setter) {
this.getter = getter;
this.setter = setter;
}
public TProperty this[TIndex index] {
get { return getter(index); }
set { setter(index, value); }
}
}
class MyType {
public MyType() {
Prop = new ParameterizedProperty<string, int>(getProp, setProp);
}
public ParameterizedProperty<string, int> Prop { get; private set; }
private string getProp(int index) {
// return the stuff
}
private void setProp(int index, string value) {
// set the stuff
}
}
MyType test = new MyType();
test.Prop[0] = "Hello";
string x = test.Prop[0];
You can extend the idea to read only and write only properties by removing getter or setter from the class as appropriate.
As you noticed, you cannot name an indexer like that, so either:
public int this[int i] { get { return i * 2; } }
Or alternatively if you are really set on naming it pal:
public class Wrapper
{
public int this[int i] { get { return i * 2; } }
}
...
public Wrapper pal { get { return _someWrapperInstance; } }
Which can then be accessed pal[ix], pal[3], etc.
Either you return an array object:
public int[] pal { get { return realArray; } }
or you return an object that has an indexer:
public class ActingAsArray {
private int[] _arr;
public ActingAsArray(int[] arr) { _arr = arr; }
public int this[int v] { get { return _arr[v]; } }
}
public ActingAsArray pal { get { return new ActingAsArray(realArray); } }
You cannot overload (overloadable operators) the bracket operator in C#. The best you can do is implement an indexer, as you have shown. As per the documentation, you must use the this keyword to implement an indexer. Indexers work much like properties, they have a getter and a setter and you can perform just about any function in the getter or setter.
If you don't mind using a bit of VB.Net, it supports parameterized properties (still beats me why it's not possible in C#, as .Net is obviously capable of doing it)
This way you could create your class in VB.Net and just reference the VB.Net DLL in your project.
This could of course get somewhat annoying if your class changes often :-/

C# - Can a List<MyClass> be seamlessly cast to a List<Interface> or similar?

I have a DataSource in my control which is always a List<T> where T has to inherit from IEntity.
public class MyClass<T> where T : IEntity
{
public List<T> DataSource
{
get;
set;
}
}
Now, obviously you can't cast a List<T> to a List<IEntity> doing the following:
List<IEntity> wontWork = (List<IEntity>)this.DataSource;
How can I get the DataSource as a List of IEntity, whilst still being able to add and remove items from the DataSource? I.e. I could do the following, but removing from the List it returns would not remove from the DataSource:
public List<TOut> GetDataSourceCopyAsUnderlyingType<TOut>()
{
if (this.DataSource == null)
{
return new List<TOut>();
}
else
{
// Get the list and the enumerator
IList list = (IList)this.DataSource;
IEnumerator enumerator = list.GetEnumerator();
// Build the target list
List<TOut> targetList = new List<TOut>();
int i = 0;
while (enumerator.MoveNext())
{
TOut entity = (TOut)list[i];
targetList.Add(entity);
i++;
}
return targetList;
}
}
Basically, I need some way of doing the following:
List<IEntity> interfaceList = this.GetDataSourceAsAnotherType<IEntity>();
int dataSourceCount = this.DataSource.Count; // Equals 5
int interfaceCount = interfaceList.Count; // Equals 5
interfaceList.RemoveAt(0);
int dataSourceCount = this.DataSource.Count; // Equals 4
int interfaceCount = interfaceList.Count; // Equals 4
And just to add, I don't mind if it means I've got to use a different type instead of a List.
EDIT: Sorry, forgot to say I'm using .Net2.0 and cannot move to .Net 3.5.
It would be a monumentally bad idea if this were allowed, which is why it isn't. I can add any old IEntity to a List<IEntity> which will blow up if that IEntity can't be cast to T. Whilst all Ts are IEntities, not all IEntities are Ts.
This works with arrays because arrays have a deliberate subtyping hole (as they do in Java). Collections do not have a subtyping hole.
Create a wrapper class that seamlessly converts. Untested sample:
public class CastList<TTarget, TOriginal>
: IList<TTarget> where TOriginal : TTarget
{
List<TOriginal> _orig;
public CastList(List<TOriginal> orig) { _orig = orig; }
public Add(TTarget item) { _orig.Add(item); }
public TTarget this[int i]
{
get { return (TTarget)_orig[i]; }
set { _orig[i] = value; }
}
public IEnumerator<TTarget> GetEnumerator()
{
foreach(TOriginal item in _orig)
yield return (TTarget)item;
}
// etc...
}
Manipulations of the original list will also be reflected in the wrapper. To use this, just construct it with your DataSource.
What DrPizza said, but with more code:
public class ListFacade<TIn, TOut> : IList<TOut> where TIn : TOut
{
private readonly IList<TIn> innerList;
public ListFacade(IList<TIn> innerList)
{
this.innerList = innerList;
}
public int Count
{
get { return this.innerList.Count; }
}
public bool IsReadOnly
{
get { return this.innerList.IsReadOnly; }
}
public TOut this[int index]
{
get { return this.innerList[index]; }
set { this.innerList[index] = (TIn)value; }
}
public void Add(TOut item)
{
this.innerList.Add((TIn)item);
}
public void Clear()
{
this.innerList.Clear();
}
public bool Contains(TOut item)
{
return (item is TIn) && this.innerList.Contains((TIn)item);
}
public void CopyTo(TOut[] array, int arrayIndex)
{
var inArray = new TIn[this.innerList.Count];
this.innerList.CopyTo(inArray, arrayIndex);
Array.Copy(inArray, array, inArray.Length);
}
public IEnumerator<TOut> GetEnumerator()
{
foreach (var item in this.innerList)
{
yield return item;
}
}
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public int IndexOf(TOut item)
{
return (item is TIn) ? this.innerList.IndexOf((TIn)item) : -1;
}
public void Insert(int index, TOut item)
{
this.innerList.Insert(index, (TIn)item);
}
public bool Remove(TOut item)
{
return (item is TIn) && this.innerList.Remove((TIn)item);
}
public void RemoveAt(int index)
{
this.innerList.RemoveAt(index);
}
Add, Insert and the indexer set will blow up if the argument is not of type TIn.
ok this might be completely beside the point but, how about using a little bit of Linq?
var interfaceList = objectList.ConvertAll<Interface>(o => (Interface)o);
this way you can cast the objectList easily.
hope this helps to find the solution...
I'm in favor of linq too, but you can do it like:
var interfaceList = objectList.Cast<IEntity>();
Which is shorter and more expressive.

Categories

Resources