Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# SharePoint related questions

Posted on 2013-10-24
4
Medium Priority
?
505 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 2000 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 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 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