Archive

Posts Tagged ‘C#’

WPF Named Colors

January 14th, 2012 No comments

I have grown tired of looking up the named colors for my WPF apps.  So I wrote a Helper class to make my life a little easier. 

After playing around with this idea for a couple hours, I found a Dictionary<string,Color> is the easiest and most flexible way to reach my goal. 

Read more…

Yet Another Singleton

January 14th, 2012 No comments

I’ve got an amusing problem at work.  We need to create a way of passing runtime arguments around our app with safe and consistent results.

One solution is create a global class that sits in the core library.  All of the properties would have to be static.  At runtime, the bootstrapper updates the properties then we point everything to the global class when we need to know a value. 

But it smells.  A stinky lit’ hack. 

Read more…

Factorials

November 17th, 2011 No comments

First, what’s a factorial?

Paraphrasing Wikipedia: In mathematics, the factorial of a non-negative integer ‘n’, denoted by ‘n!’ is the product of all positive integers less than or equal to the integer.  (link)

Read more…

Setting a read-only private field

July 14th, 2011 No comments

I found this in Jimmy Nilsson’s book, Applying Domain-Driven Design… 

For me, I can see using this when rehydrating object sent over the wire or pulled from a database.  If your class uses a self-created GUID to determine uniqueness, then you’d have to override that value when building an object up with predetermined values.

Anyway, here’s the code.

Read more…

Tags: ,

Maths

July 14th, 2011 No comments

For my current project, I’m doing some statistical analysis.  Part of that requires finding the mean and standard deviation for my collection of samples. 

Because of the data source, I need to use the formula for “Population Standard Deviation” which is a little different from the formula for “Standard Deviation.”  The denominator is “n” instead of “n – 1”.  The word from my client is that in sample sizes over 20 the difference between the two formulas is negligible.  Since we are running tests in a 96 well microplates, my sample count could be very low.  Especially when you factor in the multiple sample sources, calibration samples and quality control samples on each microplate.

Read more…

Tags: , ,

Setting a XamChart DataPoint marker through a behavior (MVVM)

June 17th, 2011 No comments

I’m finishing up a XamChart control.  The data points are created on the fly as a behavior attached to a XamDataGrid.  When the “active” record in the data grid changes, my behavior updates the chart to show the new record.  The binding is two-way on the data points so that the user can drag a data point thereby updating the XamDataGrid’s values. 

 

Still with me? 

 

More complications.

Some of the data points can be edited, while others cannot. 

By default, all of my markers are set to none.  This makes it hard to figure out if a data point can be dragged aka edited and it makes it hard to actually grad the point because it is so small.  To complicate matters, the existing style has animations.

Challenges:

  1. Find the existing Marker1 style in the resource.
  2. Assign separate styles based upon editablility.
  3. Make it easier to grab an editable data point.
    Can it be done?  Yeah.

    Read more…

    Who am I? (Determining the runtime user)

    May 29th, 2011 No comments

    (One of the last snippets from CodeKeep.)

    I’ve got a console app that I am using to test NHibernate.  The app fails to run because I can’t get connected to my local db.  After checking the current user at runtime, I realized there wasn’t any defined user which messes up my db connection. 

    Here is a work-around. 

    Read more…

    Creating a single instance application per machine.

    May 29th, 2011 No comments

    (Another snippet I originally posted to CodeKeep.)

    Read more…

    Reset RichTextBox selection

    May 29th, 2011 No comments

    (Another snippet I originally posted to CodeKeep.)

    After you finish modifying text within a RichTextBox control, you may want to move the cursor to the top of the control.

    Code:

    
    richTextBox1.SelectionStart = 0;
    richTextBox1.SelectionLength = 0;
    

     

    Modify text (visually) in a RichTextBox

    May 29th, 2011 No comments

    (Another snippet I originally posted to CodeKeep.)

    I needed to create a RichTextBox with a title, menu path, and description.  Here’s how I made customized the visuals to suit the project.  Title is enlarged and made bold.  Menu is reduced and made italics.  The description is ‘standard.’

    Code:

    Read more…

    Removing items from a typed list

    May 29th, 2011 No comments

    (Another snippet I originally posted to CodeKeep.)

    Code:

     
    var myList = new List<string>() { "one", "two", "three" }; 
    
    // print out the original list
    foreach (string item in myList)
      Console.WriteLine(item); // one two three
    
    myList.Remove("two");
    
    // print out the new list
    foreach (string item in myList)
      Console.WriteLine(item); // one three
    

     

    Notes:

    You cannot remove items from a list inside of a foreach.  You can set items to null, but you cannot remove the items.

    Tags: , ,

    Convert Enum to string

    May 29th, 2011 No comments

    (Another snippet I originally posted to CodeKeep.)

    
    public enum EFileType
    {
        Unknown = –1,
        Test,
        Production
    }
    
    ...
    
    var fileType = EFileType.Production;
    var str = Enum.GetName(typeof(EFileType),fileType); // str == 'Production'
    
    Tags: , ,

    Using C# to running command-line utility

    May 29th, 2011 No comments

    (Another snippet I originally posted to CodeKeep.)

    I needed to do a secure file copy to a remote server for an activity report.  I chose to use pscp from PuTTY.

    Read more…