473,770 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

very-dynamic casting :-)

Hi everybody,

here's my problem: I have to dymanically build (and compile, of course)
some code, from some ECMAScript function. ECMAScript variables I get are
not typezed, so I should have operators like

variable == a string or a double ore anything else

in C# I thought to build a classes that wraps an object (untypized) and
typize it inside it and to overload operators. In the code below there's an
wxample with some questions among comments. Anyway the core-question is: in
a scenario like:

Variable v;
Object o;
....
v == o

how can I detect the type of "o" and try to performe a casting (note:
casting and not a conversion) of my variable v? and viceversa?

Thanks in advance,
Kind regards,
Giulio

--

public class ECMAVar
{
private Double _d;
private String _s;
private Boolean _b;
private object _o;
private Type innerType;
private bool isNull;
public ECMAVar(object o)
{
if (o == null)
isNull = true;
else isNull = false;

if (o is String)
{
if ((String)o == "undefined" )
{
isNull = true;
_s = null;
_o = null;
}
else
{
_s = (String)o;
_o = _s;
}
/*
* handling dell'undefined;
*/
}
else
{
if ((o is Double)||(o is Int32))
{
_d = (Double)o;
_o = _d;
}
else
{
if (o is Boolean)
{
_b = (Boolean)o;
_o = _b;
}
else
{
/*
* eccezione;
*/
}
}
}
if (isNull == false)
innerType = _o.GetType();
else innerType = null;
}
public bool IsNull
{
get
{
return isNull;
}
}
public object InnerObject
{
get
{
return _o;
}
}

public static Boolean operator ==(ECMAVar apt, object o)
{
if (o is Int32)
o = Convert.ChangeT ype(o, TypeCode.Double );
if (apt.IsNull && (o == null))
return true;
if ((apt.IsNull && (o != null)) || (!apt.IsNull && (o == null)))
return false;
if (apt.InnerObjec t.GetType() == o.GetType())
return apt.InnerObject == o;
else
{
if (apt.InnerObjec t is System.String)
{
try
{
//question: how can I:
//cast apt.InnerObject into o.GetType()
//and retrurn the operator result?
}
catch (FormatExceptio n fe)
{
return false;
}
catch (InvalidCastExc eption ice)
{
return false;
}
}
else
{
try
{
//question: how can I:
//cast o.GetType() into apt.InnerObject ;
//and retrurn the operator result?
}
catch (InvalidCastExc eption ice)
{
return false;
}
catch (FormatExceptio n fe)
{
return false;
}
}
}
}
}
Jun 26 '06 #1
2 1608
Giulio Petrucci <gi************ *@SUPERCALIFRAG ILISTICHESPIRAL IDOSO.com>
wrote:
here's my problem: I have to dymanically build (and compile, of course)
some code, from some ECMAScript function. ECMAScript variables I get are
not typezed, so I should have operators like
Are you using JScript.NET? It is (roughly) a superset of EcmaScript.
variable == a string or a double ore anything else


Does object.Equals(o bject,object) do what you want?

E.g.:

v == "foo" ==> object.Equals(v , "foo")

-- Barry

--
http://barrkel.blogspot.com/
Jun 26 '06 #2
Barry Kelly ha scritto:
Does object.Equals(o bject,object) do what you want?


No, the problem is more complex.
If I have a string that's "3.2" it can be parsed as a double, so I can have:

Var v = new Var("3.2"); //Var is the wrapper class;
return (v==5.7)

or

return (v!="foo")

So I have to overload operators or one for strings, another for doubles...
and so on, or a single operator

public static Boolean operator(Var v, object o) { ... }

and put there the code to performe the operation. So I don't know if I'll
have to compare my Var object with a double or a string.

Thanks,
Giulio

--

Jun 26 '06 #3

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

Similar topics

4
1712
by: Raymond Arthur St. Marie II of III | last post by:
very Very VERY dumb ? about the new Set( ) 's Please be kind and read this like you know I've been up 33-34 hours reading PEP's but... Doc\ref 2.6 Delimiters show's three unused characters "@ $ ?". @ sort of looks like and sort of sounds like a set an $ well sort of obvious. I can imagine that the $ would be confused for money and @ is ugly.
10
2430
by: Eric S. Johansson | last post by:
I have an application where I need a very simple database, effectively a very large dictionary. The very large dictionary must be accessed from multiple processes simultaneously. I need to be able to lock records within the very large dictionary when records are written to. Estimated number of records will be in the ballpark of 50,000 to 100,000 in his early phase and 10 times that in the future. Each record will run about 100 to 150...
8
3182
by: alsemgeest | last post by:
Hi, I'd like to make my own very simple autorisation system. Just a table: tblUsers (user and pw). Next I need a form with user and pw in textbox, with an OK button. If the lookup fails: msgbox "Wrong, bye!" and docmd.quit If lookup succeeds: open the switchboard. Now I'm not very familiar with the lookup function, and this is where I
19
7276
by: shanx__=|;- | last post by:
hi i need some help regarding use of very very long integer datatype in 'c'.. i need it to store result of large number's factorial.. if someone can healp it would be a delight..
2
8275
by: Robert Hooker | last post by:
Hi, I'm curious to know if I'm doing something wrong here, or if this is just mind-numbingly slow for a reason. In a simple WindowsFormsApplication: public Form1() { // Required for Windows Form Designer support
50
5737
by: diffuser78 | last post by:
I have just started to learn python. Some said that its slow. Can somebody pin point the issue. Thans
33
3442
by: dembla | last post by:
Hey Frnds can anyone help me in this i need a program in 'c' PROGRAM to print NxN Matrix 9 1 8 1 2 3 2 7 3 as 4 5 6 6 4 5 7 8 9 in sorted form
5
1430
by: le007 | last post by:
:) Hey All, I've recently tried my hand with CSS. I know a good bit of html and thats more or less it. I've moved back to Ireland from Australia now and I'm trying to put a site together to keep in touch with my mates back home. I've got a fair few questions so hopefully some of you cats can help! At the bottom is a link to my website which is very much under construction. Please take a look at it and you'll understand what I need to...
3
7572
by: cold80 | last post by:
I was just doing a performance test in order to see the benefit of using AJAX instead of doing a postback on the server. So I have a simple page that performs a request to the server and write a counter on the page when it receives the response from the server. I'm not changing the page actually, so the browser don't have to modify the internal DOM and to render the changes. I tried this simple page with IE 7.0, Firefox 2.0 and Opera 9.10....
1
1460
by: 6655326 | last post by:
Newbi needs a very small help, thank you very much. Hello everyone and thank you very much for your time. I Have a small db for invoicing and on my form (with a subform) there is a CANCEL button which will just cancel what had been selected or entered in the form ... here is the code behind the cancel button...
0
9618
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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 we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.