473,324 Members | 2,179 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,324 software developers and data experts.

passing integer array as object array

Hi all,
I am a new programmer to C# and i am having a following problem.
I want to make a single method which takes a variable length array and
display it contents.
i have defined the method as:

public void DisplayVals(params object [] objArray) {
foreach (object o in objArray)
Console.WriteLine(o.ToString());
}

I am sending it three types of array, one by one:
1. an integer list directly as parameter to the method.
eg. t.DisplayVals(2,4,6,8);
2. an integer array explicitly defined and then sent.
eg. int [] intArray = {1,2,3,4,5,6};
t.DisplayVals(intArray);
3. an user defined type, Employee which has overriden ToString()
method.
eg. Employee [] empArray = new Employee[3];
then created the employee objects with for loop and,
t.DisplayVals(empArray);

In the first and third case the values are shown properly but in the
second case the output is: System.Int32[].
What can i do in the method DisplayVals or with the explicit array
intArray so that the conents of intArray can be displayed. I dont want
to overload DisplayVals method (one for the integers and one for the
user defined types).

The complete code is:

using System;
// A simple class to store in the array
public class Employee {
private int empID;

public Employee (int empID) {
this.empID = empID;
}

public override string ToString() {
return empID.ToString();
}
}

public class Tester {

// // method to take variable number of int parameters and dispaly
them
// public void DisplayVals(params int [] intArray) {
// foreach (int i in intArray)
// Console.WriteLine(i);
// }

// method to take variable number of object parameters and display
them
public void DisplayVals(params object [] objArray) {
foreach (object o in objArray)
Console.WriteLine(o.ToString());
}

public static void Main() {

Tester t = new Tester();

Console.WriteLine("Showing the variable number of integers");
t.DisplayVals(2,3,4,5);

Console.WriteLine("Passing an explicit array");
int [] intArray = {1,2,3,4,5,6};
t.DisplayVals(intArray);

Employee [] empArray = new Employee[3];
// populate the Array
for(int i=0; i<empArray.Length;i++)
empArray[i] = new Employee(i+5);
Console.WriteLine("Passing an Employee Array");
t.DisplayVals(empArray);
}
}

Nov 17 '05 #1
1 3070
The language spec says:

"Array covariance specifically does not extend to arrays of value-types. For
example, no conversion exists that permits an int[] to be treated as an
object[]."

When you pass an int[], it gets loaded into an object[] of length 1 and
passed to your method.

I do not think there is an easy solution - you might be able to do what you
want with reflection.
Nov 17 '05 #2

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

Similar topics

3
by: Kevin | last post by:
Hi i'm having a problem passing an arrays of controls on a form to a function or sub. I can use this code for any datatype but when i change 'long' to 'label' in the public sub and i call the sub...
1
by: PerfectDayToChaseTornados | last post by:
Hi All, I am trying to create a BeanHelper class which will set a beans values from the request much the same as it would be form a jsp using 'usebean'. I can get everything to work except for...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
1
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
3
by: SV | last post by:
Dear all, In my application I have a lot of hidden fields. I want to make them invisible for the users though for debugging reasons I want to make them visible. So I want to add these objects to...
2
by: Tobias Olbort | last post by:
Hello, i've a outer function, which takes a params-array as a parameter. I want to pass this array to inner function with a params-array (e. g. string.format). When i've passed an integer to...
2
by: Neil Munro | last post by:
I'm having some "type" difficulty in passing a .NET array (byref) to a COM based API (Autodesk Inventor). In VB6 the code to return an array of tolerance values is: Dim ToleranceCount As Long...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
9
by: Greger | last post by:
Hi, I am building an architecture that passes my custom objects to and from webservices. (Our internal architecture requires me to use webservices to any suggestion to use other remoting...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.