?
Solved

Conversion from Object to Struct or Class in C#

Posted on 2011-05-12
5
Medium Priority
?
424 Views
Last Modified: 2012-05-11
I'm creating an extension method of various classes and structs (string, int, double, etc) called "Or", which basically would work like T-SQL's "IN" function.

Anyway, here are the three extensions:

    internal static class Extensions
    {
        internal static bool Or(this int i, int[] list)
        {
            bool retval = false;

            foreach (int val in list)
            {
                if (i == val)
                {
                    retval = true;
                    break;
                }
            }
            return retval;
        }

        internal static bool Or(this double d, double[] list)
        {
            bool retval = false;

            foreach (double val in list)
            {
                if (d == val)
                {
                    retval = true;
                    break;
                }
            }
            return retval;
        }

        internal static bool Or(this string s, string[] list)
        {
            bool retval = false;

            foreach (string val in list)
            {
                if (s == val)
                {
                    retval = true;
                    break;
                }
            }
            return retval;
        }

Open in new window


My question is, is there a way to do the following code that appears in all three methods so I only have to write it once?

            foreach (int val in list)
            {
                if (i == val)
                {
                    retval = true;
                    break;
                }
            }

Open in new window


Obviously, there'd have to be some way to pass these into some method as objects then determine what those objects are?

Hope this all makes sense. :)
0
Comment
Question by:infotechelg
  • 3
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 35748541
Make it a "generic" method:


internal static bool Or<T>(this T d, T[] list)
{
    bool retval = false;

    foreach (T val in list)
    {
        if (d.Equals(val))
        {
            retval = true;
            break;
        }
    }
    return retval;
}

Open in new window


Then you can call it like this:

string val = "hello";
string[] values = { "hello world!", "goodbye world!" };
Or<string>(val, values);

Open in new window


OR

double val = 4;
double[] values = { 1, 3, 5, 7, 9 };
Or<double>(val, values);

Open in new window

0
 

Author Closing Comment

by:infotechelg
ID: 35748596
Duh :) Thanks!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35748648
NP. Glad to help  = )

I screwed up the calling examples, in case you didn't figure it out. For the benefit of future readers, the way to call the method would be like this:
double val = 4;
double[] values = { 3, 3, 5, 6, };

bool found = val.Or<double>(values);

--OR--

string val = "hello";
string[] values = { "hello world!", "goodbye world!" };
bool found = val.Or<string>(values);

Open in new window

0
 
LVL 4

Expert Comment

by:safiint
ID: 35748674
hi

you can do like that:

internal static bool Or(this object i, ArrayList list)
        {
            bool retval = false;
            foreach (object val in list)
            {
                if (i.Equals( val))
                {
                    retval = true;
                    break;
                }
            }
            
            return retval;
        }

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35748737
@safiint

Why would you want to have all that boxing/unboxing going on??
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

831 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