Naraykln February 2016

Vb.net condition last digit check

RuGroup will be containing like this...

MA1,MA2,MA3,MA4,MA5,MA6,MA7,MA8,MA9

I've checked MA and the length. How can I check if 3rd digit is a number? cuz if value is stored as MAS, I need to display that. Please let me know how to use EndsWith property or something else in this..

getRetailUgrp = (From retailUnitGrp In RetailUnits Where Not (retailUnitGrp.RuGroupName.ToString.StartsWith("MA") And retailUnitGrp.RuGroupName.ToString.Length = 3)).ToList()

Answers


Tim Schmelter February 2016

You can use Char.IsDigit:

Dim invalids = From rug in RetailUnits 
               Where rug.RuGroupName.Length < 3 _
                 OrElse Not rug.RuGroupName.StartsWith("MA") _
                 OrElse Not Char.IsDigit(rug.RuGroupName(2))


Eric J. February 2016

If you are sure that each component is at least three characters long, you can do the following in C# (which should translate directly to VB.Net)

Char.IsDigit(retailUnitGrp.RuGroupName[2])

That will return true of the third digit (0-based index) is a number.


Monty February 2016

OK, well try someting like this.... it's winform code. Create a new winforms application and put 2 ComboBox controls on the form, then copy\paste the code below...

Imports System.Text.RegularExpressions

Public Class Form1

Dim RetailUnits As New List(Of RetailUnit)

Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
    Dim getRetailUgrp1 As New List(Of RetailUnit)
    Dim regex1 As Regex = New Regex("")
    If DirectCast(sender, System.Windows.Forms.ComboBox).SelectedItem < 3 Then
        regex1 = New Regex("^([A-Z]{2,3})+$")
    Else
        regex1 = New Regex("^([A-Z]{2}[0-9]{1})+$")
    End If
    Try
        ComboBox2.DataSource = (From retailUnitGrp In RetailUnits Where (retailUnitGrp.RuGroupName.ToString = regex1.Match(retailUnitGrp.RuGroupName.ToString).Value)).ToList()
        ComboBox2.DisplayMember = "RuGroupName"
    Catch ex As Exception

    End Try
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA1"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA2"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA3"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA4"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA5"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA6"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA7"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA8"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA9"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "AU"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "AUW"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "AS"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MAR"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = " 


Naraykln February 2016

Finally I used this.

If CBool(IIf(cboCountrySpec.SelectedValue.Equals(PIAConstants.CountrySpecOne), True, False)) OrElse _
                CBool(IIf(cboCountrySpec.SelectedValue.Equals(PIAConstants.CountrySpecTwo), True, False)) Then
                'getRetailUgrp = (From retailUnitGrp In RetailUnits Where Not retailUnitGrp.RuGroupName.ToString.Contains("MA")).ToList()                
                getRetailUgrp = (From retailUnitGrp In RetailUnits Where Not (retailUnitGrp.RuGroupName.ToString.StartsWith("MA") And retailUnitGrp.RuGroupName.ToString.Length = 3 And ((retailUnitGrp.RuGroupName.EndsWith("1")) Or (retailUnitGrp.RuGroupName.EndsWith("2")) Or
                (retailUnitGrp.RuGroupName.EndsWith("3")) Or (retailUnitGrp.RuGroupName.EndsWith("4")) Or (retailUnitGrp.RuGroupName.EndsWith("5")) Or (retailUnitGrp.RuGroupName.EndsWith("6")) Or (retailUnitGrp.RuGroupName.EndsWith("7")) Or (retailUnitGrp.RuGroupName.EndsWith("8")) Or
                (retailUnitGrp.RuGroupName.EndsWith("9"))))) 'IKEA00879697
            Else
                'getRetailUgrp = (From retailUnitGrp In RetailUnits Where retailUnitGrp.RuGroupName.ToString.Contains("MA")).ToList()                
                getRetailUgrp = (From retailUnitGrp In RetailUnits Where (retailUnitGrp.RuGroupName.ToString.StartsWith("MA") And retailUnitGrp.RuGroupName.ToString.Length = 3 And ((retailUnitGrp.RuGroupName.EndsWith("1")) Or (retailUnitGrp.RuGroupName.EndsWith("2")) Or
                (retailUnitGrp.RuGroupName.EndsWith("3")) Or (retailUnitGrp.RuGroupName.EndsWith("4")) Or (retailUnitGrp.RuGroupName.EndsWith("5")) Or (retailUnitGrp.RuGroupName.EndsWith("6")) Or (retailUnitGrp.RuGroupName.EndsWith("7")) Or (retailUnitGrp.RuGroupName.EndsWith("8")) Or
                (retailUnitGrp.RuGroupName.EndsWith("9"))))) 'IKEA00879697
            End If

Post Status

Asked in February 2016
Viewed 2,681 times
Voted 14
Answered 4 times

Search




Leave an answer