Thread overview
Can't create nested classes
Jan 23, 2007
Heinz
Jan 23, 2007
Heinz
Jan 23, 2007
Heinz
Jan 23, 2007
Alexander Panek
Jan 23, 2007
Heinz
Jan 23, 2007
Bradley Smith
January 23, 2007
I have the following code, i'm working with nested classes, when i try to compile it it enters a nice loop full of errors:

class myclass
{
        private Table mytable = new Table();

        class Table
        {

        }
}

I get errors about a 'this' so i add this() constructors to myclass, Table and tried both at the same time.

What could be wrong? thx
January 23, 2007
"Heinz" <billgates@microsoft.com> wrote in message news:ep408i$2389$1@digitaldaemon.com...

>        private Table mytable = new Table();

You're not allowed to have non-constant expressions in class member initializers.  Move this 'new' into a constructor for myclass.


January 23, 2007
Jarrett Billingsley Wrote:

> 
> "Heinz" <billgates@microsoft.com> wrote in message news:ep408i$2389$1@digitaldaemon.com...
> 
> >        private Table mytable = new Table();
> 
> You're not allowed to have non-constant expressions in class member initializers.  Move this 'new' into a constructor for myclass.
> 
> 

Check this out, if i write this code it works:

class myclass
{
       private Table mytable;

       this()
       {
               mytable = new Table();
        }
}

but if i write this code i get an access violation:


class myclass
{
       private Table mytable, mytable2;

       this()
       {
               mytable = new Table();
               mytable2 = new Table();
        }
}

And of course i need the second block of code.
January 23, 2007
Jarrett Billingsley Wrote:

> 
> "Heinz" <billgates@microsoft.com> wrote in message news:ep408i$2389$1@digitaldaemon.com...
> 
> >        private Table mytable = new Table();
> 
> You're not allowed to have non-constant expressions in class member initializers.  Move this 'new' into a constructor for myclass.
> 
> 

I can't also have variables like 'int i', it throws access violation. But why in the DMD page here http://www.digitalmars.com/d/class.html is listed a bunch of classes with 1 to 3 non-static members like int i?

Man, C# is a lot more flexible in this topic, why can we have non-static members in C# and not in D?
January 23, 2007
Heinz wrote:
> I can't also have variables like 'int i', it throws access violation. But why in the DMD page here http://www.digitalmars.com/d/class.html is listed a bunch of classes with 1 to 3 non-static members like int i?
> 
> Man, C# is a lot more flexible in this topic, why can we have non-static members in C# and not in D?

No offense intended, but I do think you're doing something wrong here. Would you mind pasting the whole source code, please?
January 23, 2007
Alexander Panek Wrote:

> Heinz wrote:
> > I can't also have variables like 'int i', it throws access violation. But why in the DMD page here http://www.digitalmars.com/d/class.html is listed a bunch of classes with 1 to 3 non-static members like int i?
> > 
> > Man, C# is a lot more flexible in this topic, why can we have non-static members in C# and not in D?
> 
> No offense intended, but I do think you're doing something wrong here. Would you mind pasting the whole source code, please?


Sure man, attached is the code with the main file included, just compile with these 2 files and you're ready.

I noticed if i remove all references to type File or delegate, it works ok. I mean, you can have the delegate or the File but not both.

Good luck


January 23, 2007
The crash in the example code isn't from nested classes. It is from the destructor of class Writer. The destructor is calling a method on the garbage collected member object "testFile". This reference is no longer valid.

From http://www.digitalmars.com/d/class.html#destructors

"When the garbage collector calls a destructor for an object of a class that has members that are references to garbage collected objects, those references are no longer valid."


In addition, the call is unnecessary because the class File will call close() in its destructor.


Heinz wrote:
> Alexander Panek Wrote:
> 
>> Heinz wrote:
>>> I can't also have variables like 'int i', it throws access violation. But why in the DMD page here http://www.digitalmars.com/d/class.html is listed a bunch of classes with 1 to 3 non-static members like int i?
>>>
>>> Man, C# is a lot more flexible in this topic, why can we have non-static members in C# and not in D?
>> No offense intended, but I do think you're doing something wrong here. Would you mind pasting the whole source code, please?
> 
> 
> Sure man, attached is the code with the main file included, just compile with these 2 files and you're ready.
> 
> I noticed if i remove all references to type File or delegate, it works ok. I mean, you can have the delegate or the File but not both.
> 
> Good luck