1

I have this code :

 public struct SmartFilter
    {
        public int from, to;
        public ArrayList collect  = new ArrayList();                 
        public long bit;            
    }

i get error :

cannot have instance field initializers in structs

i try different way to get over this error without success,

how to have a array list in struct / class?

3
  • 1
    The error message you get from the compiler seems pretty clear to me... I think you could have solved this one on your own. Commented Jan 7, 2011 at 7:53
  • 1
    why is it a struct? Without a very good reason, that is an error Commented Jan 7, 2011 at 7:56
  • (for info, the reason you can't have a field initializer on a struct is that the default constructor doesn't really exist and even if it did, it would not be guaranteed to run...) Commented Jan 7, 2011 at 7:59

3 Answers 3

5

There are multiple problems there:

  • having a mutable struct - just evil
  • having public fields
  • using ArrayList

none of these is helping you...

with a class the initializer would work fine

with a property you could do lazy init:

public ArrayList Collect {
    get { return collect ?? (collect = new ArrayList()); }
}

I would refactor to:

public class SmartFilter
{
    public int From  {get;set;}
    public int To  {get;set;}
    private List<SomeKnownType> collect  = new List<SomeKnownType>();
    public List<SomeKnownType> Collect { get { return collect; } }
    public long Bit {get;set;}
}
Sign up to request clarification or add additional context in comments.

3 Comments

Why is having a mutable struct just evil? If the thing you are working is a value object, isn't it just fine to use a struct?
@Tomas the number of bugs introduced by people not fully appreciating the copy semantics of structs is staggering, and the issues with losing changes etc is huge. Structs can be used successfully as mutable entities, but as the exception rather than the rule - it forces a very different programming style (either lots of ref, or accessing directly in an array - not a list or similar). I use them myself very occasionally. But the vast majority of mutable structs I see are used inappropriately, and almost always are the underlying cause of the bug that I've been summoned to investigate.
I rarely use structs just as you, and I agree with that they cause a lot of pain since a lot of developers don't know how to deal with them. But saying that they are "just evil" is pretty hard :).
3

Eh, I'll let the guys with bigger brains than me explain why, but you can't do that. :) However you could do this...

class NotAStruct
{
    public int from, to;
    public ArrayList collect = new ArrayList();
    public long bit;   
}

...or this...

struct Blah
{
    ArrayList doh;
    public void SomeMethod()
    {
        doh = new ArrayList();
    }
}

But neither is a good idea, as pointed out by several other people.

5 Comments

"You can't have an ArrayList in a struct." -1 This is completely and utterly incorrect.
Yeah, that was worded poorly. I'll rephrase.
@cdhowie, Yeah, I think my fingers were going faster than my brain was. :-)
@Haim, Your welcome, but make sure you heed the advice in the other answers.
Even though it works, public variables is not that nice. It's a small difference to public properties, but with public properties you get some more "control"
2

You should make a class of it, I wouldn't keep an ArrayList in a struct since struct should be used for simple data. Regarding the error you have I think you just can remove the new ArrayList(); and it should work.

I wrote this and it works just fine:

public struct MyStruct
{
    public int intVal;
    public ArrayList listVal;
    public bool boolVal;
}

However, I really thing you should use a class instead; that is, use this instead:

public class MyClass    {
    public int IntVal {get; set;}
    public ArrayList ListVal {get; set;}
    public bool BoolVal {get; set;}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.