Solved

C# SharePoint related questions

Posted on 2013-10-24
4
479 Views
Last Modified: 2013-10-24
My C# understanding of the subtleties is still not very complete, and I have the following questions

Here is my code

public class BasicWebPart : WebPart
    {
        protected override void CreateChildControls()
        {
            var literal = new LiteralControl(@"<p>Some Text Here</p>");
            Controls.Add(literal);
        }
    }

This works. But this also works

var literal = new LiteralControl();
literal.Text = "<P>Some Text Here</p>";
Controls.Add(literal);

Is the first the right way to do it simply because it can be done in one line of code? Any other reasons for doing it the first way instead of the second way?

And what is the significance of the @ ?. I have read that it is used to stop escapes, but why does this need to stop escapes for <p></p>? This is a webpart and it will be viewed in the browser. Does that have something to do with it? If so, why didn't I need it when in literal.Text?


I have a few other little C# questions, but I wanted to split them out. Thanks.
0
Comment
Question by:BobHavertyComh
  • 2
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
Jamie McAllister MVP earned 500 total points
ID: 39596764
Hi there. The two lines of code versus one doesn't really matter.

In the first example you're able to pass the text into the constructor when you create the class instance, in the second you're using the version of the constructor that doesn't need an argument passing in, and setting the property later.

Clarity tends to be important. If other developers can see what you were trying to achieve easily that's often the best way for something like this. If I were setting several properties on the literal I would use your second example, if only the text as above personal preference, I'd still use the second example. :)

The @ tells the compiler to interpret the string as a literal text. Why not needed when setting the property? Good question - I'm guessing the property is implemented to handle it, but the constructor isn't. Reflector would have the answer for that one. :)
0
 
LVL 9

Author Comment

by:BobHavertyComh
ID: 39596872
Okay, your answer makes sense. But why is the @ to handle escapes being used with <P> tags? I could understand \ or other reserved characters, but not <p> It is a web part and it will be viewed in the browser, so does that make the difference? It is possible that the instructor just does this out of habit or good coding practices, but I think it is important for me to know this one way or the other and it is a vid course so I cannot ask him.  Aside from the fact that they are html tag symbols, are the < and > characters reserved for strictly C# reasons as well such as taking it as greater or less signs? I thought it shouldn't do that within a string. So I still don't have a really thorough understanding of that and i think that I need to

I'll have to look into the Text property and it's methods to see if it somehow automatically takes handles escapes. It sounds like a logical explanation and maybe the only possible logical one. I certainly understand your thought process.
0
 
LVL 31

Assisted Solution

by:Jamie McAllister MVP
Jamie McAllister MVP earned 500 total points
ID: 39596898
I suspect adding the @ is a best practice, but is often not actually needed. You may have backslash characters in there or not. If the string changes to include one the @ is there to handle it.

Looking at MS examples they don't include it even in code samples very similar to yours;

http://msdn.microsoft.com/en-us/library/aa334477(v=vs.71).aspx
0
 
LVL 9

Author Closing Comment

by:BobHavertyComh
ID: 39597213
Thanks again Jaime.  Just needed a second opinion.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question