Friday, January 25, 2013

c# fixed position in strings

I had to update how the characters in a string were positioned.  Previously the fields in the string were variable length without any delimiters.

Like this:  LastName,FirstNameMrnAccountNumberRegDate

The requirements had changed. There was a need to have each field start in a know position and space fill any unused positions in that field.

Like this:  LastName, FirstName          MRN    AccountNumber    RegDate


I seem to remember in COBOL there was some kind of FILLER command.  Something along the lines of
FILLER(30) that would add 30 spaces.

Back to C#  I went about this using char arrays.  Each field would have a char array, the number of elements in each char array would be equal to the field size.  A method gets called to space fill the arrays. Next the string data is loaded element by element into the char arrays. This is easy code since a c# string is an array of characters.  The last step is to combine the char arrays into one string.  I used the string(char) constructor and the string.format methods to complete this.


        string LastName = "Clause";
        string FirstName = "Santa";
        string MRN = "0000007xx";
        string PatComCode = "378";
        DateTime RegDate = DateTime.Now;       
       
        string noteCode2 = string.Empty;
         
        string name = string.Format("{0}, {1}", LastName, FirstName);
 
        char[] nameArray = new char[29];
        spaceFillArray(ref nameArray);
 
        char[] mrnArray = new char[7];
        spaceFillArray(ref mrnArray);
 
        char[] acctArray = new char[9];
        spaceFillArray(ref acctArray);
 
        char[] regDtArray = new char[10];
        spaceFillArray(ref regDtArray);
 
        stringToArray(ref nameArray,name);
        stringToArray(ref mrnArray,MRN);
        stringToArray(ref acctArray,PatComCode);
        stringToArray(ref regDtArray,RegDate.ToString("MM/dd/yyyy"));        
 
 
        noteCode2 =  buidPatInfoString(nameArray,mrnArray,acctArray,regDtArray);



private void spaceFillArray(ref char[] info)
    {
        for (int i = 0; i < info.Length; i++)
        {
            info[i] = ' ';
        }
    }
 
 
    private void stringToArray(ref char[] info, string inString)
    {
        for(int i = 0; i < inString.Length; i++)
        {
            if (i == info.Length)
                break;
            
            info[i] = inString[i];
        }
    }
    
    
    private string buidPatInfoString(char[] name, char[] mrn, char[] acct, char[] regDt)
    {
        string ret = string.Empty;
 
        string partA = new string(name);
        string partB = new string(mrn);
        string partC = new string(acct);
        string partD = new string(regDt);
 
        ret = string.Format("{0}{1}{2}{3}",partA,partB,partC,partD);
 
        return ret; 
    }       

Saturday, January 19, 2013

Winter Riding


Today my bro SC and I rode Wompy.  Start time was 7 AM with the temps in low 30s F with high winds.

It was an awesome scenic blast though a winter land.  No ice and the ground was frozen hard, and there was still snow to look at along in the forest.

Incomplete list of winter riding tips:

  • Dress Warm Wear many layers.  Good socks and shoes, cover hands and ears, any skin exposed will be at risk of frost bite.
  • Ride with a buddy or in a group.  Always a risk to ride alone anytime of year, but especially risking in winter.
  • Leave enough time to complete your ride before sunset. Keeping in mind personal and mechanical failures can cause delays.
  • Try to find out what the trail conditions are like and prepare for these conditions.


Happy trails!




Friday, January 4, 2013

Bluetooth c# code sample

Here's a quick code sample of how to scan for Bluetooth devices and display some properties of that device.  This was deployed to a Motorola hand held device.  The framework is the Compact Framework and the Bluetooth functionality is made possible by the InTheHand.Net.Personal API.


using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using InTheHand.Net;
using InTheHand.Net.Sockets;
using InTheHand.Windows;

namespace SmartDeviceProject2
{
    public partial class Form1 : Form
    {
        private BluetoothClient _btc;
        private BluetoothDeviceInfo[] _peers;
       
       
        public Form1()
        {
            InitializeComponent();
            this.devicesListBox.Items.Clear();

            _btc = new BluetoothClient();
            _peers = _btc.DiscoverDevices();

            deviceInfoLbl.Text = string.Empty;
           
        }

        private void scanButton_Click(object sender, EventArgs e)
        {

            deviceInfoLbl.Text = string.Empty;

         
           
            this.devicesListBox.DisplayMember = "DeviceName";
            this.devicesListBox.ValueMember = null;
            this.devicesListBox.DataSource = _peers;




        }

        private void devicesListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            deviceInfoLbl.Text = getDeviceInfo(devicesListBox.SelectedIndex);
           
        }

        private string getDeviceInfo(int index)
        {
            string ret = string.Empty;


            try
            {              
                string connectedText = "No";
                if (_peers[index].Connected)
                    connectedText = "Yes";
               
                ret = string.Format("Device Name: {0}\nDevice Address: {1}\nDevice Class: {2}\nConnected: {3} ",
                    _peers[index].DeviceName,
                    _peers[index].DeviceAddress.ToString(),
                    _peers[index].ClassOfDevice.ToString(),
                    connectedText);
            }
            catch (Exception e)
            {
                deviceInfoLbl.Text = string.Empty;
                return ret;
            }


            return ret;

        }
    }
}





Tuesday, January 1, 2013

Winter Riding Blue Hills x2

For that last two weeks my buddy SC and I rode to the blue hills and rode some good stuff.  Got new Lake Winter Riding Boots too.  17 Miles.  Enjoyed the packed crunchy trails, with patches of ice.
Trails were a mix of some ice spots, crunchy ice, and flowy flow water over leaves.