code_junkie

Do you have any recommended code-snippets for Microsoft Visual Studio? [closed]

Nov 10th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.87 KB | None | 0 0
  1. if -> Just hit tab twice and you get
  2.  
  3. if(true)
  4. {
  5.  
  6. }
  7.  
  8. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  9.  
  10. <CodeSnippet Format="1.0.0">
  11. <Header>
  12. <Title>Generic Property - View State</Title>
  13. <Description>Inserts a Generic Property</Description>
  14. <Author>David Basarab</Author>
  15. <Shortcut>property_ViewState</Shortcut>
  16. <SnippetTypes>
  17. <SnippetType>Expansion</SnippetType>
  18. </SnippetTypes>
  19. </Header>
  20. <Snippet>
  21. <Declarations>
  22. <Literal Editable="true">
  23. <ID>PropertyName</ID>
  24. <Default>PropertyName</Default>
  25. <ToolTip>Replace this with the name of the Property.</ToolTip>
  26. </Literal>
  27. <Literal Editable="true">
  28. <ID>Type</ID>
  29. <Default>string</Default>
  30. <ToolTip>Replace this with the type of the Property.</ToolTip>
  31. </Literal>
  32. <Literal Editable="true">
  33. <ID>DefaultValue</ID>
  34. <Default>string.Empty</Default>
  35. <ToolTip>Replace this with the default value of the Property.</ToolTip>
  36. </Literal>
  37. </Declarations>
  38. <Code Language="CSharp">
  39. <![CDATA[#region $PropertyName$
  40. public $Type$ $PropertyName$
  41. {
  42. get
  43. {
  44. if (this.ViewState["$PropertyName$"] == null)
  45. return $DefaultValue$;
  46.  
  47. return ($Type$)this.ViewState["$PropertyName$"];
  48. }
  49. set { this.ViewState["$PropertyName$"] = value; }
  50. }
  51. #endregion]]>
  52. </Code>
  53. </Snippet>
  54. </CodeSnippet>
  55.  
  56. </CodeSnippets>
  57.  
  58. <?xml version="1.0" encoding="utf-8" ?>
  59. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  60. <CodeSnippet Format="1.0.0">
  61. <Header>
  62. <Title>prop_databound</Title>
  63. <Shortcut>prop_databound</Shortcut>
  64. <Description>Code snippet for property, backing field, and databinding event</Description>
  65. <Author>ee from www.devfuel.com</Author>
  66. <SnippetTypes>
  67. <SnippetType>Expansion</SnippetType>
  68. </SnippetTypes>
  69. </Header>
  70. <Snippet>
  71. <Declarations>
  72. <Literal>
  73. <ID>type</ID>
  74. <ToolTip>Property type</ToolTip>
  75. <Default>int</Default>
  76. </Literal>
  77. <Literal>
  78. <ID>field</ID>
  79. <ToolTip>The variable backing this property</ToolTip>
  80. <Default>myVar</Default>
  81. </Literal>
  82. </Declarations>
  83. <Code Language="csharp"><![CDATA[private $type$ m_$field$;
  84. public $type$ $field$
  85. {
  86. get { return m_$field$;}
  87. set
  88. {
  89. if (value != m_$field$)
  90. {
  91. m_$field$ = value;
  92. On$field$Changed(EventArgs.Empty);
  93. }
  94. }
  95. }
  96. public event EventHandler $field$Changed;
  97. protected virtual void On$field$Changed(EventArgs e)
  98.  
  99.  
  100.  
  101.  
  102. link|improve this answer
  103.  
  104.  
  105. edited Jan 25 '09 at 18:39
  106.  
  107.  
  108.  
  109.  
  110.  
  111. community wikidevfuel
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127. You can use ?? in the lazy get
  128.  
  129. Right Click -> Insert Code Snippet
  130. Select -> Common Code Patterns
  131. Select -> Conditionals And Loops
  132. Select -> If .. End If Statement
  133.  
  134. <?xml version="1.0"?>
  135. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  136. <CodeSnippet Format="1.0.0">
  137. <Header>
  138. <Title>ViewState property</Title>
  139. <Author>SB</Author>
  140. <Description>Add data in the viewstate simply. </Description>
  141. <Shortcut>vprop</Shortcut>
  142. </Header>
  143. <Snippet>
  144. <Declarations>
  145. <Literal>
  146. <ID>PropertyType</ID>
  147. <ToolTip>Type of the object</ToolTip>
  148. <Default>Integer</Default>
  149. </Literal>
  150. <Literal>
  151. <ID>PropertyName</ID>
  152. <Type>String</Type>
  153. <ToolTip>Name of property</ToolTip>
  154. <Default>ViewStateProperty</Default>
  155. </Literal>
  156. </Declarations>
  157. <Code Language="VB"><![CDATA[Private $PropertyName$Default as $PropertyType$ '=
  158. Public Property $PropertyName$() as $PropertyType$
  159. Get
  160. Dim obj as Object = ViewState("$PropertyName$")
  161. if (obj isnot Nothing)
  162. return DirectCast(obj, $PropertyType$)
  163. end if
  164. return $PropertyName$Default
  165. End Get
  166. Set(ByVal value As $PropertyType$)
  167. ViewState("$PropertyName$") = value
  168. End Set
  169. End Property
  170. ]]></Code>
  171. </Snippet>
  172. </CodeSnippet>
  173. </CodeSnippets>
  174.  
  175. ///<summary>Releases unmanaged resources and performs other cleanup operations before the ClassName is reclaimed by garbage collection.</summary>
  176. ~ClassName() { Dispose(false); }
  177. ///<summary>Releases all resources used by the ClassName.</summary>
  178. public void Dispose() { Dispose(true); GC.SuppressFinalize(this); }
  179. ///<summary>Releases the unmanaged resources used by the ClassName and optionally releases the managed resources.</summary>
  180. ///<param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
  181. protected virtual void Dispose(bool disposing) {
  182. if (disposing) {
  183.  
  184. }
  185. }
  186.  
  187. ///<summary>Occurs when ....</summary>
  188. public event EventHandler<MyEventArgs> MyEvent;
  189. ///<summary>Raises the MyEvent event.</summary>
  190. ///<param name="e">A MyEventArgs object that provides the event data.</param>
  191. internal protected virtual void OnMyEvent(MyEventArgs e) {
  192. if (MyEvent != null)
  193. MyEvent(this, e);
  194. }
  195.  
  196. <?xml version="1.0" encoding="utf-8" ?>
  197. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  198. <CodeSnippet Format="1.0.0">
  199. <Header>
  200. <Title>Dispose pattern</Title>
  201. <Shortcut>dispose</Shortcut>
  202. <Description>Code snippet for virtual dispose pattern</Description>
  203. <Author>SLaks</Author>
  204. <SnippetTypes>
  205. <SnippetType>Expansion</SnippetType>
  206. <SnippetType>SurroundsWith</SnippetType>
  207. </SnippetTypes>
  208. </Header>
  209. <Snippet>
  210. <Declarations>
  211. <Literal Editable="false">
  212. <ID>classname</ID>
  213. <ToolTip>Class name</ToolTip>
  214. <Default>ClassNamePlaceholder</Default>
  215. <Function>ClassName()</Function>
  216. </Literal>
  217. </Declarations>
  218. <Code Language="csharp">
  219. <![CDATA[
  220. ///<summary>Releases unmanaged resources and performs other cleanup operations before the $classname$ is reclaimed by garbage collection.</summary>
  221. ~$classname$() { Dispose(false); }
  222. ///<summary>Releases all resources used by the $classname$.</summary>
  223. public void Dispose() { Dispose(true); GC.SuppressFinalize(this); }
  224. ///<summary>Releases the unmanaged resources used by the $classname$ and optionally releases the managed resources.</summary>
  225. ///<param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
  226. protected virtual void Dispose(bool disposing) {
  227. if (disposing) {
  228. $end$$selected$
  229. }
  230. }]]>
  231. </Code>
  232. </Snippet>
  233. </CodeSnippet>
  234. </CodeSnippets>
  235.  
  236. <?xml version="1.0" encoding="utf-8" ?>
  237. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  238. <CodeSnippet Format="1.0.0">
  239. <Header>
  240. <Title>Typed Event</Title>
  241. <Shortcut>event</Shortcut>
  242. <Description>Code snippet for event and raiser with custom EventArgs type</Description>
  243. <Author>SLaks</Author>
  244. <SnippetTypes>
  245. <SnippetType>Expansion</SnippetType>
  246. </SnippetTypes>
  247. </Header>
  248. <Snippet>
  249. <Declarations>
  250. <Literal>
  251. <ID>type</ID>
  252. <ToolTip>EventArgs type</ToolTip>
  253. <Default>My</Default>
  254. </Literal>
  255. <Literal>
  256. <ID>event</ID>
  257. <ToolTip>Event name</ToolTip>
  258. <Default>MyEvent</Default>
  259. </Literal>
  260. <Literal>
  261. <ID>desc</ID>
  262. <ToolTip>Event description</ToolTip>
  263. <Default>...</Default>
  264. </Literal>
  265. <Literal>
  266. <ID>n</ID>
  267. <ToolTip>n</ToolTip>
  268. <Default></Default>
  269. </Literal>
  270. </Declarations>
  271. <Code Language="csharp"><![CDATA[///<summary>Occurs when $desc$.</summary>
  272. public event EventHandler<$type$EventArgs> $event$;
  273. ///<summary>Raises the $event$ event.</summary>
  274. ///<param name="e">A$n$ $type$EventArgs object that provides the event data.</param>
  275. internal protected virtual void On$event$($type$EventArgs e) {
  276. if ($event$ != null)
  277. $event$(this, e);
  278. }
  279. $end$]]></Code>
  280. </Snippet>
  281. </CodeSnippet>
  282. <CodeSnippet Format="1.0.0">
  283. <Header>
  284. <Title>Event</Title>
  285. <Shortcut>event</Shortcut>
  286. <Description>Code snippet for event and raiser</Description>
  287. <Author>SLaks</Author>
  288. <SnippetTypes>
  289. <SnippetType>Expansion</SnippetType>
  290. </SnippetTypes>
  291. </Header>
  292. <Snippet>
  293. <Declarations>
  294. <Literal>
  295. <ID>event</ID>
  296. <ToolTip>Event name</ToolTip>
  297. <Default>MyEvent</Default>
  298. </Literal>
  299. <Literal>
  300. <ID>desc</ID>
  301. <ToolTip>Event description</ToolTip>
  302. <Default>...</Default>
  303. </Literal>
  304. </Declarations>
  305. <Code Language="csharp"><![CDATA[///<summary>Occurs when $desc$.</summary>
  306. public event EventHandler $event$;
  307. ///<summary>Raises the $event$ event.</summary>
  308. internal protected virtual void On$event$() { On$event$(EventArgs.Empty); }
  309. ///<summary>Raises the $event$ event.</summary>
  310. ///<param name="e">An EventArgs object that provides the event data.</param>
  311. internal protected virtual void On$event$(EventArgs e) {
  312. if ($event$ != null)
  313. $event$(this, e);
  314. }
  315. $end$]]></Code>
  316. </Snippet>
  317. </CodeSnippet>
  318. <CodeSnippet Format="1.0.0">
  319. <Header>
  320. <Title>PropertyChanged Event</Title>
  321. <Shortcut>event</Shortcut>
  322. <Description>Code snippet for PropertyChanged event and raiser</Description>
  323. <Author>SLaks</Author>
  324. <SnippetTypes>
  325. <SnippetType>Expansion</SnippetType>
  326. </SnippetTypes>
  327. </Header>
  328. <Snippet>
  329. <Declarations>
  330. <Literal>
  331. <ID>event</ID>
  332. <ToolTip>Event name</ToolTip>
  333. <Default>MyEvent</Default>
  334. </Literal>
  335. <Literal>
  336. <ID>desc</ID>
  337. <ToolTip>Event description</ToolTip>
  338. <Default>...</Default>
  339. </Literal>
  340. </Declarations>
  341. <Code Language="csharp"><![CDATA[///<summary>Occurs when a property value is changed.</summary>
  342. public event PropertyChangedEventHandler PropertyChanged;
  343. ///<summary>Raises the PropertyChanged event.</summary>
  344. ///<param name="name">The name of the property that changed.</param>
  345. internal protected virtual void OnPropertyChanged(string name) { OnPropertyChanged(new PropertyChangedEventArgs(name)); }
  346. ///<summary>Raises the PropertyChanged event.</summary>
  347. ///<param name="e">An EventArgs object that provides the event data.</param>
  348. internal protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {
  349. if (PropertyChanged != null)
  350. PropertyChanged(this, e);
  351. }
  352. $end$]]></Code>
  353. </Snippet>
  354. </CodeSnippet>
  355. </CodeSnippets>
  356.  
  357. <?xml version="1.0" encoding="utf-8"?>
  358. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  359. <CodeSnippet Format="1.0.0">
  360. <Header>
  361. <SnippetTypes>
  362. <SnippetType>Expansion</SnippetType>
  363. </SnippetTypes>
  364. <Title>compareto</Title>
  365. <Author>ee of http://www.devfuel.com</Author>
  366. <Description>
  367. A quick and dirty CompareTo implementation
  368. </Description>
  369. <HelpUrl>
  370. </HelpUrl>
  371. <Shortcut>
  372. compareto
  373. </Shortcut>
  374. </Header>
  375. <Snippet>
  376. <Declarations>
  377. <Literal Editable="true">
  378. <ID>ThisType</ID>
  379. <ToolTip>The local type</ToolTip>
  380. <Default>ThisType</Default>
  381. <Function>
  382. </Function>
  383. </Literal>
  384. </Declarations>
  385. <Code Language="csharp"><![CDATA[public int CompareTo(object obj)
  386. {
  387. $ThisType$ rhs = obj as $ThisType$;
  388. if (rhs != null)
  389. {
  390. //return this.Member.CompareTo(rhs.Member);
  391. return 0;$end$
  392. }
  393. else
  394. {
  395. throw new ArgumentException("object is not of type $ThisType$");
  396. }
  397. }]]></Code>
  398. </Snippet>
  399. </CodeSnippet>
  400. </CodeSnippets>
  401.  
  402. <?xml version="1.0" encoding="UTF-8"?>
  403. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  404. <CodeSnippet Format="1.0.0">
  405. <Header>
  406. <Title>ODP.NET Stub</Title>
  407. <Author>Rob (http://stackoverflow.com/users/1185/rob)</Author>
  408. <Description>Inserts common Oracle code</Description>
  409. <Shortcut>csoraclestub</Shortcut>
  410. <HelpUrl></HelpUrl>
  411. </Header>
  412. <Snippet>
  413. <Imports>
  414. <Import>
  415. <Namespace>Oracle.DataAccess.Client</Namespace>
  416. </Import>
  417. </Imports>
  418. <Code Language="CSharp">
  419. <![CDATA[
  420. OracleConnection oraConnection = null;
  421. OracleCommand oraCommand = null;
  422. OracleDataReader oraData = null;
  423.  
  424. try
  425. {
  426. // Connect to the database
  427.  
  428. // Prepare and run the query
  429.  
  430. oraCommand.BindByName = true;
  431.  
  432. }
  433. catch (OracleException ex)
  434. {
  435. // TODO
  436. }
  437. catch (Exception ex)
  438. {
  439. // TODO
  440. }
  441. finally
  442. {
  443. // Perform a safe cleanup
  444. if (oraData != null) { oraData.Dispose(); }
  445. if (oraCommand != null) { oraCommand.Dispose(); }
  446. if (oraConnection != null) { oraConnection.Dispose(); }
  447. }
  448. ]]>
  449. </Code>
  450. </Snippet>
  451. </CodeSnippet>
  452. </CodeSnippets>
Add Comment
Please, Sign In to add comment