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

Overloading and Arrays

I have some custom types and have overloaded the tostring method so i can print it out and get a description of the contenet of the object for debugging purposes not just the default type
however when i have an array of the objects it just uses the type, so where do i overload the tostring for an array of objects

e

class myTyp

public string name

public override string ToString(

return "{" + base.ToString() + "} " + name

myType x = new myType()
x.name = "test"

myType[] y = new myType[10]

//... set name for each on

Console.Writeline(x.ToString())
Console.Writeline(y.ToString())

---
output
{myType} tes
myType[

how can i alter the 2nd tostring output?
Nov 16 '05 #1
3 1318
n!
Console.Writeline(x.ToString());
Console.Writeline(y.ToString());


You are calling 'ToString()' on the array itself rather than your class
'myType'.

myType x = new myType();
x.name = "test";

myType[] y = new myType[] { x };

Console.WriteLine( x.ToString() );
Console.WriteLine( y[ 0 ].ToString() );

Should give you the correct output.

n!
Nov 16 '05 #2
im calling it on the array as at runtime i dnt know if anything is an object or an array of objects so i want to overload the tostring on the array so it will print out the indevidual objects for me so i dnt have to check what it is
Nov 16 '05 #3
Hi,

You could inherit from Array and override the ToString() method,
unfortunally only the compiler can do that, so you cannot change the default
behavior of it

What you can do is use another object to store the collectoin of objects,
you can use for example ArrayList or a Strong typed collection , all you
have to do is override the ToString() method to whatever you need.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Spike" <an*******@discussions.microsoft.com> wrote in message
news:DF**********************************@microsof t.com...
im calling it on the array as at runtime i dnt know if anything is an

object or an array of objects so i want to overload the tostring on the
array so it will print out the indevidual objects for me so i dnt have to
check what it is
Nov 16 '05 #4

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

Similar topics

17
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
8
by: Sebastien Boisgerault | last post by:
I wonder if the following quotation from the Python Reference Manual (release 2.3.3) about operator overloading is true : "For example, if a class defines a method named __getitem__(), and x is...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
5
by: Ramon F Herrera | last post by:
I am looking for a good excuse to pick up C++. I have been a satisfied C programmer for years, a language that provides me all the facilities that I need, with ONE exception. I keep on finding...
3
by: abhighat4214 | last post by:
Hello All , I'm a little bit confused about function and constructor overloading in PHP 5.0 I know PHP does not support operator overloading. However if I have to realize the advantages of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.