Re: struct
- From: "Neno Loje [MVP C#]" <noreply@xxxxxxxxxxxx>
- Date: Wed, 6 Apr 2005 00:22:24 +0200
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
>>
>> }
>>
.
- Follow-Ups:
- Re: struct
- From: Bruce Wood
- Re: struct
- References:
- struct
- From: 14 7/8\" x 8 1/2\"
- Re: struct
- From: Mohammad
- Re: struct
- From: Marv
- struct
- Prev by Date: Re: c# Question
- Next by Date: RE: Binding to custom class object
- Previous by thread: Re: struct
- Next by thread: Re: struct
- Index(es):
Relevant Pages
|