read-only object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

My custom control includes DataGrid as one of the constituents. I want to be
able to expose this DataGrid as public property "get":

public DataGrid InternalDataGrid
{
get {return MyDataGrid; }
}

At the same time, I don't want to let user change the grid's properties or
content. In other words, if someone gets reference to my DataGrid using
InternalDataGrid property, it should be impossible for him/her to change any
of the grid's properties.

Does anybody know how to do this?
Thank you
 
Alex said:
My custom control includes DataGrid as one of the constituents. I want to be
able to expose this DataGrid as public property "get":

public DataGrid InternalDataGrid
{
get {return MyDataGrid; }
}

At the same time, I don't want to let user change the grid's properties or
content. In other words, if someone gets reference to my DataGrid using
InternalDataGrid property, it should be impossible for him/her to change any
of the grid's properties.

Does anybody know how to do this?
Thank you

I'm not sure if this would work but I think you can just use the const
keyword like so:

public const DataGrid InternalDataGrid
{
get ( return (const) MyDataGrid; }
}

- Bill
 
if someone gets reference to my DataGrid using
InternalDataGrid property, it should be impossible for him/her to change any
of the grid's properties.

C# does not support the notion of const reference like C++, so there is no
easy fix here.

The two obvious workarounds both require a bit of effort:
- make a copy of the DataGrid and send the copy out
- create a wrapper around the DataGrid that does not allow changes
 
Bill said:
I'm not sure if this would work but I think you can just use the const
keyword like so:

public const DataGrid InternalDataGrid
{
get ( return (const) MyDataGrid; }
}

No you can't. .NET doesn't have the concept of const-correctness.
 
Back
Top