I have some code that works fine but I currently have the same code three times, I'd like to refactor to a single function that is called from three places instead.

The current code has access to a List(Of Sample) where a Sample object contains a sample reading for three axes x, y & z . The code calculates the mean average of the samples and subtracts this from each sample before then calculating the RMS (root mean square) of a given axis from the sample List.

This code is executed separately for each axis e.g. just the X axis, as follows

vb Code:
  1. Public Function CalcRmsX() As Double
  2.     Dim Rms As Double = 0
  3.     Dim Mean As Double = 0
  4.     Dim Count As UInteger = _samples.Count()
  5.     If Count > 0 Then
  6.         Mean = _samples.Sum(Function(smpl) smpl.X) 'Take the sum of each sample
  7.         Mean = Mean / Count 'Divide for the mean
  8.         For Each smpl As Sample In _samples 'Subtract the mean from each sample to remove any bias
  9.             smpl.X = smpl.X - Mean
  10.         Next
  11.         Rms = _samples.Sum(Function(smpl) smpl.X ^ 2) 'Take the sum of the square of each sample
  12.         Rms = Rms / Count 'Divide for the mean square
  13.         Rms = Math.Sqrt(Rms) 'Take the sqare root of the mean square
  14.     End If
  15.     Return Rms
  16. End Function

I'd like to end up with a function where a function call would instead look as follows
[HIGHLIGHT=vb]
Dim RmsX As Double = CalcRms(X) 'Refactor to pass the axis as an argument like this
Dim RmsX As Double = CalcXRms() 'Rather than calling a different function per axis like this
[HIGHLIGHT=vb]

But my VB.NET foo is not strong enough to know how to change the function to make a function argument (the axis) select an object member (the Sample member). Is this possible?

Thanks, T