C# Internal Interface

12 Comments »

When building a C# interface, you may find a need for both public and internal methods, such as:

public class MyClass : IMyInterface
{
    public void MyPublicMethod() { }
    internal void MyInternalMethod() { }
}
public interface IMyInterface
{
    public void MyPublicMethod();
    internal void MyInternalMethod();
}

Read the rest of this entry »

Where is PaddingF? And Other Padding Oddities

1 Comment »

There is No PaddingF

There is Point and PointF, Size and SizeF, Rectangle and RectangleF, Padding and… wait, there is no PaddingF!

System.Drawing vs. System.Windows.Forms

The 2-D drawing functions in the System.Drawing namespace accept both integer and floating point measurements.  That’s why the main 2-D drawing structures have both int and float versions such as Point and PointF, respectively.

However, the Padding structure is defined separately in the System.Windows.Forms namespace where most 2-D measurements are in integers.  Hence, there is an integer Padding structure but no floating point PaddingF.

Read the rest of this entry »

Visual Studio Productivity Power Tools Update

No Comments »

Visual Studio Productivity Power Tools.  Copyright © Microsoft Corp.

Microsoft has updated the free Productivity Power Tools for Visual Studio 2010.  These are add-ins that provide very useful additional functionality for Visual Studio.  New in this release:

  • No More Extension Resets – This version of the Productivity Power Tools will be the last which resets the extensions.
  • Find – Quick find & incremental search now pops up at top right-hand corner of the editor.
  • Enhanced Scrollbar – Icons overlay the scrollbar to show edits, breakpoints, bookmarks, errors, warnings, etc.
  • Middle-Click Scrolling – Use your scroll wheel to quickly scroll through your document.
  • Organize Imports for Visual Basic – Sort the imports logically and remove the ones that aren’t being used.

Productivity Power Tools for Visual Studio 2010

See more .NET News like this!

Microsoft All-In-One Code Framework

1 Comment »

All-In-One Code Framework.  Copyright © Microsoft Corp.

The Microsoft All-In-One Code Framework is a free, centralized code sample library provided by the Microsoft Community team.  Their goal is to provide typical code samples for all Microsoft development technologies.

The team listens to developers’ pains in MSDN forums, social media and various developer communities.  They write code samples based on developers’ frequently asked programming tasks, and allow developers to download them with a short code sample publishing cycle.  Additionally, the team offers a free code sample request service.  This service is a proactive way for our developer community to obtain code samples for certain programming tasks directly from Microsoft.

Download all code samples
Browse all code samples

Vanity Guids

6 Comments »

Do you notice anything odd about the following list?

00000000-9b6d-4998-9dd7-6026894bdfba
11111111-9022-4400-bac2-8b66a9874443
22222222-a890-4dec-98bc-f41536b760bc
33333333-e361-4239-8d04-3f16f68ad9ce
44444444-d8c2-40ab-91bd-5a84511ed9d3
55555555-447a-4aa9-a51f-35c74a154156
66666666-193b-4ac3-bd92-860b6b49aedb
77777777-49de-4cc5-b9e6-2e5785dd47af
88888888-0d00-4672-933a-d68e240772be
99999999-7d9d-4d77-9e35-5e919db0f7d1
aaaaaaaa-76cd-4d6b-bae2-574e5b57c7ab
bbbbbbbb-6f9e-4d2d-ba11-64df5c7355fa
cccccccc-b897-4b15-9ab3-11b97836ce85
dddddddd-b417-48ad-8b5b-b762df75e03b
eeeeeeee-cc9c-4cb8-bae0-bbd4b10307fa
ffffffff-8d46-4a31-b297-2ac67dda3600

Read the rest of this entry »

Cannot Set “Startup Object” in Silverlight Project

1 Comment »

If your Silverlight project compiles correctly and appears to run, but it displays only a blank page in the web browser, the first place to check is the “Startup object” in the Silverlight’s project properties:

Silverlight project properties

Read the rest of this entry »

Iterate Over IDictionary

4 Comments »

To iterate over an IDictionary<x,y> interface, use the KeyValuePair<x,y> structure.  Following is a simple example:

Read the rest of this entry »

Force XmlWriter or XmlTextWriter to use Encoding Other Than UTF-16

2 Comments »

You may have noticed the first line of XML output generated by XmlWriter or XmlTextWriter shows that the encoding defaults to UTF-16:

<?xml version="1.0" encoding="utf-16"?>

This happens even if you explicitly set the Encoding property in the XmlWriterSettings to something different, such as UTF-8:

StringBuilder sb  = new StringBuilder(); 
XmlWriterSettings settings = new XmlWriterSettings (); 
settings.Encoding = System.Text.Encoding.UTF8; 
XmlWriter writer = XmlWriter.Create (sb, settings); 

The problem occurs because the StringWriter defaults to UTF-16.  (It’s not clear from the example above, but the XmlWriter class uses a StringWriter to output the XML to the specified StringBuilder.) 

Read the rest of this entry »

Rename a File in C#

4 Comments »

If you want to rename a file in C#, you’d expect there to be a File.Rename method, but instead you must use the System.IO.File.Move method. 

You must also handle a special case when the new file name has the same letters but with difference case.  For example, if you want to rename “test.doc” to “Test.doc”, the File.Move method will throw an exception.  So you must rename it to a temp file, then rename it again with the desired case.

Here is the sample source code:

/// <summary> 
/// Renames the specified file. 
/// </summary> 
/// <param name="oldPath">Full path of file to rename.</param> 
/// <param name="newName">New file name.</param> 
static public void RenameFile( string oldPath, string newName ) 
{ 
    if (String.IsNullOrEmpty( oldPath )) 
        throw new ArgumentNullException( "oldPath" ); 
    if (String.IsNullOrEmpty( newName )) 
        throw new ArgumentNullException( "newName" );

    string oldName = Path.GetFileName( oldPath );

    // if the file name is changed 
    if (!String.Equals( oldName, newName, StringComparison.CurrentCulture )) 
    { 
        string folder = Path.GetDirectoryName( oldPath ); 
        string newPath = Path.Combine( folder, newName ); 
        bool changeCase = String.Equals( oldName, newName, StringComparison.CurrentCultureIgnoreCase );

        // if renamed file already exists and not just changing case 
        if (File.Exists( newPath ) && !changeCase) 
        { 
            throw new IOException( String.Format( "File already exists:n{0}", newPath ) ); 
        } 
        else if (changeCase)
        {
            // Move fails when changing case, so need to perform two moves
            string tempPath = Path.Combine( folder, Guid.NewGuid().ToString() );
            Directory.Move( oldPath, tempPath );
            Directory.Move( tempPath, newPath );
        }
        else
        {
            Directory.Move( oldPath, newPath );
        }
    } 
} 

Project is not selected for building in solution configuration

3 Comments »

When building a Visual Studio project, you may encounter the following error:

The project "MyProject" is not selected for building in solution configuration "Debug|Any CPU".

This error occurs because the project has not been configured to build in your Visual Studio solution.  The solution is simple:

Read the rest of this entry »

« go backkeep looking »