473,385 Members | 1,309 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,385 software developers and data experts.

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.ChangeType(o, TypeCode.Double);
if (apt.IsNull && (o == null))
return true;
if ((apt.IsNull && (o != null)) || (!apt.IsNull && (o == null)))
return false;
if (apt.InnerObject.GetType() == o.GetType())
return apt.InnerObject == o;
else
{
if (apt.InnerObject is System.String)
{
try
{
//question: how can I:
//cast apt.InnerObject into o.GetType()
//and retrurn the operator result?
}
catch (FormatException fe)
{
return false;
}
catch (InvalidCastException ice)
{
return false;
}
}
else
{
try
{
//question: how can I:
//cast o.GetType() into apt.InnerObject;
//and retrurn the operator result?
}
catch (InvalidCastException ice)
{
return false;
}
catch (FormatException fe)
{
return false;
}
}
}
}
}
Jun 26 '06 #1
2 1594
Giulio Petrucci <gi*************@SUPERCALIFRAGILISTICHESPIRALIDOSO .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(object,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(object,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
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 "@...
10
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...
8
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...
19
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
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...
50
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
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
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...
3
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...
1
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...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.