K
K
Konstantin Shkilev2016-11-21 08:49:24
.NET
Konstantin Shkilev, 2016-11-21 08:49:24

Why use type casting in this case?

Good time of the day. Please tell me why VS says that it cannot cast the parameter to the byte type and if I use an explicit cast (byte), then there is no error. In the documentation, an enumeration of type byte, an argument too. What's wrong?76dd83f6f4a4403680261910dc7aa129.png834f7197e3544706a1e523256c555e06.PNG

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using UcanDotNET;

namespace ICAN
{
    public partial class Form1 : Form
    {
        //обертка для USBCAN32.DLL
        public USBcanServer m_USBcanInst;
        //инициализирована ли шина CAN
        public bool m_fIsInstInitialized = false;
        //номер устройства
        public byte m_DeviceNrInst = 255;
        //CAN сообщение, которое будет отправлено
        public USBcanServer.tCanMsgStruct m_canMsgStruct;
        //счетчик отправленных сообщений
        public int m_dwMsgCount = 0;

        public Form1()
        {
            InitializeComponent();
            //новый экземпляр сообщения CAN
            m_canMsgStruct = USBcanServer.tCanMsgStruct.CreateInstance(0x441, (byte)USBcanServer.eUcanMsgFrameFormat.USBCAN_MSG_FF_EXT);
            //длина кода 8 байт
            m_canMsgStruct.m_bDLC = 8;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //статус подключения к USB-CAN модулю
            byte bRet = 0;
            if(m_fIsInstInitialized != true)
            {
                //создание экземпляра класса обеспечивающего подключение
                m_USBcanInst = new USBcanServer();
                //инициализация устройства с соответствующим номером
                bRet = m_USBcanInst.InitHardware(USBcanServer.USBCAN_ANY_MODULE);
                if (bRet == (byte)USBcanServer.eUcanReturn.USBCAN_SUCCESSFUL)
                {
                    //инициализация канала CAN
                    bRet = m_USBcanInst.InitCan((byte)USBcanServer.eUcanChannel.USBCAN_CHANNEL_CH0, (short)USBcanServer.eUcanBaudrate.USBCAN_BAUD_1MBit, (int)USBcanServer.eUcanBaudrateEx.USBCAN_BAUDEX_USE_BTR01, (int)USBcanServer.USBCAN_AMR_ALL, (int)USBcanServer.USBCAN_ACR_ALL, (byte)USBcanServer.tUcanMode.kUcanModeNormal);
                }
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Shkilev, 2016-11-21
@shkilevk

The base type specifies how much storage is allocated for each enumerator. However, an explicit cast is required to convert from an enum type to an integral type. https://msdn.microsoft.com/en-us/library/sbbt4032.aspx

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question