Solved

C# SharePoint related questions

Posted on 2013-10-24
4
447 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Summary In SharePoint 2010 it is easy to create custom color themes to jazz up a site. Theme colors can also be created in PowerPoint 2010 with a few clicks. But how do the chosen colors actually look in the SharePoint site? The attached PowerPoint…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now