前贴有问题故重发:Madicine.canMessage.getMessage is a field but used as a type

ellaxy 2011-08-18 09:08:57
在做一个程序,用到CAN-BUS信号,因为要频繁用到结构体TPCANMSG,结构体的不同的实例有着自己固定的参数设定,所以想自建一个类库,以后直接调用实例,就不用每次都new然后设置参数了,结果出现标题中的错误信息。
请问如何才能在自建类文件中不但实例化结构体,设置结构体变量参数,而且能在程序的其他文件中调用,如直接引用下面定义的getMessage。
谢谢各位!

我的自建步骤如下:

//1.在madicine 项目中创建一个类的cs文件:CanMessage.cs
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;
using Peak.Can.Basic;//2.引用包含有结构体TPCANMSG初始定义的文件
using TPCANHandle = System.Byte;

namespace Madicine.CanMessage
{
class CanMessage
{
//实例化结构体TPANMSG
public const TPCANMsg getMessage = new TPCANMsg();
string idText = "00350000";
int msgLength = 8;
// We configurate the Message. The ID (max 0x1FF),
// Length of the Data, Message Type and die data
//
//下面的头两条信息出错,显示标题中的错误信息
getMessage.DATA = new byte[8] { 0x42, 0x01, 0, 0, 0, 0, 0, 0 };
getMessage.ID = Convert.ToUInt32(idText, 16);
getMessage.LEN = Convert.ToByte(msgLength);
getMessage.MSGTYPE = TPCANMessageType.PCAN_MESSAGE_EXTENDED;
}
}


Peak.Can.Basic中TPCANMSG结构体定义为以下代码

public struct TPCANMsg
{
/// <summary>
/// 11/29-bit message identifier
/// </summary>
public uint ID;
/// <summary>
/// Type of the message
/// </summary>
[MarshalAs(UnmanagedType.U1)]
public TPCANMessageType MSGTYPE;
/// <summary>
/// Data Length Code of the message (0..8)
/// </summary>
public byte LEN;
/// <summary>
/// Data of the message (DATA[0]..DATA[7])
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] DATA;
}

...全文
86 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ellaxy 2011-08-19
  • 打赏
  • 举报
回复
已经找到原因了,static constructor,然后static getMessage就可以了,谢谢ojlovecd及各位的建议。
ellaxy 2011-08-19
  • 打赏
  • 举报
回复
为什么外部引用后数据为0:
messagebox.show(Madicine.CanMessage.CanMessage.getMessage.ID.tostring())显示为0.数据没有传递进去?
LMAOhuaNL 2011-08-18
  • 打赏
  • 举报
回复
你把那个const 删掉就ok
我姓区不姓区 2011-08-18
  • 打赏
  • 举报
回复
getMessage 是CanMessage的一个字段,
getMessage.DATA = new byte[8] { 0x42, 0x01, 0, 0, 0, 0, 0, 0 };
getMessage.ID = Convert.ToUInt32(idText, 16);
getMessage.LEN = Convert.ToByte(msgLength);
getMessage.MSGTYPE = TPCANMessageType.PCAN_MESSAGE_EXTENDED;
这些赋值语句不能写在“类”这一级的范围内,而只能写在方法内
如果你是vs2008或以上的版本,可以改成这样:

namespace Madicine.CanMessage
{
class CanMessage
{
string idText = "00350000";
int msgLength = 8;
//实例化结构体TPANMSG
public const TPCANMsg getMessage = new TPCANMsg()
{
DATA = new byte[8] { 0x42, 0x01, 0, 0, 0, 0, 0, 0 },
ID = Convert.ToUInt32(idText, 16),
LEN = Convert.ToByte(msgLength),
MSGTYPE = TPCANMessageType.PCAN_MESSAGE_EXTENDED
};

// We configurate the Message. The ID (max 0x1FF),
// Length of the Data, Message Type and die data
//
}
}

否则,你的赋值语句要写在构造函数内:

class CanMessage
{
//实例化结构体TPANMSG
public const TPCANMsg getMessage = new TPCANMsg();
string idText = "00350000";
int msgLength = 8;
// We configurate the Message. The ID (max 0x1FF),
// Length of the Data, Message Type and die data
//
public CanMessage()
{
getMessage.DATA = new byte[8] { 0x42, 0x01, 0, 0, 0, 0, 0, 0 };
getMessage.ID = Convert.ToUInt32(idText, 16);
getMessage.LEN = Convert.ToByte(msgLength);
getMessage.MSGTYPE = TPCANMessageType.PCAN_MESSAGE_EXTENDED;
}
}
zhou_xuexi 2011-08-18
  • 打赏
  • 举报
回复
const 关键字用于修改字段或局部变量的声明。 它指定字段或局部变量的值是常数,不能被修改。把const去掉
zhou_xuexi 2011-08-18
  • 打赏
  • 举报
回复
const 关键字用于修改字段或局部变量的声明。 它指定字段或局部变量的值是常数,不能被修改。把const去掉
zhou_xuexi 2011-08-18
  • 打赏
  • 举报
回复
const 关键字用于修改字段或局部变量的声明。 它指定字段或局部变量的值是常数,不能被修改。把const去掉
我姓区不姓区 2011-08-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ellaxy 的回复:]

ojlovecd说的是对的。ojlovecd提供的两个方法第一个在外部不能引用,第二个方法能被外部引用(Madicine.CanMessage.CanMessage.getMessage)。是不是如果要外部引用,是不是只有第二个途径才行?
[/Quote]
那就只能用第二种方法了
ellaxy 2011-08-18
  • 打赏
  • 举报
回复
ojlovecd说的是对的。ojlovecd提供的两个方法第一个在外部不能引用,第二个方法能被外部引用(Madicine.CanMessage.CanMessage.getMessage)。是不是如果要外部引用,是不是只有第二个途径才行?

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧