generics and static methods question

I'm learning generics and c#. Is there a way to call static methods in the T class so I can streamline this ? I have two static methods in T named GetItem and SaveItem that I would like to invoke by something like T.GetItem(astring) and T.SaveItem(t). Reading some of the other posts seems to indicate that a static method can not be used. Thanks for any recommendations.

publicclassFrmUpdateManager<T> :Form

{

T t;

// etc

// would like to do something like T.GetItem(tb.Text) to eliminate the if clause

// for each type I implement

if (typeof(T) ==typeof(InsuranceCompany))

{

object obj;

obj = (InsuranceCompany.GetItem(tb.Text));

t = (T)obj;

displayFormValues();

}

//etc

}

[1639 byte] By [ga2006] at [2007-12-26]
# 1

Have you tried adding a type parameter constraint to ensure T has those static methods? For example, add

class Persistent {

public static GetItem( string name );

public static SaveItem( string name );

}

And modify the generic class declaration to:

public class FrmUpdateManager<T> : Form where T : Persistent

Then compiler will know T.GetItem( string) result in successful member lookup. Of course, actual supplied type for T will have to be Persistent or its subclass. Because you want static methods, interface will not work.

Dasa at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2

Better practise -depends on the exact implementation though!!- to use an interface and a constraing

interface ISomeInterface{

//interface members go here

}

class SomeForm<T> where T : ISomeInterface{

//interface implemetations go here

}

JelleB. at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3

The methods I want to use are static methods, so an interface won't suffice.

The types don't share a common ancestor either.

Could I pass the methods to the routine as delegates?

ga2006 at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 4
The basic issue is that the compiler needs to be able to check that T.staticMethod() is valid method invocation for type T. What are the characteristics of parameter types that you expect to use with the genertic type? And how many are they? It may be the case that generic type may not be the suitable solution for the problem.
Dasa at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 5

Yes Dasa, you have stated the issue correctly. If an interface would work with static methods, it would be a perfect solution. I tried it first.

I wish to pass several different types, but all the types have a static GetItem and SaveItem defined to retrieve and save the object from/to a legacy database. The generalized Update class will use reflection to present the object in a windows form (using the GetItem), and then persiste it back to the database using the SaveItem. GetItem will always take a single string parameter and return an instance of the type. SaveItem will take a type instance as a parameter. for example, one type is for a table of insurance companies:

public static InsuranceCompany GetItem(string ID) ......

public static void SaveItem(InsuranceCompany ico) .....

and another type might for a patient

public static Patient GetItem(string ID) ......

public static void SaveItem(Patient pat)....

Thanks for any suggestions.

ga2006 at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 6
No bright idea ... the delegate idea sounds pretty good to me - you're using reflection already anyways.
Dasa at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 7

this seems the exact case in which one wants to use a interface IPersistable

i which case all the object should implement this interface...

If you use another technique this is (imo) considered bad OO design...

JelleB. at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 8
I was unable to use an interface with a static method. If you are able to, let me know how. Thanks
ga2006 at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 9

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication1 {

class Program {

static void Main(string[] args) {

MyObject o = new MyObject();

MyGenericObject<MyObject> go = new MyGenericObject<MyObject>();

go.DoSomething();

Console.ReadKey();

}

}

interface IPersistant {

void Save();

void Load(string identifier);

}

class MyObject : IPersistant{

#region IPersistant Members

public void Save() {

Console.WriteLine("Saving");

}

public void Load(string identifier) {

Console.WriteLine("Loading: {0}", identifier);

}

#endregion

}

class MyGenericObject<T> where T : IPersistant, new() {

private T t;

public MyGenericObject() {

t = new T();

}

public MyGenericObject(T t) {

this.t = t;

}

public void DoSomething() {

t.Load("AZERTY");

}

}

}

JelleB. at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 10
How about this?

public interface IPersister<T> {
T InvokeGet(string id);
void InvokeSave(T obj);
}

public class Persister1 : IPersister<Persister1> {
public static Persister1 GetItem(string id) { return null; }
public static void SaveItem(Persister1 obj) {}
public Persister1 InvokeGet(string id) { return GetItem(id); }
public void InvokeSave(Persister1 obj) { SaveItem(obj); }
}

public class GenericPersist<T> where T: IPersister<T>, new() {
private T instance;
public GenericPersist() {
instance = new T();
}
public T GetItem(string id) {
return instance.InvokeGet(id);
}
public void SaveItem(T obj) {
instance.InvokeSave(obj);
}
}

public class Test {
private GenericPersist<Persister1> persist = new GenericPersist<Persister1>();
public void test() {
Persister1 obj = persist.GetItem("nobugz");
persist.SaveItem(obj);
}
}

nobugz at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 11

Yes! Getting to the static method via an instance method was an idea I was considering but could not express correctly.

Thanks.

ga2006 at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified