Int Array problems

fj1200

Member
Joined
Sep 1, 2010
Messages
13
Programming Experience
1-3
Hi, new to the list and fairly new to VB.Net. Hope this is the correct forum. Been banging my head over this for a few days - can anyone help please?

I am in the midst of writing a new app to read a serial densitometer (shows ink density values). The result comes out of the unit as a String, eg - V0.23Y0.91M0.84C0.45, the bit I want is the MAX value. I've replaced the alpha characters with a "," to put it into an array, then converted the array to an integer array (had to multiply the values by 100 otherwise they'd be decimals - I don't mind decimals, just more familiar with integers). So far it all works fine.

VB.NET:
        trimRead = Replace(densReading, "V", "")
        trimRead = Replace(trimRead, "C", ",")
        trimRead = Replace(trimRead, "M", ",")
        trimRead = Replace(trimRead, "Y", ",")

        convRead = Split(trimRead, ",")

        Dim cA As Integer = Convert.ToInt32(convRead(1) * 100)
        Dim mA As Integer = Convert.ToInt32(convRead(2) * 100)
        Dim yA As Integer = Convert.ToInt32(convRead(3) * 100)
        Dim kA As Integer = Convert.ToInt32(convRead(0) * 100)

       Dim arrRead() As Integer = New Integer(3) {cA, mA, yA, kA}

However - I want to extract the MAX value and discard the others, so using a very well-worn bit of code from t'interweb that I've seen used at least once on this forum:

VB.NET:
    Public Function MaxValOfIntArray(ByRef arrRead)
        'This function gives max value of int array without sorting an array
        Dim i
        Dim MaxIntegersIndex
        MaxIntegersIndex = 0
        For i = 0 To UBound(arrRead)
            If arrRead(i) > arrRead(MaxIntegersIndex) Then
                MaxIntegersIndex = i
            End If
        Next
        'index of max value is MaxValOfIntArray
        MaxValOfIntArray = arrRead(MaxIntegersIndex)
    End Function

Now I just get an error
"Value of type '1-dimensional array of Integer' cannot be converted to 'Integer'"

I've tried changing everything to a decimal, no good. Surely some of you boys and girls out there must be able to assist? I need the index reference as the 4 values refer to particular colours and so need to be in those positions - otherwise I'd just sort the array.

The value will eventually get written to a SQL Server DB and I did think of writing to a temp table there and doing the sorting that way but I'd prefer to keep it all on the one PC while it's working stuff out.

Any ideas, anyone?
 
Last edited:
Just use the LINQ extension method Max.

Here's a quick example.

VB.NET:
        Dim intArray = New Integer() {100, 300, 200, 150}
        Dim maxInteger = intArray.Max

If you want to take it further you can use Regular Expressions do the split for you without all of the Replaces and conversions you're doing and then have LINQ get your max value.

VB.NET:
        Dim reading As String = "V0.23Y0.91M0.84C0.45"
        Dim maxValue = Regex.Split(reading, "[VYMC]", RegexOptions.ExplicitCapture) _
                          .Where(Function(s) Not String.IsNullOrEmpty(s)) _
                          .Select(Function(x) CInt(100 * x)) _
                          .Max

Since you also need to find the index of the maximum value you can have the LINQ query return the Integer array then find the max value and its index.

VB.NET:
        Dim reading As String = "V0.23Y0.91M0.84C0.45"
        Dim intArray = Regex.Split(reading, "[VYMC]", RegexOptions.ExplicitCapture) _
                       .Where(Function(s) Not String.IsNullOrEmpty(s)) _
                       .Select(Function(x) CInt(100 * x)) _
                       .ToArray
        Dim max As Integer = intArray.Max
        Dim indexOfMax = Array.IndexOf(intArray, max)
 
Last edited:
Doesn't like the Regex statement but otherwise looks like it should work - however playing around with something like...

VB.NET:
        Dim arrRead = New Integer() {cA, mA, yA, kA} 
        Dim maxInteger = arrRead.Max

        Dim dValue
        Dim dIndex

        For Each n In arrRead.Select(Function(x, i) New With {.Index = i, .Item = x})
            If n.Item = maxInteger Then
                dIndex = n.Index
                dValue = n.Item
                lblMessage.Text = dIndex & " " & dValue
                <then write value and index to database, display on form, email to NASA, etc......>
            End If

(well,... ok, maybe not the email...)

        Next

but need to look at threading because I need to get the values into a graph and a text box or label on the form. Don't really understand how it works in VB yet - trying to understand Delegate().

Not looked at LINQ though - looks useful, a kind of SQL for VB.
 
Last edited:
You imported 'System.Text.RegularExpressions'?

Not sure why you're going through the For Each loop. You've already got the maximum value so just get the index of it.

VB.NET:
        Dim arrRead = New Integer() {cA, mA, yA, kA}
        Dim maxInteger = arrRead.Max
        Dim maxIndex = Array.IndexOf(arrRead, maxInteger)
 
MattP said:
.Select(Function(x) CInt(100 * x)) _
x is here a String value, so with option strict that needs; CDbl(x) or CSng(x)
 
You imported 'System.Text.RegularExpressions'?

erm...... nope.

That'll be it then.... DOH!!

Not sure why you're going through the For Each loop. You've already got the maximum value so just get the index of it.

Cos RegEx wasn't working...

cos I hadn't imported it...

etc etc...

Trying again....
 
Yup - that works.

Still tying to understand threading though as this is coming in through a COM port and I need to get the value into a text box and then do other stuff with it, but not sure how to appropriate delegates - can anyone assist?
 
Back
Top