Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
class BackupVirtualDataSource : AbstractVirtualListDataSource
    {
 List<BackUpInfo> Objects;
        public int ItemCount = 0;
        //BackUpInfo
        public BackupVirtualDataSource(VirtualObjectListView listView, List<BackUpInfo> ObjList)
            : base(listView)
        {
            this.Objects = ObjList;
            ItemCount = (ObjList == null) ? 0 : ObjList.Count;
        }

        public override int GetObjectIndex(object model)
        {
            return this.Objects.IndexOf((BackUpInfo)model);
        }

        public override object GetNthObject(int n)
        {
            BackUpInfo p = null;
            try
            {
                p = this.Objects[n % this.Objects.Count];
                return p;
            }
            catch (Exception ex)
            {
                cGlobalSettings.oLogger.WriteLogException("| BackupVirtualDataSource.cs::GetNthObject() |", ex);
            }
            finally 
            {
                p = null;
            }
            return p;
            
        }

        public override int GetObjectCount()
        {
            return ItemCount;
        }

       
    }

i want to convert the following class to generics but iam getting error
The type or namespace name T could not be found

What I have tried:

C#
class BackupVirtualDataSource : AbstractVirtualListDataSource
    {
 List<T> Objects;
        public int ItemCount = 0;
        //BackUpInfo
        public BackupVirtualDataSource(VirtualObjectListView listView, List<T> ObjList)
            : base(listView)
        {
            this.Objects = ObjList;
            ItemCount = (ObjList == null) ? 0 : ObjList.Count;
        }

        public override int GetObjectIndex(object model)
        {
            return this.Objects.IndexOf((T)model);
        }

        public override object GetNthObject(int n)
        {
            T p = null;
            try
            {
                p = this.Objects[n % this.Objects.Count];
                return p;
            }
            catch (Exception ex)
            {
                cGlobalSettings.oLogger.WriteLogException("| BackupVirtualDataSource.cs::GetNthObject() |", ex);
            }
            finally 
            {
                p = null;
            }
            return p;
            
        }

        public override int GetObjectCount()
        {
            return ItemCount;
        }

       
    }
Posted
Updated 2-Aug-16 1:05am
Comments
Philippe Mori 2-Aug-16 8:58am    
Your GetNthObject function is way too long and misleading. The following code is much better and far easier to understand.

public override object GetNthObject(int n)
{
  try
  {
    return this.Objects[n % this.Objects.Count];
  }
  catch (Exception ex)
  {
    cGlobalSettings.oLogger.WriteLogException(
      "| BackupVirtualDataSource.cs::GetNthObject() |", ex);
  }
  return null;
}

1 solution

You need to add the generic definition to the class, and that is then available inside the class

C#
class BackupVirtualDataSource<T> : AbstractVirtualListDataSource
 
Share this answer
 
Comments
srilekhamenon 2-Aug-16 7:17am    
ok i miss the generic definition to the class :)
BillWoodruff 2-Aug-16 8:25am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900