Named & optional parameters are a new C# language feature coming
up with .NET FX 4.0 and guess what… it’s in Silverlight 4 as well!
Optional
parameters will come very useful when defining complex APIs
where you would usually have to provide several overloads for the same
method for it to make sense to wide variety of usages. Let’s take a look
at this basic example:
public
void PositionWindow(bool isTopMost, double left = 0, double top = 0, double width = 800, double height = 600)
{
Window window =
Application.Current.MainWindow;
window.TopMost = isTopMost;
window.Left = left;
window.Top = top;
window.Width = width;
window.Height = height;
}
isTopMost is the only parameter that’s required, all other
parameter specify their default value, which makes them optional.
But what if you wanted to provide the size only and leave the position
to be set to defaults? Enter named parameters. The same
method above can be called also with:
PositionWindow(isTopMost:true, width: 600, height:
200);
PositionWindow(true, width: 600, height:
200);
PositionWindow(true, height: 200, width:
600);
PositionWindow(height:
200, width: 600, isTopMost: true);
Simple ... Cheers:)
String.IsNullOrWhiteSpace
Previous version of C# contain method String.IsNullOrEmpty which checks whether string is empty or null. If String only contain white spaces (it's also a case of empty string) you will be required to add an additional trim() function to handle it. But if you will use trim() on an null string, it will throw exception so you will be required to write few lines of code for it. But new function in string class
The new String.IsNullOrWhiteSpace method indicates whether
a string is null, empty, or consists only of white-space characters.
In Silverlight, you will bind the controls such as List/Grid with Observable collection so that any change in the collection automatically reflected on the control.
Often you will be required to convert the List to an observable collection but there isn't exist any method which automatically do so in Silverlight. Moreover, WCF service can only return IEnumerable collection which you may want to convert to the ObservableCollection. So, here is generic way to convert any List to Observable collection;
| var observableCollection = new ObservableCollection<T>(); |
| foreach (var
item in enumerableList) |
| observableCollection.Add(item); |
| return observableCollection; |
I hope this will help you ... Cheers:)
Its flexible, while designing Database to have some fields in XML. It allows to store multiple values/structure which can be easily extended whereas in case of simple table you will need to add new column(s) and then change the data access layer etc.
XML column can be easily manipulated using the TSQL. we can select the particular node value(s) in the form of column(s), update them as well as apply them in where clause;
Here is the way how to select particular attribute value in the form of column;
Select CAST(Variables as XML).value('(/Variables/Variable[@name="OrderTakenBy"]/@value)[1]', 'varchar(500)') As OrderTakenBy
from tblVariables
Similarly, we can filters the records based on a particular attribute of xml column
Select VariableName
from tblVariables
WHERE CAST(Variables as XML).value('(/Variables/Variable[@name="OrderTakenBy"]/@value)[1]', 'varchar(500)') = 'Nadeem'
Following script shows how to update a particular attribute value with a fixed value;
UPDATE Vars
SET CAST(VARIABLES as XML).modify('replace value of (/Variables/Variable[@name=''OrderTakenBy"])[1]/@value with "Nadeem"')
FROM tblVariables AS Vars
Moreover, you can update the attribute value with different values from other table
UPDATE Vars
SET CAST(VARIABLES as XML).modify('replace value of
(/Variables/Variable[@name=''OrderTakenBy"])[1]/@value with sql:column("O.Name")')
FROM tblVariables AS Vars
INNER JOIN Orders O
ON O.pkey = Vars.kOrder
You can use XPATH to specify node values as well CHEERS :)
There are scenarios when you want to directly upload the file from the client browser/application to S3.
Amazon provides simple POST method to upload files to amazon File Upload. That method is not acceptable when you want to upload the file(s) programatically using any programming language such as C# or by using the Silverlight.
C# doesn't provide the support of "multipart/form-data" form posting which is required to upload directly to the S3. So, HttpWebRequest class can be configured so that it will send the request in the required format.
So, I've written a custom class which will generate the request contents in "multipart/form-data" format.
Below is the code how to use that custom class and upload to the amazon S3 directly using C#/SilverLight
UploadFile uf = new UploadFile("file.txt", "file", "txt/html"); // file should be in bin directory
string []a = new string[5];
UploadFile[] files = new UploadFile[1];
files[0] = uf;
NameValueCollection form = new NameValueCollection();
form["key"] = "file.txt";
form["acl"] = "public-read";
form["success_action_redirect"] = "http://www.yahoo.com";
form["x-amz-meta-uuid"] = "14365123651274";
form["x-amz-meta-tag"] = "";
form["AWSAccessKeyId"] = "zzzzzzzz";
form["Policy"] = "zzzzzzzzzzzzzzzzzzzzzzzz";
form["Signature"] = "zzzzzzzzzzzzzzzzzzzzzzzzz";
string url = "http://MyBucket.s3.amazonaws.com/";
string resultQuery = HttpUploadHelper.Upload(url, files, form);
Console.WriteLine(resultQuery);
CHEERS :)
SnapFlow is a RIA Application develop on the Cutting Edge Microsoft Technologies such as SilverLight, ASP.Net 3.5, LINQ, WCF, WWF and SQL Server 2005.
SnapFlow is a workflow-making platform that helps you coordinate your
work with anyone so that you can get things done. And with our
super-simple tool, you can build a flow that reflects your unique
process.
SnapFlow provides the interactive interface for designing the workflows by dragging and dropping items.
I really enjoyed being the part of the SnapFlow Development team and worked on the SilverLight part of the application. The application extensively use the Reflection for generating the classes and collections on the fly as well as populating the controls with dynamic collections such as DataGrid etc..
I really appreciate the effort of Tim Heuer for writing excellent blog on SnapFlow Method~Of~Failed
When writing unit tests for the Silverlight application, its important to write the unit test for the value converters so that you could test whether converters are working fine or not as Value Converters are most commonly used in Silverlight applications.
IValueConverter interface exposes two methods Convert & ConvertBack which are private by default and one can't call these methods explicitly in any other class. Neither you can write public wrappers on the Convert and ConvertBack methods as we can't explicitly call these methods. So we need some type of workaround to test the Value Converters.
Here is what we will have to do read full artical
There are scenarios when you want to Format the bound string value for
example you want to display the long names as nadeemIq... or display a
different value based on the bound string value & get back the
original value. Read the complete blog post
I was working on the unbound SilverLight DataGrid and thought to write one in this regard,
There are scenarios when you don't have a predefined collection of objects to bind to SilverLight DataGrid or you don't know the structure of the object to bind with untill run time. For Example you want to bind the DataGrid with XML or to any other source.
So the way is to Create the Structure (Type/Class) of object dynamically on the fly and then build the generic List of newly created Type and bind it to SilverLight DataGrid. Moreover you want to add the new empty rows in grid, get the object bound to that row, Extract the values from the bound object, delete a row etc......
Lets first look at how to create a new Type(class) on the fly with properties and bind it to the DataGrid;
Dynamic Typed Objects Creation by Vladimir Bodurov (thanks)
Update the DataSourceCreator class to add a public property to maintain the type of the new structure
public static Type getType{get; set;}
and set this property before the last return statement of the ToDataSource function
getType = tb.CreateType();
Now how to get the bound Object of selected row of DataGrid on a button click & get values of the properties
var item = Activator.CreateInstance(DataSourceCreator.getType);
int index = mygrid.SelectedIndex;
item = mygrid.SelectedItems[0];
MethodInfo theMethod = DataSourceCreator.getType.GetMethod("get_Column1");
object res = theMethod.Invoke(item, new object[0]);
in the above code i created the new var variable of the Type and stores the selected bound object in it. And then using reflection extracted the value of the property Column1.
Lets Add new row in SilverLight DataGrid
IEnumerable<object> collection = mygrid.ItemsSource.Cast<object>();
List<object> list = collection.ToList();
var item = Activator.CreateInstance(DataSourceCreator.getType);
list.Add(item);
mygrid.ItemsSource = list;
It first gets the bound collection of the DataGrid and then adds a new object into the collection
Similarly you can remove an object from the bound collection
list.Remove(item)
Its all about Unbound Simulation of SilverLight DataGrid. Cheers :)
You can customize the DataGridView to work the way you want. There are scenarios when default behavior of the DataGridView is not sufficient to fulfill the requirements. For example you want to override the behavior of DataGridView such that when i press enter key on a particular cell the cursor/focus move to the cell below to that cell not in the next cell (which is default behavior) similarly while editing data in DataGridView you don't want that the cursor move to the first cell of the next row after editing a row however you want to skip the first column for editing e.g there is an row counter in the first column and you don't want to edit it.
So lets dig into customizing the DataGridView and make it the way we want....
In this sample we modify the DataGridView Behavior as follows; On pressing enter on any cell it creates the new DataGridView row below to that cell and when tab button is pressed at the last cell of the row it creates the new row and moves the cursor to the second cell of the new row (skipping the first column).

In order to customize the DataGrid, we would have to create a new Component which will directly inherit from the DataGridView as;
public partial class myDGView : DataGridView
and we will required to override the following two methods;
-
protected override bool ProcessDialogKey(Keys keyData)
This method is called during message preprocessing to handle dialog characters, such as TAB, RETURN, ESCAPE, and arrow keys.
-
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
Processes keys used for navigating in the DataGridView
In both of the above methods; we do much the same thing. We check which key has been pressed and then respond accordingly;
The below code snippets checks the key pressed and then adds the new row below the current row
if (keyData == Keys.Enter)
{
base.Rows.Insert(base.CurrentRow.Index + 1, 1);
base.SelectedCells[0].Selected = false;
base.Rows[base.CurrentRow.Index + 1 + 1].Cells[1].Selected = true;
base.CurrentCell = base.Rows[base.CurrentRow.Index + 1 ].Cells[1];
return true;
}
The source code is attached which contains the C# Library Project sample code. Which will generate the DLL which you can add in your Visual Studio's Toolbox by just drag & drop and can use the customized DataGridView as an simple DataGridView.
More Posts