Usher
Usher

Reputation: 2146

Message Format using C#

I was trying to convert a sample win form app to console application.I just stuck when am trying to convert to message format.

Here is the below original code from winform

private void PutMessage(StringBuilder message, string mediaType, string filename)
{

    message.AppendFormat(messageFormat, "FileSize", videoInterrogator.GetFileSize(), Environment.NewLine);
    message.AppendFormat(messageFormat, "Duration", videoInterrogator.GetDuration(), Environment.NewLine);

}

Am trying to do the same in my console application

Am calling the method from FTPDownload method so the code look like

PutMessage(file, message);


private void PutMessage(string filename, StringBuilder message)
{
    VideoInterrogator videoInterrogator = new VideoInterrogator();
    videoInterrogator.LoadFile(filename);
    message.AppendFormat(format, "FileSize", videoInterrogator.GetFileSize(), Environment.NewLine);
    message.AppendFormat(format, "Duration", videoInterrogator.GetDuration(), Environment.NewLine);

}

Any help please how can i call this method pass the file name and return the values.It throws exception at "Format" am not sure what am missing here.

Upvotes: 0

Views: 2008

Answers (3)

siride
siride

Reputation: 209445

You're missing a variable format. It must have been a field in your WinForms code. Either add it to the method as a local variable, or a field to the class containing PutMessage.

EDIT: I think I'm just not sure what the actual problem is. Is it the missing variable or does the format string have more or less than 3 curly-brace arguments?

Upvotes: 2

Mark Hall
Mark Hall

Reputation: 54532

Judging by the signature of the AppendFormat method you are using, you are missing the string that is used for the formating. according to above MSDN link:

This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in the current StringBuilder object.

The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to arg0 through arg3, the objects in the parameter list of this method. The formatting process replaces each format item with the string representation of the corresponding object. The syntax of a format item is as follows:

{index[,length][:formatString]}

Elements in square brackets are optional.

Upvotes: 1

Andrew Cooper
Andrew Cooper

Reputation: 32576

Just call it like you'd call any method:

var message = new StringBuilder();
var filename = "file.xyz";
PutMessage(filename, message);
Console.WriteLine(message);

Upvotes: 1

Related Questions