Click here to Skip to main content
15,907,001 members
Home / Discussions / C#
   

C#

 
GeneralRe: problem converting to object Pin
Guffa14-May-08 9:35
Guffa14-May-08 9:35 
AnswerRe: problem converting to object Pin
Guffa14-May-08 9:40
Guffa14-May-08 9:40 
GeneralRe: problem converting to object Pin
humayunlalzad14-May-08 19:57
humayunlalzad14-May-08 19:57 
GeneralRe: problem converting to object Pin
Guffa15-May-08 0:55
Guffa15-May-08 0:55 
GeneralRe: problem converting to object Pin
humayunlalzad15-May-08 5:09
humayunlalzad15-May-08 5:09 
GeneralRe: problem converting to object Pin
Guffa15-May-08 6:53
Guffa15-May-08 6:53 
GeneralRe: problem converting to object Pin
humayunlalzad15-May-08 7:33
humayunlalzad15-May-08 7:33 
AnswerRe: problem converting to object [modified] Pin
Peter Josefsson Sweden14-May-08 12:01
Peter Josefsson Sweden14-May-08 12:01 
Like others have said, you can't cast an array of something into an array of something else. Ever. You have to loop, one way or the other. This is part of the philosophical heritage of C# - stuff that's very expensive to execute shouldn't be so simple to write that it routinely fools people into thinking it's inexpensive.

However, the .NET Framwork doesn't necessarily play by those rules, so a look in the class library can sometimes help you avoid writing the loop yourself. Look at System.Array.Copy and System.Array.ConstrainedCopy, for instance (or ConvertAll).

In your particular example I would suggest a generic version of Display(), though, as in:

// new generic version:
public static void Display<T>(T[] arr)
{
    foreach (T e in arr)
    {
        Console.WriteLine("{0} ", e.ToString());
    }
    Console.WriteLine();
}

// reimplementation of object array version, using generic version:
public static void Display(object[] oarr)
{
    Display<object>(oarr);
}


Provided that you rename the array called "i" (name clashes with the for loop counter, at least in VS2005) you can remove the comments and it will work like a charm. Note that you DON'T need to specify the type (as in Display<int>(intArray);), as it is inferred by the compiler.

For added value, you can also insert the keyword "params" before the parameter declarations, as in:

public static void Display<T>(params T[] arr) //...
public static void Display(params object[] oarr) //...


That way, you can invoke them like this:

Display(1, "hello", 2, "word"); // object version
Display<int>(1, 2, 3, 4); // generic version for ints


If you do anything more complex than ToString() (or something else that's inhereted from System.Object) on the elements, you will probably have to constrain T (using the where keyword) to make sure that it implements some interface or other.

I still think generics are sexy... wonder how long that will last...

Later,

--
Peter

modified on Wednesday, May 14, 2008 6:19 PM

GeneralRe: problem converting to object Pin
humayunlalzad14-May-08 19:48
humayunlalzad14-May-08 19:48 
Questioninserting an image into a database Pin
Ofori Boadu14-May-08 7:22
Ofori Boadu14-May-08 7:22 
AnswerRe: inserting an image into a database Pin
Giorgi Dalakishvili14-May-08 7:42
mentorGiorgi Dalakishvili14-May-08 7:42 
Questiondsp problems Pin
t.wegrewicz14-May-08 6:52
t.wegrewicz14-May-08 6:52 
Questionerror saving arabic chars into my sql server 05 db Pin
Boshkash14-May-08 5:21
Boshkash14-May-08 5:21 
AnswerIGNORE Pin
leckey14-May-08 8:18
leckey14-May-08 8:18 
QuestionHow to create the Setup file in C# Console Application in .Net Pin
bruze14-May-08 4:24
bruze14-May-08 4:24 
AnswerRe: How to create the Setup file in C# Console Application in .Net Pin
led mike14-May-08 4:29
led mike14-May-08 4:29 
GeneralRe: How to create the Setup file in C# Console Application in .Net Pin
parth.p14-May-08 5:48
parth.p14-May-08 5:48 
GeneralRe: How to create the Setup file in C# Console Application in .Net Pin
bruze14-May-08 6:38
bruze14-May-08 6:38 
GeneralRe: How to create the Setup file in C# Console Application in .Net Pin
parth.p14-May-08 7:23
parth.p14-May-08 7:23 
GeneralRe: How to create the Setup file in C# Console Application in .Net Pin
bruze14-May-08 20:31
bruze14-May-08 20:31 
QuestionGlobal dictionary & partial class scheme - error: cannot be accessed with an instance reference Pin
RNEELY14-May-08 4:09
RNEELY14-May-08 4:09 
AnswerRe: Global dictionary & partial class scheme - error: cannot be accessed with an instance reference Pin
carbon_golem14-May-08 4:24
carbon_golem14-May-08 4:24 
QuestionAny bug in my Code? Pin
ASysSolvers14-May-08 3:27
ASysSolvers14-May-08 3:27 
AnswerRe: Any bug in my Code? Pin
#realJSOP14-May-08 4:11
professional#realJSOP14-May-08 4:11 
Questionhow to use message box in windows service project. Pin
prasadbuddhika14-May-08 3:09
prasadbuddhika14-May-08 3:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.