interface and implement again....still not work

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have followed Jon, Morten and Mohanmoss advice, but still not work.

Can someone pls give some comments about the following codes of interface and implements ?
2 files, ITrans.cs and DDDBIQ.cs (is part of the whole solution)

In file ITrans.cs,
namespace Trans
{
interface ITrans
{
string DoTrans(string strXML);
}
}


In file DDDBIQ.cs,
using CDBUtil;
namespace Trans
{
public class DDDBIQ : ITrans
{
public string DoTrans(string strXML)
{
Log.WriteLine(strXML, "DoTrans");
return "<XML><ValidCode>Axis</ValidCode><STATUS>00</STATUS></XML>";
}
}
}


Now I make the DoTrans as public.

When I build the solution, I have the following error.

C:\Inetpub\wwwroot\CDBWebService\Service1.asmx.cs(75): Cannot implicitly convert type 'object' to 'Trans.ITrans'



The following code in Service1.asmx.cs get the error.
string strResponse = string.Empty;
Type typ = Type.GetType("WebService1.Trans." + strClassName);

Trans.ITrans objITrans = Activator.CreateInstance(typ); =====>>>> this line Activator.CreateInstance give error
strResponse = objITrans.DoTrans(strXML);

How to reslove this? Thanks.
 
Francis said:
Trans.ITrans objITrans = Activator.CreateInstance(typ); =====>>>>
this line Activator.CreateInstance give error

Look at the documentation for Activator.CreateInstance - it says it
returns "object". The only way the compiler can know that the object
*actually* implements ITrans is by casting:

Trans.ITrans objITrans = (Trans.ITrans) Activator.CreateInstance(typ);

At this stage I would *strongly* suggest that you take a step back and
start learning the basics of C# and .NET rather than wading straight in
with web services etc - you'll find your later development goes a lot
faster that way.

See http://www.pobox.com/~skeet/java/learning.html for more about this
(tailored to Java, but the same principles apply).
 
Thanks for ur advice. I'm really new to .Net, I need time to learn.
Your suggestion make the program compiled finally. Anyway when I run it, it get error in

Type typ = Type.GetType("WebService1.Trans." + strClassName); ====>>> type = <undefined value> and Type.GetType = <overloaded>
Trans.ITrans objITrans = (Trans.ITrans)Activator.CreateInstance(typ); ====>> exception happens


Do you know why? Thanks.
 
Thanks for ur advice. I'm really new to .Net, I need time to learn.
Your suggestion make the program compiled finally. Anyway when I run it, it get error in

Type typ = Type.GetType("WebService1.Trans." + strClassName); ====>>> type = <undefined value> and Type.GetType = <overloaded>
Trans.ITrans objITrans = (Trans.ITrans)Activator.CreateInstance(typ); ====>> exception happens


Do you know why? Thanks.
 
Francis said:
Thanks for ur advice. I'm really new to .Net, I need time to learn.

But messing with reflection before you know the basics (like why you
couldn't compile before) strikes me as a really bad idea.
Your suggestion make the program compiled finally. Anyway when I run
it, it get error in

Type typ = Type.GetType("WebService1.Trans." + strClassName); ====>>>
type = <undefined value> and Type.GetType = <overloaded>
Trans.ITrans objITrans = (Trans.ITrans)Activator.CreateInstance(typ);
====>> exception happens

Right. Where is your type defined? If it's not in the currently
executing assembly (or mscorlib), and you don't specify the assembly
information in the call to Type.GetType, GetType will return null.

You need to know which assembly to get the type from, and then
(preferrably) call Assembly.GetType.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top