Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, December 31, 2012

Chapter 2013

More:

C#
JavaScript
Comics
Photography
Books
Fitness
read more...

Wednesday, March 9, 2011

Required And Optional Function Parameters

I love using different programming languages. Whether it's C#, PHP, Python or Ruby on Rails, they each have their strengths, which makes finishing projects faster. Choose the best tool for the problem.

Today a project I'm working on required an additional parameter to a  Javascript method that was already called in several files. In C# I could write an overload method

Use Overloading in C# versions below 4
[csharp]
public void MyFunctionA (int par1, int par2)
{
//some code
}

public void MyFunctionA (int par1, int par2, string par3)
{
//some code
}
[/csharp]

Use Default values in C# 4
[csharp]
public void MyFunctionA(int a, int b = 0)
{
//some code
}
[/csharp]

A solution I just found today is using named parameters with defaults
[csharp]
public void MyFunctionB(int par1, int par2,
string par3 = "test")
{

}

MyFunctionB(par1: 10, par2: 4);
MyFunctionB(par1: 10, par2: 4, par3: "My Test");
[/csharp]

One way to do it in PHP is through a default value function parameter. If nothing is passed into the function for that parameter, the default is used:
[php]
function myFunctionA ($par1, $par2 = "test") {
//some code
}
[/php]

And in because I'm just starting to learn Ruby on Rails, I wondered how it handled function defaults or overloads. This is what I found on StackOverflow. Pretty clean, and similar to PHP.

[ruby]
def hello_world(name, message="Hello World"):
print "name = "+name
print "message = "+message
[/ruby]

And finally to Javascript, the real piece I needed to solve. Javascript let's you create a method with no defined parameters, but gives you an array you can check for values. Here is an example:

[javascript]
function myFunctionA() {
alert(arguments[0]);
alert(arguments[1]);
}
[/javascript]

This Javascript option worked great for our needs. We were able to leave the code written originally, but allow new references to the function to add in a new parameter. I didn't want to have another method with duplicate code just to handle the one new parameter, and this seemed to be the best solution.

I'm sure some of you would solve it a different way, so please share.

Thanks to Noah Sparks for helping resolve this issue.
read more...

Wednesday, August 5, 2009

Easy Way Detect Changes On The Clipboard With Windows API

A recent Windows Forms project required that I copy data from a grid and an associated object with each cell copied. This allows a user to paste the data in the same grid but in a different set of cells.

My problem came when a user would copy data from an external application like Excel, and tries to copy the text into my grid. I had to come up with a way to determine the user was trying to paste something copied from an external program.

There is a great solution using a Windows API called GetClipboardSequenceNumber().

[DllImport("user32.dll")]

static extern uint GetClipboardSequenceNumber();



Using this API allows you top capture the sequence number every time something is copied to the clipboard. Here is the workflow I went through.



1. Capture the sequence number when the mouse or keyboard events are fired



2. When the paste event is fired, check the clipboard sequence and compare it to one saved locally. If the numbers are difference, only paste the text, if they are the same, paste the text and the object.



I put the sequence capture in my Copy method so it’s changed internally every time someone chooses Edit/Copy from the menu, presses CTL-C, or right clicks and selects Copy from a popup menu.



This seems to work pretty well. There really isn’t any code to share other than the knowing which DLL and method to use. Where to store the sequence and how you use it is going to be up to you.

read more...
 
Copyright © 2003 - 2014 Thom Allen Weblog • All Rights Reserved.