Brian Hanf Brian Hanf - 1 year ago 28
C# Question

Do I have to pass my T in my abstract class to use it, can I use self / this

I have an abstract class on a bunch of data models for doing some database update / insert type work (Importing parts of complex structure -users and their settings- from one database to another).

It seems I can't use

this
to do any work with the items in T. I would have thought I could have done so. Instead it seems I need to make the method static and pass in an object of type T.

public abstract class AImportItemListDatabaseCalls<T, T2>
where T : IImportListBase<T2>, IImportListDatabaseCalls<T2>
where T2 : IImportItem
{


public static bool ProcessListToDestination(T DataListModel, SqlConnection connection, SqlTransaction tran)
{

foreach (T2 item in DataListModel.ImportItems)
{
if (item.IsPaired) // Do, Do Not Insert work - different by T
{
if (!DataListModel.DestinationDoNotInsert(item, connection, tran))
{
//
}


}
else // Do, Insert work - different by T
{
if (!DataListModel.DestinationInsertInto(item, connection, tran))
{
//
}
}

}

return true;
}
}


Edit: what I would like to do

public abstract class AImportItemListDatabaseCalls<T, T2>
where T : IImportListBase<T2>, IImportListDatabaseCalls<T2>
where T2 : IImportItem
{


public bool ProcessListToDestination( SqlConnection connection, SqlTransaction tran)
{

foreach (T2 item in ImportItems)
{
if (item.IsPaired) // Do, Do Not Insert work - different by T
{
if (!DestinationDoNotInsert(item, connection, tran))
{
//
}


}
else // Do, Insert work - different by T
{
if (!DestinationInsertInto(item, connection, tran))
{
//
}
}

}

return true;
}
}


solution:

public abstract class AImportItemListDatabaseCalls< T2> : IImportListBase<T2>, IImportListDatabaseCalls<T2>
where T2 : IImportItem
{

public abstract List<T2> ImportItems { get; set; }
public abstract bool DestinationInsertInto(T2 item, SqlConnection connection, SqlTransaction tran);
public abstract bool DestinationDoNotInsert(T2 item, SqlConnection connection, SqlTransaction tran);
public bool ProcessListToDestination( SqlConnection connection, SqlTransaction tran)
{

foreach (T2 item in ImportItems)
{
if (item.IsPaired) // Do, Do Not Insert work - different by T
{
if (!DestinationDoNotInsert(item, connection, tran))
{
//
}


}
else // Do, Insert work - different by T
{
if (!DestinationInsertInto(item, connection, tran))
{
//
}
}

}

return true;
}
}

Answer Source

It seems I can't use this. to do any work with the items in T

Of course, not. What items in T? AImportItemListDatabaseCalls does not contain a T or a T2. It merely has those types (not object of those types) available to it.

You probably want to do something like this:

public abstract class AImportItemListDatabaseCalls<T2> :
                           ImportListBase<T2>, IImportListDatabaseCalls<T2>
                      where T2 : IImportItem

where there's now a base class based on T2. THEN you can use this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download