www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Question about property & method access scope.

reply Vinod K Chandran <kcvinu82 gmail.com> writes:
Hi all,
I am practising D with a win api GUI hobby project.
I have a Window class and it resides in module window.d
My WndProc function resides in another module named 
wnd_proc_module.d
Inside my WndProc, I get the Window class like this.
```d
Window win = cast(Window) (cast(void*) GetWindowLongPtrW(hWnd, 
GWLP_USERDATA)) ;
```
So in many situations, I need to check some boolean properties of 
Window class and call some functions of Window class in WndProc.
But I don't want to expose those props and functions to the user. 
So if I make them private, I can't access them inside the WndProc 
function. How do solve this issue. Thanks in advance.
May 11 2021
next sibling parent drug <drug2004 bk.ru> writes:
11.05.2021 12:10, Vinod K Chandran пишет:
 Hi all,
 I am practising D with a win api GUI hobby project.
 I have a Window class and it resides in module window.d
 My WndProc function resides in another module named wnd_proc_module.d
 Inside my WndProc, I get the Window class like this.
 ```d
 Window win = cast(Window) (cast(void*) GetWindowLongPtrW(hWnd, 
 GWLP_USERDATA)) ;
 ```
 So in many situations, I need to check some boolean properties of Window 
 class and call some functions of Window class in WndProc.
 But I don't want to expose those props and functions to the user. So if 
 I make them private, I can't access them inside the WndProc function. 
 How do solve this issue. Thanks in advance.
Make them `protected`?
May 11 2021
prev sibling next sibling parent reply cc <cc nevernet.com> writes:
On Tuesday, 11 May 2021 at 09:10:02 UTC, Vinod K Chandran wrote:
 Hi all,
 I am practising D with a win api GUI hobby project.
 I have a Window class and it resides in module window.d
 My WndProc function resides in another module named 
 wnd_proc_module.d
 Inside my WndProc, I get the Window class like this.
 ```d
 Window win = cast(Window) (cast(void*) GetWindowLongPtrW(hWnd, 
 GWLP_USERDATA)) ;
 ```
 So in many situations, I need to check some boolean properties 
 of Window class and call some functions of Window class in 
 WndProc.
 But I don't want to expose those props and functions to the 
 user. So if I make them private, I can't access them inside the 
 WndProc function. How do solve this issue. Thanks in advance.
The `package` protection attribute should work here if the modules reside in the same package (directory)? ```d module mywindow.window; import mywindow.wnd_proc_module.d class Window { package int x, y; } ``` ```d module mywindow.wnd_proc_module.d import mywindow.window; class Proc { Window win; void doStuff() { win.x = 3; } } ```
May 11 2021
parent Vinod K Chandran <kcvinu82 gmail.com> writes:
On Tuesday, 11 May 2021 at 10:47:15 UTC, cc wrote:

 The `package` protection attribute should work here if the 
 modules reside in the same package (directory)?
Thanks. "package" scope worked.
May 11 2021
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Tuesday, 11 May 2021 at 09:10:02 UTC, Vinod K Chandran wrote:

 So in many situations, I need to check some boolean properties 
 of Window class and call some functions of Window class in 
 WndProc.
 But I don't want to expose those props and functions to the 
 user. So if I make them private, I can't access them inside the 
 WndProc function. How do solve this issue. Thanks in advance.
Assuming window.d and wndproc.d are in the same package (and not the default global package), then you can use `package` instead of `private`.
May 11 2021
parent Vinod K Chandran <kcvinu82 gmail.com> writes:
On Tuesday, 11 May 2021 at 10:48:03 UTC, Mike Parker wrote:
 On Tuesday, 11 May 2021 at 09:10:02 UTC, Vinod K Chandran wrote:

 So in many situations, I need to check some boolean properties 
 of Window class and call some functions of Window class in 
 WndProc.
 But I don't want to expose those props and functions to the 
 user. So if I make them private, I can't access them inside 
 the WndProc function. How do solve this issue. Thanks in 
 advance.
Assuming window.d and wndproc.d are in the same package (and not the default global package), then you can use `package` instead of `private`.
On Tuesday, 11 May 2021 at 10:48:03 UTC, Mike Parker wrote: > > Assuming window.d and wndproc.d are in the same package (and > not the default global package), then you can use `package` > instead of `private`. Thanks. "package" scope worked. this is the code now. ```d package : bool misBkClrChanged ; void setBkClrInternal(HDC dcHandle) { RECT rct; HBRUSH hBr = CreateSolidBrush(cast(COLORREF) this.mBackColor); GetClientRect(this.mHandle, &rct) ; FillRect(dcHandle, &rct, hBr) ; DeleteObject(hBr) ; } // And this is the wndproc case WM_ERASEBKGND : { if(win.misBkClrChanged) { auto dch = cast(HDC) wParam ; win.setBkClrInternal(dch) ; return 1 ; } } break ; } ```
May 11 2021