1

The title sounds weird, but I am having in putting my question in proper english. What I want to do it something like

String question = "";

String message = "This message has the following question:\n" + question;

...//later on the code

question = question.path.readingFromAFile(FileAddress);

So, I am building a message that will be sent by mail, and I need to retrieve a piece of text that will be on a html file and insert that on the message.

The catch is: at the point where I need to build the message, the file doesn't properly exist yet, and since it doesn't exist I cannot find it (obviously). What I had in mind was to put a "empty" variable inside the message where I want the text to be and then when the file is created, I would update the variable value, and have it sent to the string that was created before. However I am not sure if that is possible, I thought about something like a placeholder or something of the sorts, but I am not sure how that would work. Any help would be much appreciated, I also apologize for any misunderstanding beforehand, I am having trouble in properly explaining that problem in english, if anything is unclear, please let me know.

  • You can use a while loop with a true condition and break it when the read returns the value you want, or use a .net timer and set an interval, break it when the read is complete – Ferus7 Apr 27 '17 at 14:43
  • You can either use a template, or simply create the full question later. – msitt Apr 27 '17 at 14:43

3 Answers3

0

Sounds like you need string templating.

A simular stackoverflow question. What's a good way of doing string templating in .NET?

There are libraries out there that can help you like SmartFormat.NET and StringTemplate

Community
  • 1
  • 1
AndyC
  • 1,325
  • 1
  • 13
  • 23
0

Disclaimer
Since this is presumably about inserting a single string, I can advise a simple solution. However; should your scope expand considerably (e.g. many template fields to fill in, or excessivly large text chunks), I suggest you look at a more robust fix.

string myEmptyTemplate = "This message has the following question:\n{0}";

//Much later...

string theQuestion = GetTheQuestion();

string myFilledTemplate = String.Format(myEmptyTemplate, question);

People often only use the format parameter of String.Format() as a string literal, but you could just as well make a format string much earlier, and only call String.Format() once you need the template to be filled in, using the format string that you composed ahead of time..

Flater
  • 12,908
  • 4
  • 39
  • 62
0

All other answers deals with templating. But it sounds to me you probably want a publisher-subscribe pattern.

The setup: You'll need a class MyClass to have an event broadcaster. Then whenever something happens, you'll invoke this. But your invoker doesn't care who's watching.

public class MyClass {

    public virtual event EventHandler BroadcastHandler;

    public virtual void BroadcastEvent(string question) {
        var broadcastHandler = BroadcastHandler;
        var eventArg = new ObservableEvent(question);

        if(broadcastHandler != null)
            broadcastHandler(this, eventArg);
    }

    public void UploadFile() {
        // Some logic... etc...

        // Invoke the event
        this.BroadcastEvent(question.path.readingFromAFile(FileAddress))
    }
}

Then you'll need an Observer class to watch for this specific event:

public class Observer {
    private readonly object syncRoot = new object();

    public string Question { get; private set; }

    // Basic constructor.
    public Observer() {}

    public void HandleEvent(object sender, EventArgs args) {
        lock(this.syncRoot) {
            var theEvent = (ObservableEvent)eventArgs;
            this.Question = theEvent.Question;
        }
    }

}

public class ObservableEvent : EventArgs {
    public string Question {get; private set;}

    public ObservableEvent(string question) {
        this.Question = question;
    }
}

Telling the observer to subscribe to the event:

var observer = new Observer();
var myClass = new MyClass();
myClass.BroadcastHandler += observer.HandleEvent;

Now just use string templating to use your question:

var message = $"This message has the following question:\n{observer.Question}"
Kyle
  • 5,407
  • 6
  • 32
  • 47