Solved

DirectCast very very difficoult question

Posted on 2012-03-31
12
318 Views
Last Modified: 2012-04-18
Hi ,

I need a particular request.

In my project there are more then 150/200 calls to DirectCast.

I see that in C# there is an equivalent, but use a very different sintax.

In short:

I like to create a function that emulate DirectCast with another name:


My_DirectCast that inside use direct cast. To explain better the concept, something like this:

//
Public Function My_DirectCast(Expression , Type) as Object

 return DirectCast(Expression , Type)

End Function
//

and IT is not easy do this, because the compiler do not accept Type.

But after this I need a way to emulate the some situation in C#

//
Object My_DirectCast(Expression , Type)) {

Return (Type)Exression;

}
//

and probably this is more difficoult then the previous.

I am aware that it is not easy but I hope is possible do this.

If is possible do this I don't need to replace MANUALLY 150/200 or more code rows but only one.

Please HELP ME !!!!!
0
Comment
Question by:luca345
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37790788
The following works - but really needs error trapping to in case the conversion fails.

       public T  foo<T>(Object obj)
        {
            return (T) Convert.ChangeType(obj, typeof(T));
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int i = foo<int>(23.45);
            double d = foo<double>(3.14159);
        }
0
 

Author Comment

by:luca345
ID: 37790812
Hi AndyAinscow ,

Because I have a bit difficoult to understand C# can you try to do the some in VB.net ?

Thank you !!
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37791022
I thought you wanted it in C# because you gave an example in VB but said C# uses different syntax
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37791390
VB isn't my speciality.
I've done a quick bit of searching and found this:

   Public Shared Function CAnyType(Of T)(ByRef UTO As Object) As T
        Return CType(UTO, T)
    End Function

I think you call it with something like
d = CAnyType(of double)(3.14159)
i = CAnyType(of integer)(2.345)

CType should be similar to your DirectCast, I think this might work or at least get you started
0
 

Author Comment

by:luca345
ID: 37791791
Hi Andy ,

Not work, I have try a similar code but VB don't accepts the 2° parameter because is a Type and VB want a object or class.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37794680
Works perfectly here - see picture
0002.jpg
0
 

Author Comment

by:luca345
ID: 37796704
Hi Andy ,

Thank you for your code but the the problem is more hard.

Please see attach.

I need a function that accept the some arguments of the original directcast and return the some value.
directr-cast.png
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37797249
I don't know how the developers that wrote the compiler do the work of transferring a type in the way it is done in DirectCast.

The way it usually is done is what I showed earlier and here again using DirectCast in the function.
How one can pass a generic Type into a function
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37797375
There are always two (rather silly) other possibilities

1)
Function CastToDouble(1.23) as Double   and so on for int, string....

2)
Function Cast(obj, string) as object  which has a big switch statement to call the appropriate DirectCast based upon the string value
0
 

Author Comment

by:luca345
ID: 37797565
Hi ,

I explain better what I need:

I my project there are 100/150 calls  to DirectCast.

In my compiler I need to go to 'replace' and 'all'    'DirectCast' with 'MyDirectCast' .

In your code I can't do 'replace all' because the arguments are different:

1) Is different the order.
2) You have add 'of'.


In short I need a clone of DirectCast with identical arguments::

//

a = DirectCast(b , c)

a = CAnyType(b , c )

//

In this mode, what I replace  'DirectCast' with ' CAnyType' I don't need to change others.

I don't known if this is possible or not.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 37797618
I don't think you can do it that simply.
0
 

Author Closing Comment

by:luca345
ID: 37860504
Very good !
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question