Technology:
Visual Studio 2010
Title:
Box Selection and Multi-Line Editing with VS 2010
Description:
Box selection is a feature that has been in Visual Studio for a while. It allows you to select a rectangular region of text within the code editor by holding down the Alt key while selecting the text region with the mouse.
With VS 2008 you could then copy or delete the selected text.
VS 2010 now enables several more capabilities with box selection including:
• Text Insertion: Typing with box selection now allows you to insert new text into every selected line
• Paste/Replace: You can now paste the contents of one box selection into another and have the content flow correctly
• Zero-Length Boxes: You can now make a vertical selection zero characters wide to create a multi-line insert point for new or copied text
These capabilities can be very useful in a variety of scenarios.
Some example scenarios: change access modifiers (private->public), adding comments to multiple lines, setting fields, grouping multiple statements together, restructuring HTML.
Here is another example code to run batch script from C#
public void RunBatchScript(string batchPath, string arguments)
{
Process process = new Process();
process.StartInfo.FileName = batchPath;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += new DataReceivedEventHandler(writeStdOutStreamInfo);
process.ErrorDataReceived += new DataReceivedEventHandler(writeStdErrStreamInfo);
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
// Wait for the process to end
while (!process.HasExited)
{
Thread.Sleep(500); // sleep
}
Console.ReadKey();
}
public void writeStdOutStreamInfo(object sender,DataReceivedEventArgs e)
{
string s = e.Data;
Console.WriteLine(s);
}
public void writeStdErrStreamInfo(object sender, DataReceivedEventArgs e)
{
string s = e.Data;
Console.WriteLine(s);
}
Using there event handlers we will get the standardoutput and standarderror asynchronously in the command prompt
Using LinQ to XML its very easy to query the Xml files and traverse through the nodes and elements.
Here is one simple exaple to read the configuration XML file
bellow is my simple XML configuration file
Then here is the simple class to load the XML file
class ConfigElements
{
public string ServerMain { get; set; }
public string ServerVM { get; set; }
public string ClientMain { get; set; }
public string ClientVM { get; set; }
public string LatestBuild { get; set; }
public ConfigElements(XElement xElement)
{
ServerMain = xElement.Element("ServerMain").Value;
ServerVM = xElement.Element("ServerVM").Value;
ClientMain = xElement.Element("ClientMain").Value;
ClientVM = xElement.Element("ClientVM").Value;
LatestBuild = xElement.Element("LatestBuild").Value;
}
public ConfigElements(string ID)
{
XDocument doc = XDocument.Load("XMLFile2.xml");
var query = from xElem in doc.Descendants("Automation")
where xElem.Attribute("ID").Value ==ID
select new ConfigElements(xElem);
}
}
By creating the object of this class we will get all the reqired nodes
ConfigElements objConfigElements = new ConfigElements("1");
//pass here the Id of the requied values
string s= objConfigElements.ServerMain;
string t = objConfigElements.ServerVM;
string u = objConfigElements.ClientMain;
string v = objConfigElements.ClientVM;
string w = objConfigElements.LatestBuild;
int[] marks = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 100 };
int firstClassCount = marks.Where(n => n > 60).Count();
int[] distMarks = marks.Where(n => n > 80).ToArray();
Response.Write("Total first classes : "+firstClassCount.ToString() + "</br>");
foreach (int mark in distMarks)
{
Response.Write("Above Distinction : " + mark.ToString() + "</br>");
}
Lambda expressions provide a concise syntax for writing anonymous
methods. The C# 3.0 specification describes lambda expressions as a
super set of anonymous methods.
In C# 2.0, you can write a delegate using an anonymous method, as
shown in this example:
public delegate int MyDelegate(int n);
class MyClass
{
static void Main()
{
// Anonymous method that returns the argument multiplied by 5:
MyDelegate delegObject1 = new MyDelegate(
delegate(int n) { return n * 5; }
);
// Display the result:
Console.WriteLine("The value is: {0}", delegObject1(5));
}
}
This program outputs the value 25.Using a lambda expression you can use a simpler syntax to achieve the
same goal:
MyDelegate delegObject2 = (int n) => n * 5;
using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq;
namespace Lambda
{
public delegate int MyDelegate(int n);
class MyClass
{
static void Main()
{
MyDelegate delegObject1 = new MyDelegate(
delegate(int n) { return n * 5; }
);
Console.WriteLine("The value using an anonymous method is: {0}",
delegObject1(5));
MyDelegate delegObject2 = (int n) => n * 5;
Console.WriteLine("The value using a lambda expression is: {0}",
delegObject2(5));
Console.ReadLine();
}
}
}
Output:The value using an anonymous method is: 25
The value using a lambda expression is: 25
A lambda expression can use two arguments, especially when you are
using the Standard Query Operators. Let us start by declaring the following
delegate that uses two arguments:
public delegate int MyDelegate(int m, int n);
You can instantiate the delegate by using a lambda expression like this:
MyDelegate myDelegate = (x, y) => x * y;
You can then invoke the delegate and display the result as follows:
Console.WriteLine("The product is: {0}", myDelegate(5, 4));
// output: 20
