2011-09-23 3 views
1

어떻게 지금은 정말 이상합니다. 그리고 슬픈 것은 많은 구조를 가지고 있으며 자주 사용한다는 것입니다. 여기F # 변경 가능한 구조로 작업하는 더 쉬운 방법이 있습니까?

내가 잠시 행동 해요 방법은 다음과 같습니다

여기
[<type:StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)>] 
type OneDevice = { 
     mutable id    : UInt16 
     mutable typeDev   : byte 
     mutable portNum   : byte 
     mutable Parity   : byte 
     mutable StopBits  : byte 
     mutable BaudRate  : byte 
     mutable addr1   : byte 
     mutable addr2   : byte 
     mutable useCanal  : byte 
     mutable idGroup1  : byte 
     mutable idGroup2  : byte 
     mutable idGroup3  : byte 
     mutable idGroup4  : byte 
     mutable idGroupSos1  : byte 
     mutable idGroupSos2  : byte 
     mutable idGroupSos3  : byte 
     mutable idGroupSos4  : byte 
     mutable idSosReserv  : byte 
     mutable addrModbus  : byte 
     mutable offsetModbus : System.UInt16 
     [<field: MarshalAs(UnmanagedType.ByValArray, SizeConst = 17)>] 
     mutable pwd    : byte array 
     mutable offsetInModbus : UInt16 
     mutable reserv   : UInt16 } 

내가 구조를 정의하는 방법이다. "type str = struct ..."정말 다르게 작동합니다! 구조의 변종이 필요합니다. 그러나 이것을 쓰는 것은 나의 문제의 절반에 불과하며, 두 번째 절반은이 구조의 새로운 요소를 만드는 방법입니다. mutable dev = new OneDevice()가 작동하지 않습니다! 그래서 이것을 만들 필요가 있습니다 :

let mutable dev = { 
    id    = 0us 
    typeDev   = 0x00uy 
    portNum   = 0x00uy 
    Parity   = 0x00uy 
    StopBits  = 0x00uy 
    BaudRate  = 0x00uy 
    addr1   = 0x00uy 
    addr2   = 0x00uy 
    useCanal  = 0x00uy 
    idGroup1  = 0x00uy 
    idGroup2  = 0x00uy 
    idGroup3  = 0x00uy 
    idGroup4  = 0x00uy 
    idGroupSos1  = 0x00uy 
    idGroupSos2  = 0x00uy 
    idGroupSos3  = 0x00uy 
    idGroupSos4  = 0x00uy 
    idSosReserv  = 0x00uy 
    addrModbus  = 0x00uy 
    offsetModbus = 0us 
    pwd    = Array.zeroCreate 17 
    offsetInModbus = 0us 
    reserv   = 0us } 

그리고 그것은 이상한 부분입니다. 어떻게 든 쉽게 만들 수 있습니까?

감사합니다.

답변

5

문제는 구조가 아닙니다. 레코드 유형입니다. type [<Struct>] MyStruct = ...을 사용하십시오. http://msdn.microsoft.com/en-us/library/dd233184.aspx

구조체는 값 종류 :

http://msdn.microsoft.com/en-us/library/dd233233.aspxhttp://msdn.microsoft.com/en-us/library/ee340416.aspx

레코드 유형을 참조하십시오. 레코드는 클래스와 같은 참조 유형입니다.

+0

'[] MyStruct'이어야하지 않습니까? – ildjarn

+0

어느 것이 든 작동합니다. 때때로이 방법을 재귀 정의와의 일관성을 위해 사용합니다.이 정의는'and' 다음에 속성을 요구합니다. – Daniel

관련 문제