# Calculating Median in VB 2005



## Charles_Bukowski (May 3, 2007)

Hello all:

We are trying to pull the median number from a selected text boxes. We need to be able to do this on the fly. The situation is like this:

User does 3 tests on material and will plug the results from the 3 tests into text boxes. The test results will not be in any numerical order (i.e. the first result could be 49, the second result could be 42, and the last result could be 55). We need to be able to look at those 3 numbers and be able to return the median number.

Any help on this would helpful as this has brought the project to a standstill.


----------



## JimmySeal (Sep 25, 2007)

This function will return the median of 3 numbers. It just puts them in a list, sorts it, and then returns the middle item.


```
Public Shared Function Median(ByVal Num1 As Double, ByVal Num2 As Double, ByVal Num3 As Double)
        Dim numList As New List(Of Double)
        numList.Add(Num1)
        numList.Add(Num2)
        numList.Add(Num3)
        numList.Sort()
        Return numList(1)
    End Function
```


----------



## Charles_Bukowski (May 3, 2007)

That worked great! Thank you for your help on this issue.


----------



## Charles_Bukowski (May 3, 2007)

This worked but I have a form that has 8 tabs on it that are all doing calculations. This isn't working on any of the tabs except for tab1. If I cut and paste something from another tab onto tab1 then it will calculate it too. When you move it to any other tab it does not work.


----------



## JimmySeal (Sep 25, 2007)

Is the code for different tabs located in different classes? If it is, you can access this function by making it _Public Shared_ (I've updated it above) and calling it with the classname first:


```
Dim med As Double = [I][COLOR="Red"]ClassNameHere[/COLOR][/I].Median(3.5, 4.2, 1)
```


----------



## Charles_Bukowski (May 3, 2007)

I am attaching a screen shot of the layout. We need to be able to get the median to be calculated on all of the tabs.


----------

