Solved

C# SharePoint related questions

Posted on 2013-10-24
4
493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 32

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 32

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
REST call Failing 1 41
Inserting LocalDB Table to SQL Server C# 3 51
VS C# how to design libary of functions 7 33
Adding Rows to Grid 4 34
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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