473,473 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

interface and implement again....still not work

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.
Nov 16 '05 #1
4 1212
Francis <Fr*****@discussions.microsoft.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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.
"Jon Skeet [C# MVP]" wrote:
Francis <Fr*****@discussions.microsoft.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
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.
"Jon Skeet [C# MVP]" wrote:
Francis <Fr*****@discussions.microsoft.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4
Francis <Fr*****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached...
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
9
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
10
by: Martin Zenkel | last post by:
Assumed two assemblies (one C# and one C++), C++ refers to C#. The follwing code compiles and works well under VS 2002! VS 2003 C++ compiler reports the error "error 2555:...
6
by: John Salerno | last post by:
I understand how they work (basically), but I think maybe the examples I'm reading are too elementary to really show their value. Here's one from Programming C#: #region Using directives ...
18
by: Bradley | last post by:
I'm trying to determine if there's a general rule for when an Interface should used vs. an Abstract Class. Is there any design advantage to using one or the other? Brad
4
by: Martin | last post by:
Hi all! As a preparation of a conversion from VB6 to VB2005 I'm playing around with VB2005. One of the first things that strike me is the user-interface. In all documentation Microsoft keeps...
15
by: Gustaf | last post by:
Using VS 2005. I got an 'IpForm' class and an 'IpFormCollection' class, containing IpForm objects. To iterate through IpFrom objects with foreach, the class is implemented as such: public class...
1
by: Larry | last post by:
I checked definition of class CollectionBase public abstract class CollectionBase : IList, ICollection, IEnumerable, it implements 3 interface IList, ICollection and IEnumerable. I found...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.