我的if语句不起作用!请帮忙!!!! [英] My if statement wont work!! please help!!!!

查看:79
本文介绍了我的if语句不起作用!请帮忙!!!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个if语句这将使您可以选择使用主子事件中的过程或分配器私人事件中的过程.这 确定将取决于发送消息evnt处理程序中的字符串.在这种情况下,将其设置为"dis".这应该使其进入分配器事件处理程序.分配器和主要事件非常相似.主要区别是 它解析出与main sub相同的变量,但起始索引和长度将不同.目前我正在   NullreferenceException已得到处理,提示内容为 对象引用未设置为对象的实例.我需要一个简单的代码,使该两车道高速公路"成为可能代码.

这就是我所拥有的远.

导入ANDI_AXC_LIBLib

导入系统

导入System.IO

导入System.Text

导入System.Text.RegularExpressions

导入System.Net

导入System.Collections.Generic

导入System.Timers

Module Module1

; 公共WithEvents ANDI为ANDI_AXC_LIBLib.ANDI_AXC

; 公共WithEvents ANDI_LOOP为ANDI_AXC_LIBLib.ANDI_AXC

; 公开返回的消息为字符串

; Sub Main()

;    'ANDI库访问

;    ANDI =新的ANDI_AXC

;    控制台的IP列表

;    将addys作为新列表(字符串)

;    addys.Add("10.0.99.139")

;    addys.Add("10.0.99.138")

;    addys.Add("10.0.99.181")

;    对于每个爸爸来说都是字符串

;        使用ANDI

;             .DeviceClass ='POS01'

;                                .ServiceClass ="DGS"

;                              .Mode = ANDI_AXC_LIBLib._COMM_MODE.ETHERNET

;             .CommPort = 1

;             .EthernetPort = 6627

;             .BaudRate = 9600

;             .DataBits = 8

;                              .Parity = ANDI_AXC_LIBLib._PARITY_MODE.NO_PARITY

                                      .StopBits = 2

;        结尾为

;        将MyResult设置为ANDI_AXC_LIBLib.CONN_OP_RESULT'从Open返回的值(在类型库中找到)

;        ANDI.DeviceID = addy

;        ANDI.Close()

;        '我们已经连接好了吗?

;        如果ANDI.Connected然后

;             '关闭连接.

;             调用ANDI.Close()

;        其他

;        如果结束

;        '打开连接.

;        MyResult = ANDI.Open()

;        MsgBox(MyResult.ToString())

;        将制造商昏暗为字符串

;        将系统设置为String

;        作为字符串的暗淡版本

;        Dim软件As String

;        以字符串形式将连接昏暗

;        将逻辑昏暗为字符串

            Dim DClass As String

;        将ID设为字符串

;        将ServiceCount设置为字符串

;        昏暗服务以字符串形式提供

;        如果MyResult = ANDI_AXC_LIBLib._CONN_OP_RESULT.CONN_OP_FAILED然后

;             Console.WriteLine(ANDI.StatusMessage)

;             Console.WriteLine(您未连接")

;        其他

;             Console.WriteLine(按Enter键以连接到联盟网络")

;        如果结束

;        SendMessage("DIS00 S00",2)

;        Console.ReadLine()

;        致电ANDI.Open()

;        Console.WriteLine(您在以下位置连接到NexGen:& ANDI.DeviceID)

;        MsgBox(您已连接!!!")

;        Console.WriteLine(")

;        MAnufacturer = returnMessage.Substring(9,20)

;        Console.WriteLine("Manufacturer =" + MAnufacturer)

;        设备名称"

;        system = returnMessage.Substring(29,15)

;        Console.WriteLine("设备名称=" +系统)

;        版本名称

;        version = returnMessage.Substring(44,5)

;        Console.WriteLine(" Version =" +版本)

;        软件标识

;        software =返回的Message.Substring(49,20)

;        Console.WriteLine("Software =" +软件)

;        连接状态"

;        connecttion =返回的Message.Substring(69,1)

;        Console.WriteLine("连接状态=" +连接)

;        '逻辑状态

;        logical = returnMessage.Substring(70,1)

;        Console.WriteLine("逻辑状态=" +逻辑)

;        '设备类 

;        DClass = returnMessage.Substring(71,3)

;        Console.WriteLine("Device Class =" + DClass)

;        'ID

;        ID =返回的Message.Substring(74,2)

;        Console.WriteLine(" ID =" + ID)

;        '提供的服务数量

;        ServiceCount = returnMessage.Substring(76,2)

;        Console.WriteLine("服务数量=" + ServiceCount)

;        '提供的服务

;        servicesupplied = returnMessage.Substring(78)

;        将sb作为新的System.Text.StringBuilder

;        对于SplitIntoSets中的每个字符串(servicesupplied,3)

;             sb.AppendLine(s)

;         下一步

;        Console.WriteLine(服务已提供")

;        Console.WriteLine(" ---------------------")

;        Console.WriteLine(sb.ToString)

;    下一个

;    Console.ReadLine()

; 结束子

; '提供的服务根据ServiceCount编号解析为3个字符的集合

; 公共函数SplitIntoSets(ByVal源为字符串,ByVal setSize为整数)作为String()

;    将setCount设置为Integer = CInt(Math.Ceiling(source.Length/setSize))

;    昏暗的结果(setCount-1)为字符串

;    对于i As Integer = 0,要设置setCount-1

;        如果i = setCount-1然后setSize = source.Length-(((setCount-1)* setSize)

;        result(i)= source.Substring(i * setSize,setSize)

;    下一个

;    返回结果

; 结束功能

;

Private Sub SendMessage(ByVal消息为字符串) ,将ByVal目标设置为整数)

;    将MyResult设为布尔值

;    '我们连接了吗?

;    如果没有ANDI.Connected,则退出Sub'Nope.

;   

;    '用户输入了什么吗?

;    如果message ="然后退出Sub'Nope.

        如果消息包含("DIS00"),则<< ;这是我的DIS字符串,它将使它进入分配器事件

            Dispenser()

;   如果结束

;    '发送文本框的内容.

;    MyResult = ANDI.SendMessage((message))

;    'System.Threading.Thread.Sleep(1000)

        Console.WriteLine("result: " & MyResult)

        'MsgBox("my result: " & MyResult)

        ' How did we do?

        If MyResult Then

            If target = 1 Then

                Console.WriteLine("Message Sent 1")

            Else

                Console.WriteLine("Message Sent else")

            End If

        Else

            Beep()

            If target = 1 Then

                Console.WriteLine(ANDI.StatusMessage)

            Else

                Console.WriteLine(ANDI.StatusMessage)

            End If

        End If

    End Sub

   

   

解决方案

 Private Sub Dispenser()<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<dispenser sub event

        ' ANDI Library access

        ANDI = New ANDI_AXC

        'IP list for the console

        Dim addys As New List(Of String)

        addys.Add("10.0.99.139")

        addys.Add("10.0.99.138")

        addys.Add("10.0.99.181")

        For Each addy As String In addys

            With ANDI

                .DeviceClass = "POS01"

                .ServiceClass = "DGS"

                .Mode = ANDI_AXC_LIBLib._COMM_MODE.ETHERNET

                .CommPort = 1

                .EthernetPort = 6627

                .BaudRate = 9600

                .DataBits = 8

                .Parity = ANDI_AXC_LIBLib._PARITY_MODE.NO_PARITY

                .StopBits = 2

            End With

            Dim MyResult As ANDI_AXC_LIBLib.CONN_OP_RESULT ' The return value from Open (found in the type library)

            ANDI.DeviceID = addy

            ANDI.Close()

            ' Are we already connected?

            If ANDI.Connected Then

                ' Close the connection.

                Call ANDI.Close()

            Else

            End If

            ' Open the connection.

            MyResult = ANDI.Open()

            MsgBox(MyResult.ToString())

            Dim ManDis As String

            Dim system As String

            Dim version As String

            Dim software As String

            Dim connecttion As String

            Dim logical As String

            Dim DClass As String

            Dim ID As String

            Dim ServiceCount As String

            Dim servicesupplied As String

            If MyResult = ANDI_AXC_LIBLib._CONN_OP_RESULT.CONN_OP_FAILED Then

                Console.WriteLine(ANDI.StatusMessage)

                Console.WriteLine("you are not connected")

            Else

                Console.WriteLine("Press enter to connect to the Allied network")

            End If

            SendMessage("DIS00 S00", 2)

            Console.ReadLine()

            Call ANDI.Open()

            Console.WriteLine("You are connected to the NexGen at: " & ANDI.DeviceID)

            MsgBox("You are connected!!!!")

            Console.WriteLine("")

            MAnufacturer = returnedMessage.Substring(9, 20)

            Console.WriteLine("Manufacturer = " + MAnufacturer )

            'Device Name

            system = returnedMessage.Substring(29, 15)

            Console.WriteLine("Device Name = " + system)

            'Version name

            version = returnedMessage.Substring(44, 5)

            Console.WriteLine("Version = " + version)

            'Software Identification

            software = returnedMessage.Substring(49, 20)

            Console.WriteLine("Software = " + software)

            'Connection Status

            connecttion = returnedMessage.Substring(69, 1)

            Console.WriteLine("Connection Status = " + connecttion)

            'Logical Status

            logical = returnedMessage.Substring(70, 1)

            Console.WriteLine("Logical Status = " + logical)

            'Device Class 

            DClass = returnedMessage.Substring(71, 3)

            Console.WriteLine("Device Class = " + DClass)

            'ID

            ID = returnedMessage.Substring(74, 2)

            Console.WriteLine("ID = " + ID)

            ' number of services supplied

            ServiceCount = returnedMessage.Substring(76, 2)

            Console.WriteLine("Number of Service = " + ServiceCount)

            'services Supplied

            servicesupplied = returnedMessage.Substring(78)

            Dim sb As New System.Text.StringBuilder

            For Each s As String In SplitIntoSets(servicesupplied, 3)

                sb.AppendLine(s)

            Next

            Console.WriteLine("Services Suppled ")

            Console.WriteLine("---------------------")

            Console.WriteLine(sb.ToString)

        Next

        Console.ReadLine()

    End Sub

End Module



I would like to make an if statement that will give an option to either use the procedures in the main sub event or the dispenser private event. the determination would depend on the string in the send message evnt handler. in this case it is set to "dis" this should make it go to the dispenser event handler. the dispenser and main event are very similar. the major difference is that it parses out the same variable as the main sub but the starting index and length will be different. currently i am getting a  NullreferenceException was handled prompt that says Object reference not set to an instance of an object. I need a simple code that will make this "two lane Highway" of code.

 this is what I have so far.

Imports ANDI_AXC_LIBLib

Imports System

Imports System.IO

Imports System.Text

Imports System.Text.RegularExpressions

Imports System.Net

Imports System.Collections.Generic

Imports System.Timers

Module Module1

    Public WithEvents ANDI As ANDI_AXC_LIBLib.ANDI_AXC

    Public WithEvents ANDI_LOOP As ANDI_AXC_LIBLib.ANDI_AXC

    Public returnedMessage As String

    Sub Main()

        ' ANDI Library access

        ANDI = New ANDI_AXC

        'IP list for the console

        Dim addys As New List(Of String)

        addys.Add("10.0.99.139")

        addys.Add("10.0.99.138")

        addys.Add("10.0.99.181")

        For Each addy As String In addys

            With ANDI

                .DeviceClass = "POS01"

                .ServiceClass = "DGS"

                .Mode = ANDI_AXC_LIBLib._COMM_MODE.ETHERNET

                .CommPort = 1

                .EthernetPort = 6627

                .BaudRate = 9600

                .DataBits = 8

                .Parity = ANDI_AXC_LIBLib._PARITY_MODE.NO_PARITY

                .StopBits = 2

            End With

            Dim MyResult As ANDI_AXC_LIBLib.CONN_OP_RESULT ' The return value from Open (found in the type library)

            ANDI.DeviceID = addy

            ANDI.Close()

            ' Are we already connected?

            If ANDI.Connected Then

                ' Close the connection.

                Call ANDI.Close()

            Else

            End If

            ' Open the connection.

            MyResult = ANDI.Open()

            MsgBox(MyResult.ToString())

            Dim MAnufacturer As String

            Dim system As String

            Dim version As String

            Dim software As String

            Dim connecttion As String

            Dim logical As String

            Dim DClass As String

            Dim ID As String

            Dim ServiceCount As String

            Dim servicesupplied As String

            If MyResult = ANDI_AXC_LIBLib._CONN_OP_RESULT.CONN_OP_FAILED Then

                Console.WriteLine(ANDI.StatusMessage)

                Console.WriteLine("you are not connected")

            Else

                Console.WriteLine("Press enter to connect to the Allied network")

            End If

            SendMessage("DIS00 S00", 2)

            Console.ReadLine()

            Call ANDI.Open()

            Console.WriteLine("You are connected to the NexGen at: " & ANDI.DeviceID)

            MsgBox("You are connected!!!!")

            Console.WriteLine("")

            MAnufacturer = returnedMessage.Substring(9, 20)

            Console.WriteLine("Manufacturer = " + MAnufacturer)

            'Device Name

            system = returnedMessage.Substring(29, 15)

            Console.WriteLine("Device Name = " + system)

            'Version name

            version = returnedMessage.Substring(44, 5)

            Console.WriteLine("Version = " + version)

            'Software Identification

            software = returnedMessage.Substring(49, 20)

            Console.WriteLine("Software = " + software)

            'Connection Status

            connecttion = returnedMessage.Substring(69, 1)

            Console.WriteLine("Connection Status = " + connecttion)

            'Logical Status

            logical = returnedMessage.Substring(70, 1)

            Console.WriteLine("Logical Status = " + logical)

            'Device Class 

            DClass = returnedMessage.Substring(71, 3)

            Console.WriteLine("Device Class = " + DClass)

            'ID

            ID = returnedMessage.Substring(74, 2)

            Console.WriteLine("ID = " + ID)

            ' number of services supplied

            ServiceCount = returnedMessage.Substring(76, 2)

            Console.WriteLine("Number of Service = " + ServiceCount)

            'services Supplied

            servicesupplied = returnedMessage.Substring(78)

            Dim sb As New System.Text.StringBuilder

            For Each s As String In SplitIntoSets(servicesupplied, 3)

                sb.AppendLine(s)

            Next

            Console.WriteLine("Services Suppled ")

            Console.WriteLine("---------------------")

            Console.WriteLine(sb.ToString)

        Next

        Console.ReadLine()

    End Sub

    ' Services supplied parse out into their sets of 3 characters based on the ServiceCount number

    Public Function SplitIntoSets(ByVal source As String, ByVal setSize As Integer) As String()

        Dim setCount As Integer = CInt(Math.Ceiling(source.Length / setSize))

        Dim result(setCount - 1) As String

        For i As Integer = 0 To setCount - 1

            If i = setCount - 1 Then setSize = source.Length - ((setCount - 1) * setSize)

            result(i) = source.Substring(i * setSize, setSize)

        Next

        Return result

    End Function

   

Private Sub SendMessage(ByVal message As String, ByVal target As Integer)

        Dim MyResult As Boolean

        ' Are we connected?

        If Not ANDI.Connected Then Exit Sub ' Nope.

       

        ' Did the user type anything?

        If message = "" Then Exit Sub ' Nope.

        If message.Contains("DIS00") Then <<<<<<<<<<<<<<<<<<<<this is my DIS string that will make it go to the dispenser event

            Dispenser()

        End If

        ' Send the contents of the text box.

        MyResult = ANDI.SendMessage((message))

        'System.Threading.Thread.Sleep(1000)

        Console.WriteLine("result: " & MyResult)

        'MsgBox("my result: " & MyResult)

        ' How did we do?

        If MyResult Then

            If target = 1 Then

                Console.WriteLine("Message Sent 1")

            Else

                Console.WriteLine("Message Sent else")

            End If

        Else

            Beep()

            If target = 1 Then

                Console.WriteLine(ANDI.StatusMessage)

            Else

                Console.WriteLine(ANDI.StatusMessage)

            End If

        End If

    End Sub

   

   

解决方案

 Private Sub Dispenser()<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<dispenser sub event

        ' ANDI Library access

        ANDI = New ANDI_AXC

        'IP list for the console

        Dim addys As New List(Of String)

        addys.Add("10.0.99.139")

        addys.Add("10.0.99.138")

        addys.Add("10.0.99.181")

        For Each addy As String In addys

            With ANDI

                .DeviceClass = "POS01"

                .ServiceClass = "DGS"

                .Mode = ANDI_AXC_LIBLib._COMM_MODE.ETHERNET

                .CommPort = 1

                .EthernetPort = 6627

                .BaudRate = 9600

                .DataBits = 8

                .Parity = ANDI_AXC_LIBLib._PARITY_MODE.NO_PARITY

                .StopBits = 2

            End With

            Dim MyResult As ANDI_AXC_LIBLib.CONN_OP_RESULT ' The return value from Open (found in the type library)

            ANDI.DeviceID = addy

            ANDI.Close()

            ' Are we already connected?

            If ANDI.Connected Then

                ' Close the connection.

                Call ANDI.Close()

            Else

            End If

            ' Open the connection.

            MyResult = ANDI.Open()

            MsgBox(MyResult.ToString())

            Dim ManDis As String

            Dim system As String

            Dim version As String

            Dim software As String

            Dim connecttion As String

            Dim logical As String

            Dim DClass As String

            Dim ID As String

            Dim ServiceCount As String

            Dim servicesupplied As String

            If MyResult = ANDI_AXC_LIBLib._CONN_OP_RESULT.CONN_OP_FAILED Then

                Console.WriteLine(ANDI.StatusMessage)

                Console.WriteLine("you are not connected")

            Else

                Console.WriteLine("Press enter to connect to the Allied network")

            End If

            SendMessage("DIS00 S00", 2)

            Console.ReadLine()

            Call ANDI.Open()

            Console.WriteLine("You are connected to the NexGen at: " & ANDI.DeviceID)

            MsgBox("You are connected!!!!")

            Console.WriteLine("")

            MAnufacturer = returnedMessage.Substring(9, 20)

            Console.WriteLine("Manufacturer = " + MAnufacturer )

            'Device Name

            system = returnedMessage.Substring(29, 15)

            Console.WriteLine("Device Name = " + system)

            'Version name

            version = returnedMessage.Substring(44, 5)

            Console.WriteLine("Version = " + version)

            'Software Identification

            software = returnedMessage.Substring(49, 20)

            Console.WriteLine("Software = " + software)

            'Connection Status

            connecttion = returnedMessage.Substring(69, 1)

            Console.WriteLine("Connection Status = " + connecttion)

            'Logical Status

            logical = returnedMessage.Substring(70, 1)

            Console.WriteLine("Logical Status = " + logical)

            'Device Class 

            DClass = returnedMessage.Substring(71, 3)

            Console.WriteLine("Device Class = " + DClass)

            'ID

            ID = returnedMessage.Substring(74, 2)

            Console.WriteLine("ID = " + ID)

            ' number of services supplied

            ServiceCount = returnedMessage.Substring(76, 2)

            Console.WriteLine("Number of Service = " + ServiceCount)

            'services Supplied

            servicesupplied = returnedMessage.Substring(78)

            Dim sb As New System.Text.StringBuilder

            For Each s As String In SplitIntoSets(servicesupplied, 3)

                sb.AppendLine(s)

            Next

            Console.WriteLine("Services Suppled ")

            Console.WriteLine("---------------------")

            Console.WriteLine(sb.ToString)

        Next

        Console.ReadLine()

    End Sub

End Module



这篇关于我的if语句不起作用!请帮忙!!!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆