1

I have a class (students) with say 2 strings and an int array

A second class then does this:

List<student> myS = new List<student>();

All of this works correctly and I have a list containing multiple students. I am however having difficulty accessing the values within the int[].

So, my student list is populated from a database and into a list.

I then have a generic class which has a parameter List which I then want as a data table. The generic class will be called for the student class, subjects class and other classes - some of which may contain arrays and some which do not.

If I debug and step through, I get the following: (() myS.stu[6].termMark[1] and the value is 50. However if I enter int d =stu[6].termMark[1] the error is T does not contain a definition for termMark and no extension method 'termMark' accepting a first argument of type T could be found.

int d = myS[0][1] returns the error

cannot apply indexing with [] to an expression of type 'T'

. I have tried various things like creating a separate list and adding it to myS. Nothing works.

Thanks in advance for the help.

I am fairly new to this and probably missing the obvious...

4
  • int d = myS[0].YourArrayFieldName[1] ; (The field must be public) Commented May 31, 2015 at 14:25
  • You have to use the property name to get the values from student object int d = myS[0].ArrayProperty[1] Commented May 31, 2015 at 14:26
  • student is not a 2 stage list so what is with the second index Commented May 31, 2015 at 14:26
  • I guess you have a generic function somewhere.Show us the entire example Commented May 31, 2015 at 14:29

2 Answers 2

1

You must read about Generic Constraints
You cannot do much with an argument of type T.
If your function accepts arguments of type student , add a constraint like this

private static void NewMethod<T>(List<T> myS) where T : student
{
        int d = myS[0].IntArray[1];
}

The following is not related your problem...
If you add an indexer to your class you can make it a little shorter

public class student
{
    public int[] IntArray;

    public int this[int x]
    {
        get
        {
            return IntArray[x];
        }
    }
}
private static void NewMethod<T>(List<T> myS) where T : student
{
        int d = myS[0][3];
 }
Sign up to request clarification or add additional context in comments.

Comments

0

Perhaps the type T does not hold any definition for indexers. But, your field (int[]) does. So, instead of calling the indexer upon your object (student) call it on the field of it. So, for this answer I would assume that your object is declared as class like this

public class student {
   public string S { get; set; }
   public string s { get; set; }
   public int[] integers { get; set; }
}

Now, you can indeed call the indexers on the integers field but not on the student object itself (it does not have any indexing mechanism). Such as,

int d = myS[0].integers[1]; // <-- your code should be

The above code (if compiled) would give you the element at the index 1 (2nd element) of the first object in the list. You were instead calling the element at index 1 of the student (which does not have indexers?).

1 Comment

myS[0] is obviously of type T that's why he gets that error.Even if he add an indexer in his class he will still get that error...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.