Showing posts with label How To. Show all posts
Showing posts with label How To. Show all posts

November 16, 2011

How to create Relative Path in C#

It is pretty easy to create relative path in C#. Just what you need is to use System.Uri class.

Example:
System.Uri root = new System.Uri( @"C:\TestDir\" );
System.Uri filename = new System.Uri( @"C:\TestDir\MyTestLocation\MyFile.cs" );
System.Uri relativePath = root.MakeRelativeUri( filename )

Console.WriteLine( relativePath.toString() );
At the Output you will see "../MyTestLocation/MyFile.cs".

September 17, 2011

Refactoring

Every developer sooner or later starts understanding that his code needs to be re-factored. This presentation will give answers for "What is refactoring?", "Where is it necessary to do refactoring?", "How to do this?"
View more presentations from Yuriy Seniuk

April 17, 2011

How to share resources between projects.

It was a very annoying problem with sharing icons between projects.

The solution described here is how to share icons between projects using Visual Studio in correct way. Lets start step-by-step!
1. The correct way in sharing some resources consist in creation of the global shared project. Create new Project with name Resources;


2. Next we must add some resource (e.g some icons) to the project. Do this as usual. Go to the projects setting. Select tab Resources and Add Existing File... to the project. We can see that icon is added to the project and was copied to the local folder;


3. Next step consists of adding this icons to the other project. But one important think! You must add this icon as link. In this case we avoid the resource duplication. OK. Create new Project is same solution and name it for example Main. Create some folder in your other project. For example we can name it as Resource. (Logical name for our purpose). Then right click at this folder. Select Add Existing Item... and choose our image from shared project. Do not forget to add this file as link. Well, you see that just added file can a little differ icon;


Added image must looks like this

4. Now we must set Build Action for this file to None. For this select the file and go to the Properties Window. There choose None for Build Action. We need to do this to avoid  the embedding this icon to the Assembly;


5. Well, we must to perform last step to complete task. Open Project Settings for project where we just adds the icon. Choose the Resource tab and drag linked image to the resources.

 

Yep! These five simple steps you must perform to share some icons between projects. But you can ask "What is the benefits of this?" There are:
  • All resources are located in one place;
  • Resources are not grabbed by projects locally. It means you can substitute some icons is Shared project and they will automatically be caught in all paced where they used;
  • If you share large files, you save the size of your App;
  • In case if you share icons between project you can use this icons in designer mode.

November 30, 2010

How to increase speed of DataGridView or using DoubleBuffered property...

A few days ago I finished developing the output window of results. It's a window similar to Visual Studio Output Window. It has a few tabs and one tab was very-very annoying. Why? Because it first performance was very low... This tab is responsible for showing all messages from external compiler what we use to compile some source. For the displaying compiler messages we used standard DataGridView component. I don't like it but his layout is that what I need. When we start our application to have real tests we were sadden. Blinking, flicker, pulling... It was the terrible to think that someone will use this dung... Google is power! But not in this situation. Every query doesn't get appropriate results. Search and search.. Nothing... Only similar troubles with no smart answers. You say I search bad.. Maybe, but in that day I don't think that. In that day power has a stackoverflow. :) Yes, yes, yes... Two minutes and I have a great answer.
public static void DoubleBuffered(this DataGridView dgv, bool setting)
{
    Type dgvType = dgv.GetType();
    PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
          BindingFlags.Instance | BindingFlags.NonPublic);
    pi.SetValue(dgv, setting, null);
}


Only one extension method to DataGridView and that's all. It's amazing..

Blinking, flicker, pulling.. No, no, no... Only smooth, clarity, speed... All what we need.

And only one question stay in my head. Why "DoubleBuffered" property is hidden? Maybe when whe set it to the true our application eats all RAM memory? Maybe our Duo Core Processors will work to the 100% to render data.. Hm.. Practical experience shown that with memory is all right (a little more, but a little) and with processor too.

MSDN says that this property indicating whether this control should redraw its surface using a secondary buffer to reduce or prevent flicker.

October 13, 2010

Express Review of Expression Trees

Yesterday I spoke about the Expression Trees. I must admit that haven't practice with this pretty feature in C#3.0. But after several blogs and msdn info I can tell that this is powerful technology which allows us to create a dynamic scenarios in our programs. To work with expression trees user must has a basic knowlages of lambda expression and understand what is a tree. Calculations in the tree are moves from the leafs to the root of tree. Miscrosoft provides a 56 different actions to use in Expressions. There are Add, Bind, Block, Break, Call, Catch, Coalesce, Continue, Equal, GreaterThan, Lambda, Loop, New, Or , PowerAssign and other.. Expression class has a several derived class such as BinaryExpression, BlockExpression, ConditionalExpression, ConstantExpression, DynamicExpression and other, which separate expression by their type. It is necessary to say that every node in the tree is an expression. Digits or Strings are also expression, to be exact there is an Expression.Constant.
Everybody who works with expression trees must be very attentive. It's too lightly to make some mistake when constuct trees. For example if you write
Expression.Add( Expression.Constant( 2 ), Expression.Constant( 3 ) ); 
 it's correct and represent a (2+3) expression, but the next code will take a runtime exception "The binary operator Add is not defined for the types 'System.String' and 'System.String'."

Expression.Add( Expression.Constant( "ab" ), Expression.Constant( "cd" ) );
It's one of the many cases when successful compilation isn't a successfull execution of the program.
 
Expression trees
View more presentations from Yuriy Seniuk.

Good luck! Try yourself with expression!

September 27, 2010

Lambda Expressions

Now I'm finished work at my presentation about Lambda Expressions. You can find here a list of examples how to use Lambda Expressions in your life.I hope that this material will be useful. Look and enjoy!