English 中文(简体)
question about print List<T> to screen
原标题:

A question about List;

alt text When i have "List<PlugwiseMessage> msg" with the value s from the picture :

I only get PlugwiseLib.BLL.BC.PlugwiseMessage as output.

But how can i see the value s from _message, _owner and _type on my screen ? or the value s of Message, Owner, and Type?

And can somebody explain the difference to me ?

最佳回答

Your list has a collection of PlugwiseLib.BLL.BC.PlugwiseMessage objects. Message, Owner and Type are properties on the object. The _message, _owner and _type variables are the backing fields that are exposed by the properties.

When you are doing the console output, you are calling .ToString() on the PlugwiseMessage object. The default behavior of ToString() is to print the name of the object. If you want to display the properties you will need to add several lines

Console.WriteLine(msg[i].Message);
Console.WriteLine(msg[i].Owner);
Console.WriteLine(msg[i].Type);
问题回答

The problem is that you re printing the object itself and not the properties, so it uses the default ToString() method which returns the object s type s name.

There s one of two options. You can override the ToString() method in the class PluginwiseMessage to return a formatted string with the info you want or if you don t have access to that you can do the following:

foreach(PluginwiseMessage message in msg)
{
    Console.WriteLine("{0} {1} {2}", message.Message, message.Owner, message.Type);
    Console.Read();
}

You can easily rearrange the parameters being printed and add more text to the output, but that will simply output Message, Owner, and Type separated by spaces.

Your list contains PlugwiseMessage objects and you tell Console to write them down. To do that PlugwiseMessage instances have to converted into a string. ToString() is used to do that and the default implementation just dumps the name of the type. That s what you observe.

If it s possible, you should override the ToString method and adjust it to your needs. If that s not possible, you have to dump the values by yourself. That means you have to pass msg[i].Messasge, msg[i].Owner, ... to WriteLine().

overide ToString method in PlugWiseMessage type.

public override string ToString()
        {
            return String.Format("Owner {0}, Message {1}, Type {2}", this.Owner, this.Message, this.Type);
        }
msg[i].Message
msg[i].Owner
msg[i].Type




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签