0

I have been reading about verbatim string literals and escape sequences. What I am curious about now is if it is possible to escape and say call a dynamic source from a database when using verbatim string literals?

For example how would one achieve something like below?

string jsFunc = @"
    (function() 
        {
            var image = 'Images/" + {sqlConn[2]} + "';
            var img = document.getElementbyId('img1');
            img.src = image;
        }
     );";

Thanks

Jordan
  • 391
  • 4
  • 20
  • No I meant c# because the @" is c# not javascript – Jordan Jan 04 '17 at 14:27
  • 2
    I think you need to elaborate on what you're trying to do, and why. It's very unclear. –  Jan 04 '17 at 14:27
  • I want to be able to connect out of the verbatim somehow to a dynamic source from my databse that changes with each entry. – Jordan Jan 04 '17 at 14:28
  • I think I see what you're trying to do here, dynamically generate (on the server) some javascript function ready for render to the client presumably? –  Jan 04 '17 at 14:29
  • What about `string.Format` or string `interpolation` (using `$"{expression}"`)? Have you already tried either of these? – Igor Jan 04 '17 at 14:29
  • Repeating what you already said in the question isn't elaborating. You need to expand on what you've already said, not repeat it. –  Jan 04 '17 at 14:29
  • do you mean string interpolations? they start with `$` instead of `@`? they might be what you're actually looking for: http://stackoverflow.com/a/31014895/1132334 – Cee McSharpface Jan 04 '17 at 14:29
  • I attempted string interpolations however they error because of the enclosed javascript. – Jordan Jan 04 '17 at 14:30
  • Javascript has nothing to do with it, it is not parsed or executed. Any error is likely due to something else like a syntax error. – Igor Jan 04 '17 at 14:31
  • 1
    @Amy I don't know how it could be any clearer. I have image names in a db and I want them to populate in my javascript function. The way I see it I have to escape the verbatim to drop it in. Is this not true? Is there a better way to do it? That's what I'm asking. – Jordan Jan 04 '17 at 14:32
  • show us that code along with the compilation error. it will be a matter of escaping, and not due to the string content being JavaScript, C# is completely agnostic of what's in a string, except that the curly brackets will need duplicating. – Cee McSharpface Jan 04 '17 at 14:33
  • @Jordan - I appear to be the only one who understands here. Maybe you can make your question clearer by showing what you ultimately intend to do with `jsFunc`. –  Jan 04 '17 at 14:33

1 Answers1

7

You have 3 options:

string jsFunc = $@"
    (function() 
        {{
            var image = 'Images/{sqlConn[2]}';
            var img = document.getElementbyId('img1');
            img.src = image;
        }}
     );";

(Notice the $ before the @ and the removal of the string concatenation part, which turns this into an interpolated string. Also note that I had to double up the braces to avoid tripping up the underlying string.Format.)

Or this:

string jsFunc = string.Format(@"
    (function() 
        {{
            var image = 'Images/{0}';
            var img = document.getElementbyId('img1');
            img.src = image;
        }}
     );", sqlConn[2]);

Which is just good'ol string.Format. Same here about doubling up the braces.

Or this:

string jsFunc = @"
    (function() 
        {
            var image = 'Images/" + sqlConn[2] + @"';
            var img = document.getElementbyId('img1');
            img.src = image;
        }
     );";

(Notice that I added a new @ before the rest of the string after the concatenation, and also removed the braces.)

Graham
  • 7,431
  • 18
  • 59
  • 84
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • I tried 3 and it didn't respond the way I expected. I tried 1 but didn't double up the braces and that is exactly what I was looking for. Thank you very much. – Jordan Jan 04 '17 at 14:37