.NET Framework - Class boxed in an object

Asked By Nio
21-Nov-09 03:49 AM
Hello, I have a class implementing comparison operators and cast operators
(CType) .
This class called MyOpTest

Now look at this code:

Dim MyOp As New MyOpTest
Dim data As String = MyOp                        'OK no error, MyOpTest
implements "Shared narrowing operator CType(byval a As MyOpTest) As String"
Dim obj As Object = New MyOpTest        'now instance is boxed into an
object type

Dim Result as Boolean = Cbool(obj < 2)    'OK no error MyOpTest implements
Dim data2 As String = obj                            'This failed!! casting
exception from MyOpTest!!  to String

So why when instance is boxed into object, casting operations are not called
and comparison operators are called ?

Is there any solution ? (a way to implicit unbox object type) ?
Thanks for your answers.
Otherwise
(1)
MyOpTest
(1)
Result
(1)
DirectCast
(1)
CType
(1)
MyOp
(1)
Cbool
(1)
Implements
(1)
  Armin Zingler replied to Nio
21-Nov-09 06:31 AM
Nio schrieb:

No, it does not work. Option Strict Off? Switch is it on, first. Otherwise
you can throw away all type awareness. it is a _narrowing_ operator, which means,
it could fail at runtime.


CBool is an explicit cast. The compiler trusts you.
Whereas "Dim data2 As String = obj" requires an implicit cast/conversion. Casting
from Object to String is not defined/allowed.


I do not think so.


--
Armin
  Armin Zingler replied to Armin Zingler
21-Nov-09 06:58 AM
Armin Zingler schrieb:

When I wrote the 2nd part of my answer, I was not aware of Option Strict Off, yet.
I misread it and assumed a compile time error but you mentioned an exception. Sorry,
my fault.

Well, with Option Strict On, you do not get an exception, so I think this changed
the situation. But, you can also not write

Dim data2 As String = CType(obj, String)	'failure

because it is the compiler that is responsible for making use of your own operator.
it is not used in this case because the expression type is Object, not MyOpTest.

So you would  have to write

Dim data2 As String = CType(DirectCast(obj, myoptest), String)



--
Armin
Create New Account
help
signature of the web service i have created: using WebServiceData; [SoapHeader("Consumer")] [WebMethod] public WebServiceData.Result PutData(WebServiceData.Result res) { } The proxy file generated by wsdl always generates the above function as public Result PutData(Result) And hence, after the proxy file is generated every time i need manually replace Result with WebServiceData.Result otherwise I am getting error while consuming the web service Here is the command I have successfuly without any error! I welcome any workaround Thank you Regards Raj C# Discussions WebServiceData.Result (1) SoapHeader (1) WebServiceData (1) Result (1) WebMethod (1) PutData (1) Mike (1) WSDL (1
public: Klasse operator+(Klasse Op); public: int X; }; Implementation: Klasse Klasse::operator+(Klasse Op) { Klasse Result; Result.X = this-> X + Op.X; return Result; }; I want to transfer it to C#, like this: class Klasse { public int X; public static Klasse operator +(Klasse Op) / / 'static' is obligatory! { MVector Result; Result = new Result(); Result.X = this.X + Op.X; / / <- but this doesn't work in 'static'! return Result; } } The C#-Code is not working, because: 1. operator-methods have to be declared as
the element name. Surely there is a better way. private string FixupJavascript(string htmlCode) { string result = FixupHTML(htmlCode); result = result.Replace(" \ ", " \ \ "); result = result.Replace(" \ "", " \ \ ""); Regex regex = new Regex("< \ s*script", RegexOptions.IgnoreCase); result = regex.Replace(result, "<scr \ " + \ "ipt"); regex = new Regex("< \ s* \ / script> ", RegexOptions.IgnoreCase); result = regex.Replace(result, "< / scr \ " + \ "ipt> "); result = result.Replace(Environment.NewLine, string.Empty).Replace(" \ t", string
I have a question about the infamous GOTO statement and the way to return a result from a sub: I have a sub that has to make some calls to external check them to be running ok, like this: sub method(byval param1 as integer, . . . . , BYREF result as String) dim . . . . . result = callCOM1(params) if result = -1 then 'bad result result = "callCOM1 failed, dude!" GOTO ENDPOINT . . result = callCOM2(params) if result = -1 then 'bad result result = "callCOM2 failed, dude!" GOTO ENDPOINT . . ENDPOINT: 'make some dispose, and