Re: struct



Hi Marv,

instance fields in structs cannot be initiated with value.

Just remove the "= string.Empty;" parts and it will work.

Hope it helps,
Neno

--
Neno Loje [MVP C#]
http://www.dotnet-online.com

"Marv" <Marv@xxxxxxxx> schrieb im Newsbeitrag
news:425308AB.7050705@xxxxxxxxxxx
> Mohammad wrote:
>> 14 7/8" x 8 1/2" wrote:
>>
>>>I want to use a struct to constrain a result.
>>>
>>>The result is a string composed of several strings of fixed length;
>>>
>>>Result is composed of 3 strings, each 4 char in length.
>>>
>>>Can I build a struct that would enforce such a construct?
>>
>> Yes you can, something like the following:
>>
>> struct MyResult
>> {
>> private string s1 = string.Empty;
>> private string s2 = string.Empty;
>> private string s3 = string.Empty;
>
> I tried doing this and VS.NET reports:
>
> "cannot have instance field initializers in structs"
>
>
>
>>
>> public string S1
>> {
>> get
>> {
>> return s1;
>> }
>>
>> set
>> {
>> if ( value.Length <= 3 )
>> s1 = value;
>> else
>> throw new InvalidOperationExcpetion();
>> }
>> }
>>
>> // repeat for S2 and S3
>>
>> }
>>


.



Relevant Pages

  • Re: struct versioning
    ... to be initialized by client code, by taking the sizeof of the struct. ... write the WIDGET macro-opcode and provide operands for the ... was very often a length indicator for the whole block. ... C's initializers, even now, are not robust ...
    (comp.lang.c)
  • Re: [RFC 04/10] Temporary struct vfs_lookup in file_permission
    ... Used to check for read/write/execute permissions on an already opened ... int file_permission(struct file *file, int mask) ... I didn't use initializers because they initialize the entire data structure. ... In case of struct vfs_lookup, unless the LOOKUP_OPEN flag is set, then the ...
    (Linux-Kernel)
  • Re: [PATCH] Detect mmconfig on nVidia MCP55
    ... more reviewable - and more pleasant to ... Same goes for static structure initializers. ... static const struct file_operations perf_fops = { ... The typos are of the ...
    (Linux-Kernel)
  • Re: missing braces around initializer
    ... to warn about missing braces or struct members in initializers. ... it's annoying that gcc warns about this. ...
    (comp.lang.c)
  • Re: [patch] add private data to notifier_block
    ... notifier_block struct to add a void *, ... initializers -- their bare priority initialization might now shift to ... putting the void * after the int. ...
    (Linux-Kernel)