Solved

C# SharePoint related questions

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Pimping Sharepoint 2007 without Server-Side Code Part 1 One of my biggest frustrations with Sharepoint 2007 in the corporate world is that while good-intentioned managers lock down the more interesting capabilities of Sharepoint programming in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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