by robotix1986
19. July 2009 11:44
So just some time back I got thinking... everytime I start reading mails in the C# discussion alias at my company or Eric Lippert's explanations in them... the first thing I do is open up my very extensively used Console Application project in Visual Studio, comment out the existing main function for future reference and start writing a new one... and it is kind of a pain to do this.. so I thought that it's time to get it automated.. so I wrote a snippet to do that for me.
Below is the code for that: -
|
<?
xml version="1.0" encoding="utf-8" ?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>mainfn</Title> <Shortcut>mainfn</Shortcut> <Description>Code snippet for 'Main' function</Description> <Author>Alvaro Rahul Dias</Author> <SnippetTypes> <SnippetType>Expansion</SnippetType> <SnippetType>SurroundsWith</SnippetType> </SnippetTypes> </Header> <Snippet> <Declarations> <Literal> <ID>expression</ID> <ToolTip>Code in main method</ToolTip> <Default>Console.WriteLine("Hello World!");</Default> </Literal> </Declarations> <Code Language="csharp"><![CDATA[ public class MyClass { public static void Main() { $expression$ $selected$ $end$ } }]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets> |
Most of the fields above are self explanatory. I'll explain a few important ones.
The <Shortcut> tag specifies what you need to type in VS to use this snippet.
<SnippetTypes> - where you can use this snippet, Expansion, Surround With or both.
<Code Language="csharp"> - the actual code to be inserted
Interesting thing to note is that all the variable items are marked with $ signs. The $selected$ attribute signifies where the selected code should go in case you use this code as a sorround with snippet.
The $end$ attribute signifies where the cursor should go once you're finished with the snippet.
Hope the above helps in your coding adventures.
Regards,
AlD
by robotix1986
16. July 2009 16:02
Well this entry is all code.. no explanations... you figure it out yourselves
Microsoft.SqlServer.Dts.Runtime.
Package package = new Microsoft.SqlServer.Dts.Runtime.Package();
Executable executable = package.Executables.Add("STOCK:PipelineTask");
Microsoft.SqlServer.Dts.Runtime.TaskHost thMainPipe = executable as Microsoft.SqlServer.Dts.Runtime.TaskHost;
MainPipe dataFlowTask = thMainPipe.InnerObject as MainPipe;
// Add a flat file destination component to the data flow task
IDTSComponentMetaData100 destinationComponent = dataFlowTask.ComponentMetaDataCollection.New();
destinationComponent.Name = "FlatFileDestination";
destinationComponent.ComponentClassID = "DTSAdapter.FlatFileDestination";
// Get the design time instance of the flat file destination component.
CManagedComponentWrapper destinationInstance = destinationComponent.Instantiate();
// Initialize the flat file destination component
destinationInstance.ProvideComponentProperties();
// Configure the flat file destination component to use the Flat File connection manager
destinationComponent.RuntimeConnectionCollection[0].ConnectionManager = DtsConvert.GetExtendedInterface(destinationConnection);
destinationComponent.RuntimeConnectionCollection[0].ConnectionManagerID = destinationConnection.ID;
// Configure the custom properties of the Flat File destination component
destinationInstance.SetComponentProperty("Header", "");
destinationInstance.SetComponentProperty("Overwrite", true);
// Reinitialize the metadata.
destinationInstance.AcquireConnections(null);
destinationInstance.ReinitializeMetaData();
destinationInstance.ReleaseConnections();
// ADD OTHER TASKS HERE
Hope this helps.
Regards,
AlD
by robotix1986
2. June 2009 11:16
So there I was, sitting at my desk staring at the computer screen working on VS 2010 and exploring a bit of F#. I just sat back and started reflecting on my college days and the interviewing season in the 7th semester. All of us, revising all our technical knowledge, solving puzzles and complex analytical problems.
As I think about it now, I think that those problems (especially the programming ones) were pretty easy ones, but in that period, preparing for your first tryst with the world outside, its a huge thing. So, feeling a little nostalgic, I thought I’ll rewrite some of those programs in C# this time.
1. How to swap two numbers without using a third variable.
public class SwapperClass
{
public static void Main()
{
int number1 = 10;
int number2 = 20;
Console.WriteLine("a = {0}, b={1}", number1, number2);
Swap(ref number1, ref number2);
Console.WriteLine("a = {0}, b={1}", number1, number2);
Console.ReadLine();
}
public static void Swap(ref int a, ref int b)
{
a = a + b;
b = a - b;
a = a - b;
}
}
2. How to find out whether a number is a power of 2 or not in O(1) or without using any loops
public class PowerOfTwo
{
public static void Main()
{
Console.WriteLine(IsPowerOfTwo(4));
Console.ReadKey();
}
public static bool IsPowerOfTwo(int a)
{
return (a != 0 && (a & (a - 1)) == 0);
}
}
Till my next post,
Adeus,
Alvaro