C# Exception Class Template

by bill 11/21/2006 8:26:00 AM

Download the ExceptionClass.zip (1.53 kb) and place it in [Drive]:\Documents and Settings\[User]\My Documents\Visual Studio 2005\Templates\ItemTemplates\Visual C#\.

The next time you go to add a file, ExceptionClass should be in your "My Templates" section at the bottom.

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Simple ClickOnce Utils

by bill 11/8/2006 7:59:00 PM

Two simple methods for ClickOnce deployed applications.

 

public static class ClickOnce
{
    /// <summary>
    /// Determines whether this instance is deployed.
    /// </summary>
    /// <returns>
    ///     <c>true</c> if this instance is deployed; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsDeployed()
    {
        return (AppDomain.CurrentDomain.ActivationContext != null && ApplicationDeployment.IsNetworkDeployed);
    }

    /// <summary>
    /// Parses the query string.
    /// </summary>
    /// <returns><see cref="NameValueCollection"/> containing the key/value pairs from the ActivationUri if they are found; otheriwise an empty <see cref="NameValueCollection"/>.</returns>
    public static NameValueCollection ParseQueryString()
    {
        if (!IsDeployed())
            return new NameValueCollection();

        Uri uri = ApplicationDeployment.CurrentDeployment.ActivationUri;
        if (uri != null)
        {
            NameValueCollection parms = HttpUtility.ParseQueryString(uri.Query);
            if (parms.Count != 0)
                return parms;
        }

        return new NameValueCollection();
    }
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Enum Helper Class Using Generics

by bill 11/1/2006 8:23:00 PM

A week or so ago I came across this post in microsoft.public.dotnet.languages.csharp  asking about using Generics to make dealing with bitwise operations on enums that have the FlagsAttribute.  I was thinking that even though you can't use an Enum as Generic Constraint you can probably fudge it just enough to make a usefull class. So I posted my answer to the question and then decided to write my Enum<T> class. Yes, I actually named the class Enum<T>.

Enum.cs (17.12 kb)

public class Class1
{
    [Flags]
    public enum FlaggedEnum
    {
        None = 0,
        Red = 1,
        Blue = 2,
        White = 4,
        Black = 8,
        Green = 16
    }

    public enum MyTestEnum
    {
        [Description("This is a description")]
        Val0,
        Val1,
        Val2
    }

    static void Main(string[] args)
    {
        MyTestEnum a;
        Enum<MyTestEnum>.TryParse("Val1", out a);
        Debug.WriteLine(a); // output: Val1

        bool returnValue = Enum<MyTestEnum>.TryParse("Val3", out a);
        Debug.WriteLine(returnValue); // output: False

        MyTestEnum? b;
        Enum<MyTestEnum>.TryParse("Val2", out b);
        Debug.WriteLine(b); // output: Val2

        Enum<MyTestEnum>.TryParse("Val3", out b);
        Debug.WriteLine(b); // output: nothing

        //Enum<MyTestEnum>.Flags.IsFlagSet(a, MyTestEnum.Val2); // raises an assert by design
        //int test;
        //Enum<int>.TryParse("hello", out test); // raises an assert by design




        // Flags Methods
        FlaggedEnum flagged = FlaggedEnum.Red | FlaggedEnum.Blue | FlaggedEnum.White;
        Debug.WriteLine(flagged); // output: Red, Blue, White

        flagged = Enum<FlaggedEnum>.Flags.SwapFlag(flagged, FlaggedEnum.Red);
        Debug.WriteLine(flagged); // output: Blue, White

        flagged = Enum<FlaggedEnum>.Flags.SwapFlag(flagged, FlaggedEnum.Red);
        Debug.WriteLine(flagged); // output: Red, Blue, White

        flagged = Enum<FlaggedEnum>.Flags.ClearFlag(flagged, FlaggedEnum.White);
        Debug.WriteLine(flagged); // output: Red, Blue

        flagged = Enum<FlaggedEnum>.Flags.SetFlag(flagged, FlaggedEnum.White);
        Debug.WriteLine(flagged); // output: Red, Blue, White


        // using the ref overrides
        Enum<FlaggedEnum>.Flags.SwapFlag(ref flagged, FlaggedEnum.White);
        Debug.WriteLine(flagged); // output: Red, Blue

        Enum<FlaggedEnum>.Flags.SetFlag(ref flagged, FlaggedEnum.White);
        Debug.WriteLine(flagged); // output: Red, Blue, White


        IList<int> values = Enum<FlaggedEnum>.GetValues<int>();

        string name = Enum<FlaggedEnum>.GetName(2);
        Debug.WriteLine(name); // output: Blue


        DescriptionAttribute attrib = Enum<MyTestEnum>.Reflection.GetAttributeOnValue<DescriptionAttribute>(MyTestEnum.Val0);
        Debug.WriteLine(attrib.Description); // output: This is a description



        // For WinForms DataBinding
        IList<IBindableEnum<MyTestEnum>> list = Enum<MyTestEnum>.DataBinding.CreateList();
        foreach( IBindableEnum<MyTestEnum> bindable in list )
            Debug.WriteLine(String.Format("{0} : {1}", bindable.Name, bindable.Value));
        // output:
        //  Val0 : Val0
        //  Val1 : Val1
        //  Val2 : Val2


        list = Enum<MyTestEnum>.DataBinding.CreateList
        (
            delegate(MyTestEnum target)
            {
                return target.ToString().Replace("Val", "Value ");
            }
        );
        foreach (IBindableEnum<MyTestEnum> bindable in list)
            Debug.WriteLine(String.Format("{0} : {1}", bindable.Name, bindable.Value));
        // output:
        //  Value 0 : Val0
        //  Value 1 : Val1
        //  Value 2 : Val2
    }

 

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Powered by BlogEngine.NET 1.3.1.0
Theme by Mads Kristensen

About the author

Name of author Bill Rodenbaugh
I bang on the keyboard and somehow the end result turns into some kind of thing that sometimes does something.

E-mail me Send mail
View Bill Rodenbaugh's profile on LinkedIn

Calendar

<<  August 2008  >>
MoTuWeThFrSaSu
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

View posts in large calendar

Recent comments

Authors

Categories

None


Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in