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

How to print out contents of a structure in a linked list in c#?

2
I'm trying to print out the contents of a linked list of structures but am not able to access the attributes of the structure by using the dot operator. I don't know what's the problem. Can someone please help me out? Thank you!

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class route
  3.     {
  4.         public struct Bus_stop
  5.         {
  6.             // Fields
  7.  
  8.             private int stop_num;
  9.             private double cost;
  10.  
  11.             // Property
  12.  
  13.             public Bus_stop(int stop_num, double cost)
  14.             {
  15.                 this.stop_num = stop_num;
  16.                 this.cost = cost;
  17.             }
  18.         }
  19.  
  20.  
  21.          public static void stop_node(int stop_no, double fare) //add new bus stop with stop num n cost from prev stop
  22.          {
  23.              LinkedList<Bus_stop> bus_route = new LinkedList<Bus_stop>();
  24.              Bus_stop bs1 = new Bus_stop(stop_no, fare);
  25.  
  26.              bus_route.AddLast(bs1);
  27.              Console.Writeline(bs1);
  28.          }
  29.  
  30.  
Mar 12 '11 #1
2 4835
GaryTexmo
1,501 Expert 1GB
There's a few ways you can go about this. The first way is to make your fields publicly accessible. Right now you have them as private.

If it's intentional and desired for those fields to be private, you can make public properties for those fields so that you can control the access level. Here is an example of a struct with a property.

Expand|Select|Wrap|Line Numbers
  1. public class Person
  2. {
  3.   private string m_name;
  4.  
  5.   public string Name
  6.   {
  7.     get { return m_name; }
  8.     set { m_name = value; }
  9.   }
  10.   // The rest of your class follows...
  11. }
NOTE: You can include only the access that you want. If it's intended to be read only, don't include a set on the property definition. Also note that you can use properties on classes as well as structs.

The final way you can go about this, and something you might want to do anyway, is to override the ToString method on your struct. The ToString method is a part of the object class, something that every class in C# inherits from. Simply put the following code in your class...

Expand|Select|Wrap|Line Numbers
  1. public override ToString()
  2. {
  3.   return "string value";
  4. }
The return value can be anything you like... so you could return the values of the variables. Then you can output the bus stop using Console.WriteLine, or directly call the ToString method to get the string value of it.

I hope that makes sense, let me know if you have any questions :)
Mar 13 '11 #2
iceice
2
Hi

I have an error when I add in the code you gave for the public override ToString because it asks for a return type. But from what I understand, there is no specific return type for this, is that correct?
Mar 14 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
10
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
6
by: Fazana | last post by:
I was doing one of the question but my program was not working properly. Can anyone help me with it pliz........ Here is the question for the program Question. Part 1. Add the function...
22
by: joshc | last post by:
In an interview for an embedded software position recently I was asked to write code, in C, for printing the contents of a linked list backwards. After a few minutes I came up with the recursive...
9
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node *...
1
by: dabbakal | last post by:
Hello, am a new member and this is my first posting. C++ is the first progrsaming language am taking and is just for 3 months. Having benefited from lot of posting i decided to join. But currently...
6
by: mattmao | last post by:
Okay, this is just my exercise in order to prepare for the coming assignment regarding the damned Linked List issue... The task is simple and I am about to finish it. However, I couldn't go around...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
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?
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
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
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...
0
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...

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.