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

Printing contents of an object in C#

I am new to OO programming and learning C#. I am trying to print out the
contents of an object, but am only seeing the reference to the object. The
accounts array contains Account objects, which consist of an Id, Owner and
Balance. My code is printing out

Account Name: ass1.Account

instead of

Account Name: John Smith

// List the accounts in the account array
public void ListAccounts()
{
for (int i=0; i < 100; i++){
Console.WriteLine("Account Name: {0}", accounts[i]);
}

How do I get the actual values from the object rather than the reference?

Alison
Jul 21 '05 #1
3 4053
Console.Writeline internally calls the ToString method of an object. The
default implementation simply returns the class name. You should override it
in your account class and return the name of the owner.

Niki

"Alison" <Al****@discussions.microsoft.com> wrote in
news:CF**********************************@microsof t.com...
I am new to OO programming and learning C#. I am trying to print out the
contents of an object, but am only seeing the reference to the object. The
accounts array contains Account objects, which consist of an Id, Owner and
Balance. My code is printing out

Account Name: ass1.Account

instead of

Account Name: John Smith

// List the accounts in the account array
public void ListAccounts()
{
for (int i=0; i < 100; i++){
Console.WriteLine("Account Name: {0}", accounts[i]);
}

How do I get the actual values from the object rather than the reference?

Alison

Jul 21 '05 #2
please post the code that puts the values in accounts[], so we can get a
clue on how to help you.

Alison wrote:
I am new to OO programming and learning C#. I am trying to print out the
contents of an object, but am only seeing the reference to the object. The
accounts array contains Account objects, which consist of an Id, Owner and
Balance. My code is printing out

Account Name: ass1.Account

instead of

Account Name: John Smith

// List the accounts in the account array
public void ListAccounts()
{
for (int i=0; i < 100; i++){
Console.WriteLine("Account Name: {0}", accounts[i]);
}

How do I get the actual values from the object rather than the reference?

Alison

Jul 21 '05 #3
I worked it out late last night. I needed to add get properties to the
Account object, and then access the object properties:

public int AccountId
{
get{return Id;}
}
public string AccountName
{
get{return Owner;}
}
public long AccountBalance
{
get{return Balance;}
}

Then access those properties:

public void ListAccounts(int AccountNumber)
{
for (int i=0; i < (AccountNumber -1); i++){
Console.WriteLine("ID: {0}", accounts[i].AccountId);
Console.WriteLine("Name: {0}", accounts[i].AccountName);
Console.WriteLine("Balance: {0}", accounts[i].AccountBalance);
}
}

Thanks for the help.

Alison

"Alison" wrote:
I am new to OO programming and learning C#. I am trying to print out the
contents of an object, but am only seeing the reference to the object. The
accounts array contains Account objects, which consist of an Id, Owner and
Balance. My code is printing out

Account Name: ass1.Account

instead of

Account Name: John Smith

// List the accounts in the account array
public void ListAccounts()
{
for (int i=0; i < 100; i++){
Console.WriteLine("Account Name: {0}", accounts[i]);
}

How do I get the actual values from the object rather than the reference?

Alison

Jul 21 '05 #4

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

Similar topics

9
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
5
by: Mark Preston | last post by:
Admission first - I don't actually have a problem here but have noticed that a lot of people have been asking similar questions and getting very varied answers. What I've done is to sort of...
9
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
9
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
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...

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.