Cannot create an instance of the variable type 'TsmNodes' because it does not have the new() constraint

Here is the interface:
public interface IComPlugin {	
   string PluginName { get; set; }
   IComPlugin GetPlugin();
   T GetAllNodesData<T>(String sourceConfigFileName, string nameFilter, out List<Exception> outExceptions);
}

Open in new window


Here is the class in short:
public class TsmMethods : IComPlugin {
   public string PluginName { get; set; }
   public TsmMethods(string pluginName) {
			this.PluginName = pluginName;
		}
   public IComPlugin GetPlugin() {
			return this;
		}
   public TsmNodes GetAllNodesData<TsmNodes>(String sourceConfigFileName, string nameFilter, out List<Exception> outExceptions) {
       XmlReaderLocal tsmServersConfigReader = new XmlReaderLocal(sourceConfigFileName);
       List<Hashtable> hashtableList = tsmServersConfigReader.ReadAllServers();
       Exception cachedTSMException = new Exception("unassigned");
       List<Exception> loopExceptions = new List<Exception>();
       TsmNodes allNodes = new TsmNodes(); //Here is where the error happens
//And so on..
   }
}

Open in new window


So I can't have a function of a generic type that returns the same generic type?
How to do this the right way?
LVL 2
itniflAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
You need to add a type constraint to you generic method, specifically the "new()" constraint:

public TsmNodes GetAllNodesData<TsmNodes>(String sourceConfigFileName, string nameFilter, out List<Exception> outExceptions)
    where TsmNodes : new() {
...

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itniflAuthor Commented:
I made a change to the interface to make it look like something as this:

public interface IComPlugin {	
	string PluginName { get; set; }
	IComPlugin GetPlugin();
	T1 GetAllNodesData<T1, T2>(String sourceConfigFileName, string nameFilter, out List<Exception> outExceptions) 
		where T1 : IComNodeList<T2>, new()
			where T2 : IComNode, new();
}

Open in new window

That worked, so I guess you are right.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.