0

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.

1
  • It would be useful if you could post some more code to give more context of the Method. Where is format declared, for example? Commented Feb 6, 2012 at 4:49

3 Answers 3

2

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?

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks all for the great help ,am an Idiot,haven't notice this piece in the code block public string messageFormat { get { return "{0}: {1}{2}"; } }
@Usher: properties should start with capitals, which would have made it less confusing.
1

Just call it like you'd call any method:

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

2 Comments

Thanks Andrew,its throwing exception at @format,it throws format doesnt exist in the current context.
You either need to add format as another parameter to PutMessage, or make it a member of the class.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.