Appending a String to All Strings in a List<String>

by fwhagen Wed, 13 June 2012

This is one of those great 1 liners in C# that I can never remember how to do.  If you want to add a value to every string in a List<String>, you can use LINQ to easily utilize String.Format() (or String.Concat()) to do so.  Given a List<String> paths:

paths = (from p in paths select String.Format(@"{0}\badfiles\", p)).ToList<String>();

Keywords:

Filed Under: .NET

Software Versioning Rules

by fwhagen Wed, 07 March 2012

Find a versioning method and stick to it!  I have been looking for a good set of rules to apply to all versioning exercises and I have finally found one called Semantic Versioning, or SemVer, online.  And as a bonus, it follows my preference in versioning pretty closely too.  While I normally use Major.Minor.build or Major.Minor.Patch.build depending on scope, SemVer prescribes following convention:

  • Major: Breaking changes.
  • Minor: New features, but backwards compatible.
  • Patch: Backwards compatible bug fixes only.

Good rules to code by.  And since I like to include the build number as the last digits, automation through the fantastic Build Version Increment VS plugin, makes it (almost) easy.  (Configuration can be a challenge on larger projects.)  Dig a little to find the latest version that also supports VS2010.

Keywords: , ,

Filed Under: .NET | Programming | Software

Simple C# Command-Line Compiles

by fwhagen Tue, 29 September 2009

In the category of “So I don’t forget again”, here is how to compile a simple code.cs file to a console executable:

csc /t:exe NetDir.cs

Very simple, very easy.  To make it a Windows exe (console app with no console window, very useful for scheduled or batch processing), change the target (/t == /target) to winexe.

Keywords:

Filed Under: .NET

Converting Large (Random) Numbers to AlphaNumeric Strings

by fwhagen Thu, 07 May 2009

I have a need to take large random numbers (on the order of 20 digits) and encode them to easily typable strings.  I created a quick method to do this in C#.  This is the code:

private string ConvertToBase(ulong id)
{
    // Seed replacement characters for baseX values
    string NumericBaseData = "0123456789ABCDEFGHJKMNPRSTUVWXYZ"; // this string may be in any order as long as each char used only once
    ulong OutputBase = Convert.ToUInt64(NumericBaseData.Length); // baseX determined by number of unique chars in seed string
    string OutputValue = "";
    ulong In = id;
    while (In > 0)
    {
        ulong tn = In % OutputBase;
        OutputValue = string.Concat(NumericBaseData.Substring(Convert.ToInt32(tn), 1), OutputValue);
        In = In - tn;
        In = (In / OutputBase);
    }
    return OutputValue;
}
 
For simplicity, I used unsigned longs (64bit), since I am dealing with really big numbers.  Performance is reasonable, I am able to generate a large System.Random, mod it against an arbitrary number of DateTime.Now.Ticks, datestamp it, and convert it using the method above in under 9 ms each.  (I also have an algorithm to test for uniqueness against previously generated values, but the timings grow with greater sample size as expected.)  This method can be modified with any number of cipher characters as long as they are only used once.  This particular one uses all letters and numbers except 'O', 'Q', 'L', and 'I' to avoid confusion to end users and is URL freindly.

Keywords: ,

Filed Under: .NET | Programming

Does This String Have Anything In It? in C#

by fwhagen Wed, 04 March 2009

The following code should be failed by the C#.NET compiler:

if (eventName != null && eventName.Length > 0)

It should never be used.  People, always use this instead:

if (!String.IsNullOrEmpty(eventName))

It is much more compact, clearer, singleton, and as a String class method, more efficient.

So stop it!  You will thank me later.  Not to mention:  Where are your parentheses?  Block your code.  Other people have to read it too!

That is all.

Keywords: , ,

Filed Under: .NET | Programming | Rant

C# Business Objects and CSLA.NET

by fwhagen Thu, 11 December 2008

I am learning and working with a Business Objects framework at work called CSLA.NET (Component-based Scalable, Logical Architecture).  It is built heavily using the best of OOP especially polymorphism and abstraction.  You build your objects all based on a handful of base classes that provide a huge variability of useful features.  It also abstracts out data and .NET remoting layers to make the developers life easier.  If you are building large, scalable n-tier applications, you should definitely look this one up. 

Also check out the book Expert C# 2005 Business Objects, which is the companion to the framework.  If nothing else, it contains a great description of n-tier architecture and why to use it.

Keywords:

Filed Under: .NET

Subversion for Visual Studio Users

by fwhagen Tue, 14 October 2008

VisualStudioLogo subversion_logo-200x173 There are a few articles online covering SVN and VS, but many are out of date and a bit more complicated than necessary with advances in both products.  For a current install (Oct 2008:  SVN 1.5.2 / VS2005), you only need 3 files:

Subversion is, of course, the Source Control server itself.  The MSI install contains everything needed to run on a Windows server.

TortoiseSVN is a plug-in for Explorer that gives a great deal of control to the source repository using the right-click context menu.  Check-ins, updates, merges, etc. can be done through this interface as well as full repository browsing and maintenance.

AnkhSVN is the most important tool for the .NET developer.  It integrates the controls needed for team development directly in the environment.  Much like the integration offered through VSS, but much better.

Installation of Subversion itself is very easy.  Just run the installer.  I installed into C:\SVN\ instead of the default "Program Files" directory, but I am lazy and hate spaces in filename on the command-line.
NOTE:  The installer only puts the files in place and sets the important environment variables.
Once installed, the following command is used to setup a repository:

svnadmin create "D:\SVN\Repository"
You can create as many repositories as needed, but one for now.  Before we activate the server, two config files need to be edited.  They were created with the repository above.  They are svnserve.conf and passwd in the conf sub-dir of the repository.  Uncomment the following:
~/conf/svnserve.conf
[general]
anon-access = read
auth-access = write
password-db = passwd

~/conf/passwd
[users]
harry = harryssecret
sally = sallyssecret

Now the installation can be tested. Execute the following command in a command window:

svnserve --daemon --root "D:\SVN\Repository"

To create a project, execute:

svn mkdir snv://localhost/newproject

Now would be a good time to install TortoiseSVN if it hasn't been yet.  Connect to the running server from within an explorer window by right-clicking and selecting TortoiseSVN > Repo-Browser.  Enter the name of the server to see the interface using the following URL:  svn://servername/  The project "newproject" should be viewable there.

And now the most difficult part of all:  Installing SVN as a service in windows.  The following command will accomplish this.  Syntax and proper spacing is critical.  (I was having problems until I realized the space after each '=' in the command.)  Here it is:

sc create SVN_CSTeam binpath= "c:\svn\bin\svnserve.exe --service --root D:\SVN\Repository" 
displayname= "Subversion Repository for CSharp.NET Team" depend= iisadmin

And that is why I do not use the default paths.  Spaces have to be delimited with quotes, which have to be escaped, and it just is not worth it.  All on one line of course.  Also note that the name after the "create" parameter can be anything desired and allows for multiple repositories on the same server, along with unique displayname.  I chose to use iisadmin as a depend, not because it necessarily is, but because it is sure to load other depends such as Tcpip and such.

The last step is to load AnkhSVN; a solution I leave to the reader.  It's not hard.  If you are familiar with source control at all, it is pretty obvious.


I'd like to thank the following sites for very useful information in helping me through the installation and the creating of this post:

Keywords: ,

Filed Under: .NET | Programming

Suppressing Paging Numbers on Short DataGrids

by fwhagen Thu, 21 August 2008

I hate seeing a solitary "1" on the bottom of a paged datagrid because the dataset is too short to have more than one page.  You don't really want to turn off paging but suppress the "1" when it is not needed.

Use this code to do just that when binding the grid:

        if (dgEmployee.Items.Count < dgEmployee.PageSize)
            dgEmployee.PagerStyle.Visible = false;

Keywords:

Filed Under: .NET

Easy Image Tools (and FREE too!)

by fwhagen Thu, 08 May 2008

It amazes me in my travels how many people feel the NEED Photoshop for web development.  And, of course, they only use about 1% of its total capability.  It is true that years ago, it was the only game in town for Image creation and manipulation.  But not for a while now.  There are many free alternatives that suit the web developer much better.

Paint.NET My favorite for most tasks is Paint.NET.  It greatest feature is its price:  absolutely free!  It started life as a college project and was so successful, that it lives on as a great piece of Freeware development.  It is not Open Source, however.  Some of the other features include layering, filters, great image processing tools, alpha transparency, PNG, and many, many more.  All put together in a small, efficient package that is both easy to use and very powerful.  And hundreds of dollars less than Photoshop and lacking the disturbing Adobe trend toward spyware.  And as the name suggests, it is built on C#.NET, so I feel like I am supporting the community I am a part of.

GIMP If you need more, there is the GIMP project.  Much more complicated, far more powerful, still free, but not nearly as user friendly.  It resembles Photoshop in many ways, including the powerful plug-in model that PS uses.  I use GIMP 2.2 for things I cannot do in Paint.NET, which really isn't very often. 

You know, between the two, I don't at all miss the copy of PS I had at my last job.  Not at all.  Of course, I am not a graphic designer or a heavy Photographer either that needs the power of PS, but I haven't worked with many people who really were either.

One last note.  For those that have been heard to say, "[MS] Paint is good enough.":  Do yourself a favor and get Paint.NET.  MSPaint hasn't been good enough since the '90s.

Keywords: , ,

Filed Under: .NET | Programming | Work

Checking for Database NULL Values in C#

by fwhagen Tue, 09 October 2007

This really should have been more obvious to me, so why did I have to look it up?  If you need to check for NULL in a returned field from SQL (or any other datasource), you should the .Equals method on System.DBNull.Value.  I usually prefer the "==" notation for conditionals, but that's just me.  This is more efficient.  The code follows:

if (!dsSpecQuery.Tables[0].Rows[0]["device_type_end"].Equals(System.DBNull.Value)) 
{
    _EndDate = Convert.ToDateTime(dsSpecQuery.Tables[0].Rows[0]["device_type_end"]);
}

Of course, dsSpecQuery is a DataSet, and the field in question is DateTime out of SQLServer (not that it matters for the conditional).

Keywords: , ,

Filed Under: .NET | Programming | SQL

TextBox

RecentPosts