473,583 Members | 3,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(par ams object [] objArray) {
foreach (object o in objArray)
Console.WriteLi ne(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(i ntArray);
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(e mpArray);

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(par ams int [] intArray) {
// foreach (int i in intArray)
// Console.WriteLi ne(i);
// }

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

public static void Main() {

Tester t = new Tester();

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

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

Employee [] empArray = new Employee[3];
// populate the Array
for(int i=0; i<empArray.Leng th;i++)
empArray[i] = new Employee(i+5);
Console.WriteLi ne("Passing an Employee Array");
t.DisplayVals(e mpArray);
}
}

Nov 17 '05 #1
1 3109
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
6564
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 from a mousedown or keydown event belonging to an array of labels i get an error saying: "tpye mismatch, array or user defined type expected". ...
1
5432
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 methods which take arrays of primitives. If my bean method takes an array of primitves I get a "java.lang.IllegalArgumentException: argument type...
58
10093
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 code... TCHAR myArray; DoStuff(myArray);
1
6414
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 the array in fortran and then accessing it in my c++ code. Say in my c++ code I have; extern "C" { void foo_(float **, int &, int &); }
3
5550
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 an array-variable and pass this variable to a subroutine in which I make all stored objects in the array-variable invisible. Can somebody explain...
2
15117
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 the outer function, the string.format sets the string-value "System.Object" at the position of {0}. How can i get it work, so that all my values,...
2
4143
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 Dim ExistingTolerances() As Double 'oSurfBody is declared and assigned in the following Call oSurfBody.GetExistingFacetTolerances(ToleranceCount,...
11
8106
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 number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub...
9
3777
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 techniques are not feasible) The question is; Given that I have a Person object with a private set for id. What is the recommended approac in passing...
0
7826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8327
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7935
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5701
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5374
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3843
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.