C#: Enumeration

This entry is part 69 of 71 in the series C#

The enum keyword is used to declare an enumeration, a distinct type that consists of a set of constants called the enumerator list.

By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.

Enumerators can use initializers to override the default values, as shown in the following example.

enum Months {Jan=1, Feb, Mar, Apr, May, Jun, July };

using System;

namespace Enumeration
{
    public class EnumTest
    {
        enum Months {Jan, Feb, Mar, Apr, May, Jun, July };

        static void Main()
        {
            int x = (int)Months.Mar;
            int y = (int)Months.Jun;
            Console.WriteLine("Mar = {0}", x);
            Console.WriteLine("Months.Mar = {0}", Months.Mar);
            Console.WriteLine("Jun = {0}", y);
            Console.WriteLine("Months.Jun = {0}", Months.Jun);

            Console.ReadKey();
        }
    }
}

enum1

If we change the default value of the first enumerator.
using System;

namespace Enumeration
{
    public class EnumTest
    {
        enum Months {Jan = 1, Feb, Mar, Apr, May, Jun, July };

        static void Main()
        {
            int x = (int)Months.Mar;
            int y = (int)Months.Jun;
            Console.WriteLine("Mar = {0}", x);
            Console.WriteLine("Months.Mar = {0}", Months.Mar);
            Console.WriteLine("Jun = {0}", y);
            Console.WriteLine("Months.Jun = {0}", Months.Jun);

            Console.ReadKey();
        }
    }
}

The output will be:

enum2

Posted in

WPF: Master detail for a class with data

This entry is part 48 of 54 in the series Learn WPF

In the previous example, we have used master detail view to show the running processes in a PC.

For this example, we work out a real world Person class and on each click to the ListBox on the left, the Age of the person will be shown on the right.

MainWindow.xaml
<Window x:Class="MasterDetail.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition  />
        </Grid.ColumnDefinitions>
        <ListBox Name="lb" DisplayMemberPath="Name" />
        <Grid Grid.Column="1" TextBlock.FontSize="16" DataContext="{Binding SelectedItem, ElementName=lb}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <TextBlock Margin="6" Text="{Binding Name, StringFormat='Name: \{0\}'}" />
            <TextBlock Grid.Row="1" Margin="6" Text="{Binding Age, StringFormat='Age: 0'}" />
        </Grid>
        <GridSplitter Background="Blue" Width="2" HorizontalAlignment="Right" VerticalAlignment="Stretch" />
    </Grid>
</Window>

MainWindow.xaml.cs
using System.Collections.Generic;
using System.Windows;

namespace MasterDetail
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }

        List<Person> people = new List<Person> {
              new Person { Name = "Michelle", Age = 10 },
              new Person { Name = "Emily", Age = 45 },
              new Person { Name = "John", Age = 35 },
              new Person { Name = "Lisa", Age = 12 },
              new Person { Name = "Maggie", Age = 1 }
            };

        public MainWindow()
        {
            InitializeComponent();
            lb.ItemsSource = people;
        }
    }
}

As the left column master view is a ListBox, we build a Person class and create a few instances of the class and put the instances into a list.

masterdetaillist

 

C#: Try catch block

This entry is part 68 of 71 in the series C#

If an application handles exceptions that occur during the execution of a block of application code, the code must be placed within a try statement and is called a try block.

Application code that handles exceptions thrown by a try block is placed within a catch statement and is called a catch block.

Here using the previous example, we roll out a simple try catch exception block.

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace GetProcess
{
    /// <summary> 
    /// Shell for the sample. 
    /// </summary> 
    class MyProcess
    {
        void BindToRunningProcesses()
        {
            // Get the current process.
            Process currentProcess = Process.GetCurrentProcess();
            Console.WriteLine(currentProcess.ProcessName);

            try
            {
                Process[] localByName = Process.GetProcessesByName("notepad");
                Console.WriteLine(localByName[0].ProcessName);
            }
            catch
            {
                Console.WriteLine("Notepad not running");
            }


            // Get a process on the local computer, using the process id.  

            try
            {
                Process localById = Process.GetProcessById(6132);
                Console.WriteLine(localById.ProcessName);
            }
            catch
            {
                Console.WriteLine("Process id 6132 not running");
            }
        }

        static void Main()
        {
            MyProcess myProcess = new MyProcess();
            myProcess.BindToRunningProcesses();
            Console.ReadKey();
        }
    }
}

trycatch

C#: Checking if a process is running

This entry is part 67 of 71 in the series C#

For some server running process, we might actually have to monitor if a process.

If it is not running, we might actually need to restart the process.

We can check if a particular program is runing by using GetProcessesByName.  Here we check if notepad is running.

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace GetProcess
{
    /// <summary> 
    /// Shell for the sample. 
    /// </summary> 
    class MyProcess
    {
        void BindToRunningProcesses()
        {
            // Get the current process.
            Process currentProcess = Process.GetCurrentProcess();
            Console.WriteLine(currentProcess.ProcessName);


            // Get all instances of Notepad running on the local 
            // computer.
            Process[] localByName = Process.GetProcessesByName("notepad");
            Console.WriteLine(localByName[0].ProcessName);


            // Get a process on the local computer, using the process id.  
            // Change the ID according to the process ID running in your Desktop
             Process localById = Process.GetProcessById(6132);
             Console.WriteLine(localById.ProcessName);

        }

        static void Main()
        {

            MyProcess myProcess = new MyProcess();
            myProcess.BindToRunningProcesses();
            Console.ReadKey();

        }
    }
}

processname