473,378 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Cast my custom class into System.String


How can I allow someone to cast my C# class into System.String? Is it
possible in C#? In C++ I used "operator" keyword to mark C++ member
function.
Apr 13 '06 #1
4 3857
John Smith wrote:
How can I allow someone to cast my C# class into System.String? Is it
possible in C#? In C++ I used "operator" keyword to mark C++ member
function.


You can create an explicit (or even implicit) conversion operator in
C#.

See http://msdn2.microsoft.com/en-us/lib...0a(VS.80).aspx for
examples.

Personally, I'd just override ToString and get people to call that
instead, but it's up to you.

Jon

Apr 13 '06 #2

Thank you very much for your response.

I'll ask you and others one more question which might be a little a bit off
the topic, but I have no other chice, since I have asked this on many
newsgroups and haven't received good answer. This is my case:

I'm writing webervice client using .Net 2.0. I have this class:

[System.Web.Services.WebServiceBindingAttribute(Nam e = "ValidateBinding",
Namespace = "http://example.org/Avtentikacija")]
public class MyWebService : SoapHttpClientProtocol
{
[System.Web.Services.Protocols.SoapDocumentMethodAt tribute("", Use =
System.Web.Services.Description.SoapBindingUse.Lit eral, ParameterStyle =
System.Web.Services.Protocols.SoapParameterStyle.B are)]
public XmlDocument validate(string url,
[System.Xml.Serialization.XmlElementAttribute(Names pace =
"http://www.example.org/PodpisaniDokument")] XmlDocument xmlDocument)
{
this.Url = url;
object[] results = null;
try
{
// ERROR occur at this line :(
results = this.Invoke("validate", new object[] { xmlDocument });
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return null;
}
return ((XmlDocument)(results[0]));
}
}

and I call it like this:

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = true;
xmlDocument.Load("C:\\request.xml");

MyWebService web = new MyWebService();
xmlDocument =
web.validate("http://localhost:8080/MyWeb/services/Avtentikacija",
xmlDocument);
if (xmlDocument != null)
xmlDocument.Save("C:\\response.xml");

And I got this error message:

System.InvalidOperationException: There was an error generating the XML
document. ---> System.InvalidCastException: Unable to cast object of type
'System.Xml.XmlDocument' to type 'System.String'.
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriterMyWebService.Write1_validate(Obj ect[]
p)
at
Microsoft.Xml.Serialization.GeneratedAssembly.Arra yOfObjectSerializer.Serialize(Object
objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String
id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String
id)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle)
at
System.Web.Services.Protocols.SoapHttpClientProtoc ol.Serialize(SoapClientMessage
message)
at System.Web.Services.Protocols.SoapHttpClientProtoc ol.Invoke(String
methodName, Object[] parameters)
at MyWeb.MyWebService.validate(String url, XmlDocument xmlDocument) in
C:\Documents and Settings\I\My Documents\Visual Studio
2005\Projects\MyWeb\MyWeb\Form1.cs:line 152

Why? :(

Interesting is that the same code (Invloke() method) works fine for the auto
generated class from WSDL file by Visual Studio 2005. I've searched for
"string" operator, but there isn't any. I have also displayed ToString()
return value and it returns something like
"SomeNamespace.SomeAutoGeneratedClass". I was expection to see whole XML. I
don't know how .Net code casts those auto generated classes???

This is peace of auto generated class:

[System.Xml.Serialization.XmlRoot("PodpisaniDokumen t",
Namespace="http://www.example.org/PodpisaniDokument")]
[System.CodeDom.Compiler.GeneratedCodeAttribute("Sy stem.Xml",
"2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://www.example.org/PodpisaniDokument")]
public partial class PodpisaniDokumentTip {

}
Apr 13 '06 #3

John Smith wrote:
Thank you very much for your response.

I'll ask you and others one more question which might be a little a bit off
the topic, but I have no other chice, since I have asked this on many
newsgroups and haven't received good answer. This is my case:
Generally creating a new topic for a new question is more advisable,
but anyway...
I'm writing webervice client using .Net 2.0. I have this class: public class MyWebService : SoapHttpClientProtocol
{
public XmlDocument validate(string url,
[System.Xml.Serialization.XmlElementAttribute(Names pace =
"http://www.example.org/PodpisaniDokument")] XmlDocument xmlDocument)
{
this.Url = url;
object[] results = null;
try
{
// ERROR occur at this line :(
results = this.Invoke("validate", new object[] { xmlDocument });


Don't you mean

results = this.Invoke("validate", new object[] { url, xmlDocument } );

?

--
Larry Lard
Replies to group please

Apr 13 '06 #4

Thank you very much for your response. :> You were very close.
Don't you mean

results = this.Invoke("validate", new object[] { url, xmlDocument } );


Although VS2005 auto generated classes doesn't invoke web service with 2
arguments, I have tried your example. The only problem is that it adds
<url></url> tags in my <soap:body> request and I don't want this, because
server can't parse it. Is there any way to remoce <url></url> tags?
Apr 13 '06 #5

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

Similar topics

13
by: Jack MacRank | last post by:
Hello, I'm coding a webform application in C# (ASP.NET 1.1 SP1 with VS.NET 2003 Pro on WinXP SP2 using IIS 5.1). I created a seperate "data" class to house all the MySQL connection and sql...
3
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
12
by: Jeremy Chapman | last post by:
I want to access the viewstate of a page object from outside of the class (in a helper class), so I made a class inherited from Page, and exposed a property that returns the viewstate object. I my...
6
by: Shimon Sim | last post by:
Hi I am working on application that need to hold custom user information - Last and first name, email, some other domain related information. I used to create Base class for all my pages. The base...
2
by: AMDRIT | last post by:
Hello everyone, I have created a custom component and one of its properties is a class object with it's own properties. During runtime, I can assign values to the class object properties just...
0
by: Pieter | last post by:
Hi, I'm using NHibernate 1.2 (CR1), and I'm using a custom list (inherited from BindingList(Of T) ) for all my lists. The NHibernate documentation told me that I had to implement...
1
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
Hello, Using VS2008 in a C# web service application, a class has been created that inherits from the ConfigurationSelection. This class file has been placed in the App_Code folder. The...
4
by: =?Utf-8?B?UmljaEI=?= | last post by:
I am trying to create a project using the ASP.NET AJAX accordion control. I would like to dynamically add panes to the control with a form template added when the pane is added. I have tried...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.