I get a compilation error and can't understand why

T

Tony Johansson

What I want to accomplish is to use class FlowerNetDbContext in class
OrderRepository.
So when I call OrderRepository property in RepositoryFactory I return an
instans of OrderRepository and pass in dbContext
which is a FlowerNetDbContext.
OrderRepository is implementing IOrderRepository

So what do is this. I can't see any problem error in doinf this.
public IOrderRepository OrderRepository
{
get {return new OrderRepository(this.dbContext); ;}
}

The problem start when I do this row
private ProductRepository productRepository =
repositoryFactory.ProductRepository
in class ProductsDisplay below.

The error I get is this
Error 2 A field initializer cannot reference the non-static field, method,
or property 'Assignment_2.ProductsDisplay.repositoryFactory'
c:\datadrivenwebapplicationprogramming_2014\assignment_2\productsdisplay.aspx.cs
19 53 Assignment_2

I mean I instanciate RepositoryFactory and tell him to give me the
ProductRepository by calling the property that is named ProductRepository

So how can I fix this ?


public partial class ProductsDisplay : System.Web.UI.Page
{
private RepositoryFactory repositoryFactory = new RepositoryFactory();

private ProductRepository productRepository =
repositoryFactory.ProductRepositor
....
....
}


public class RepositoryFactory
{
private IOrderRepository orderRepo;
private FlowerNetDbContext dbContext;

public RepositoryFactory()
{
dbContext = new FlowerNetDbContext();
}

public IOrderRepository OrderRepository
{
get {return new OrderRepository(this.dbContext); ;}
}
}


public class OrderRepository : IOrderRepository
{
private FlowerNetDbContext dbContext;

public OrderRepository(FlowerNetDbContext context)
{
dbContext = context;
}
}


//Tony
 
A

Anders Eriksson

The error I get is this
Error 2 A field initializer cannot reference the non-static field,
method, or property 'Assignment_2.ProductsDisplay.repositoryFactory'
c:\datadrivenwebapplicationprogramming_2014\assignment_2\productsdisplay.aspx.cs
19 53 Assignment_2

You cannot use an instance variable to initialize another instance
variable. Why? Because the compiler can rearrange these - there is no
guarantee that repositoryFactory will be initialized before
productRepository

See
http://stackoverflow.com/questions/...erence-the-nonstatic-field-method-or-property

// Anders
 
T

Tony Johansson

But they are of the same type
How can I force to establish what I said in the mail

If I have an object in a class and want to use this object in other classes
how can I do that.
I want to share an object.

//Tony
 
A

Anders Eriksson

But they are of the same type
How can I force to establish what I said in the mail

If I have an object in a class and want to use this object in other
classes how can I do that.
I want to share an object.

//Tony
You didn't read the StackOverflow article did you ?
You need to put the initialization in the constructor of the class.
But since the code doesn't make any sense to me I can't help you...

Maybe someone else can make sense of the code can be more specific.

// Anders
PS! PLEASE don't top post! It makes it hard to follow the thread!
 
A

Anders Eriksson

But they are of the same type
How can I force to establish what I said in the mail

If I have an object in a class and want to use this object in other
classes how can I do that.
I want to share an object.

//Tony
You didn't read the StackOverflow article did you ?
You need to put the initialization in the constructor of the class.
But since the code doesn't make any sense to me I can't help you...

Maybe someone else can make sense of the code can be more specific.

// Anders
PS! PLEASE don't top post! It makes it hard to follow the thread!
 
A

Arne Vajhøj

You cannot use an instance variable to initialize another instance
variable. Why? Because the compiler can rearrange these - there is no
guarantee that repositoryFactory will be initialized before
productRepository

Yes - C# does not specify the order so:

private int a = 123;
private int b = a;

gives this error.

Note that this is just a decision by the C# people.

The Java people made another decision and the code snippet
is in fact valid in Java, because Java specify that fields
are initialized in textual order.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top