-
Jul 4th, 2022, 08:58 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Why does C# allow direct access to private members of an object?
I often see code like the following:
Code:
internal class MyClass {
private int _size;
private int _version;
public Clone(MyClass obj) {
this._size = obj._size;
this._version = obj._version;
}
}
Why does C# allow direct access to private members of an object? Closure?
Last edited by SearchingDataOnly; Jul 4th, 2022 at 09:22 AM.
-
Jul 4th, 2022, 09:04 AM
#2
Re: Why does C# allow direct access to private members of an object?
Why does it exist or why does it work?
If why does it work, then:
"internal: The type or member can be accessed by any code in the same assembly, but not from another assembly. In other words, internal types or members can be accessed from code that is part of the same compilation."
Source: https://docs.microsoft.com/en-us/dot...cess-modifiers
-
Jul 4th, 2022, 09:19 AM
#3
Thread Starter
Frenzied Member
Re: Why does C# allow direct access to private members of an object?
Originally Posted by OptionBase1
Why does it exist or why does it work?
If why does it work, then:
"internal: The type or member can be accessed by any code in the same assembly, but not from another assembly. In other words, internal types or members can be accessed from code that is part of the same compilation."
Source: https://docs.microsoft.com/en-us/dot...cess-modifiers
Access Modifiers (C# Programming Guide):
private: The type or member can be accessed only by code in the same class or struct
-
Jul 4th, 2022, 09:28 AM
#4
Re: Why does C# allow direct access to private members of an object?
Change the class modifier to something other than internal and see if it still works. If not, then it is likely because the class itself is defined as internal.
Edit to add: I didn't test your sample code to see if that actually works, I'm taking your word for it that does.
-
Jul 4th, 2022, 09:47 AM
#5
Re: Why does C# allow direct access to private members of an object?
Originally Posted by SearchingDataOnly
Access Modifiers (C# Programming Guide):
private: The type or member can be accessed only by code in the same class or struct
That code is in the same class or struct, so it's accessible. It doesn't say the same instance of the same class or struct, just the same class or struct.
-
Jul 4th, 2022, 02:13 PM
#6
Re: Why does C# allow direct access to private members of an object?
Originally Posted by SearchingDataOnly
I often see code like the following:
Code:
internal class MyClass {
private int _size;
private int _version;
public Clone(MyClass obj) {
this._size = obj._size;
this._version = obj._version;
}
}
Why does C# allow direct access to private members of an object? Closure?
Isn't that the same with VB6 as well? Private members are private to the class or type, not to the instance.
-
Jul 4th, 2022, 08:54 PM
#7
Re: Why does C# allow direct access to private members of an object?
I just noticed that this is a C# question in the VB.NET forum. This site has a C# forum. Don't post in the wrong forum just because you think it will mean getting more people to see your question.
-
Jul 4th, 2022, 10:47 PM
#8
Re: Why does C# allow direct access to private members of an object?
-
Jul 5th, 2022, 12:26 AM
#9
Re: Why does C# allow direct access to private members of an object?
Yeah, it makes perfect sense to be able to do this. You can only do it in the same type that the members are declared so, if you're the author of both those private members and the code accessing them, you can make sure they are not accessed in an inappropriate way. Without this feature, cloning and copy constructors would be impossible without exposing data that shouldn't be exposed. It's completely practical.
-
Jul 5th, 2022, 09:02 AM
#10
Thread Starter
Frenzied Member
Re: Why does C# allow direct access to private members of an object?
Originally Posted by OptionBase1
Change the class modifier to something other than internal and see if it still works. If not, then it is likely because the class itself is defined as internal.
Edit to add: I didn't test your sample code to see if that actually works, I'm taking your word for it that does.
Thank you, OptionBase1.
Originally Posted by jmcilhinney
That code is in the same class or struct, so it's accessible. It doesn't say the same instance of the same class or struct, just the same class or struct.
Thank you, jmcilhinney.
Under normal circumstances, the access rights of the member variables of the class should refer to "the access rights of the member variables of the instance of the class at runtime", not the access rights in the code window. However, since the C# compiler and documentation really have to interpret it in its own way, we can only accept it. In contrast, VB6's approach is more scientific, but inconvenient and inflexible.
-
Jul 5th, 2022, 09:07 AM
#11
Thread Starter
Frenzied Member
Re: Why does C# allow direct access to private members of an object?
Originally Posted by PlausiblyDamp
Isn't that the same with VB6 as well? Private members are private to the class or type, not to the instance.
In VB6, the access rights of the member variables of the class should refer to "the access rights of the member variables of the instance of the class at runtime", not the access rights in the code window.
So, in VB6, both Me.m_Size and obj.m_Size are not allowed if m_Size is a private member.
Last edited by SearchingDataOnly; Jul 5th, 2022 at 09:16 AM.
-
Jul 5th, 2022, 09:15 AM
#12
Thread Starter
Frenzied Member
Re: Why does C# allow direct access to private members of an object?
Originally Posted by jmcilhinney
I just noticed that this is a C# question in the VB.NET forum. This site has a C# forum. Don't post in the wrong forum just because you think it will mean getting more people to see your question.
I didn't even know there was a C# sub-forum, I thought the C# question was merged into the VB.NET forum.
Originally Posted by jmcilhinney
Yeah, it makes perfect sense to be able to do this. You can only do it in the same type that the members are declared so, if you're the author of both those private members and the code accessing them, you can make sure they are not accessed in an inappropriate way. Without this feature, cloning and copy constructors would be impossible without exposing data that shouldn't be exposed. It's completely practical.
Yes, I agree with that.
I guess that VB6 uses dynamic compilation in the IDE window to instantly check the code for syntax errors. Therefore, in VB6, the access rights of class member variables can only be in the instance environment. It is also like this, VB6-IDE can do "Edit-Continue" when the code is running. I don't know if VisualStudio.NET can also do "Edit-Continue" while the code is running
Last edited by SearchingDataOnly; Jul 5th, 2022 at 09:20 AM.
-
Jul 7th, 2022, 10:57 AM
#13
Re: [RESOLVED] Why does C# allow direct access to private members of an object?
Originally Posted by SearchingDataOnly
Closure?
How I use Closure - this is JS, but concept is the same.
Simple example:
Here I am calling a function that will open an EDIT window in the browser.
It allows you to edit the "report layout" formula for a particular grid that is referenced by that single parameter intGO. That grid sits in a global object (array) of g_objGrid[]. I grab the formula "text", put it in an HTML element, and then call the .DIALOG function to setup the OK and CANCEL buttons. They will run a function that is being created right inline and note that the function can reference intGO. This is Closure - keeping a variable ALIVE even after the editReportLayout function exits, and someone comes along and clicks an OK button.
Let that sink in.
I created a function on the fly, that might or might not ever be executed. And it will retain a value that allows access to that g_objGrid array at some later time. And that intGO value is "hidden" from everyone - known only to the function when it might ever execute and bring into scope that very old variable.
Code:
function editReportLayout(intGO) {
$("#acs-formula .acs-dlg-prompt").html("Report Layout Source");
$("#acs-formula .acs-dlg-label").html("");
$("#acs-formula .acs-dlg-formula").val(g_objGrid[intGO].awcoptions.reportlayout || "");
$("#acs-formula").dialog(
{
title: 'Report Layout'
, buttons: {
'Ok': function () {
var strLayout = $("#acs-formula .acs-dlg-formula").val();
g_objGrid[intGO].awcoptions.reportlayout = strLayout
$("#acs-formula").dialog('close');
},
'Cancel': function () {
$("#acs-formula").dialog('close');
}
}
});
$("#acs-formula").dialog('open');
}
And a more complex example:
Here I am running a report with printStart - passing in the STORED PROCEDURE to run on the backend server (rptSproc) and some supporting info in ctrlval1, 2 and 3.
strCT is the "tag" that identifies the HTML element that initiated this report request. The array source optionally contains local web grid "data" to be printed.
Note that it calls ctrlWebService - that function explained shortly...
The key item to note in printStart is that it is starting a "timer" to run a "web service" to check the "state" of the report creation. Polling the server to find out how many pages have been printed. And of course the UI needs to know what HTML element to "associate" that returned information with.
This is accomplished by passing in strCt to that INLINE function being created on the fly. And note that it's also calling ctrlWebService
Code:
function printStart(rptSproc, ctrlval1, ctrlval2, ctrlval3, strCT, source) {
ctrlWebService(rptSproc, ctrlval1, ctrlval2, ctrlval3, strCT, source);
g_printCycle = 1000;
setTimeout(function () {
ctrlWebService("acscheck", "", "", "", strCT, []);
}, g_printCycle);
}
And here is that function - ctrlWebService.
Basically it's going to create an AJAX call to the server. Nice thing about this type of request is that it's async and allows for "callback" functions to be executed when the browser finally gets the results of the request.
You can see it does a POST to the web server - runs a web service called CtrlService and most important, upon that AJAX call coming back, runs another function ajaxCtrlFinished.
That function is being created INLINE, on the fly. Actually three of them for SUCCESS, FAILURE and ERROR.
And that function created on the fly, needs to know the HTML element - the strId variable here...
And even more important, a complex OBJECT variable is being held in closure - objWebParam. The cost of this was considered. And more important, it's a locally created object, not something passed in that I have to worry about keeping alive and/or stepping all over with async returns.
Code:
function ctrlWebService(strOpt, strVal1, strVal2, strVal3, strId, source, extra) {
var newId = "";
var objWebParam = {};
var wesId = [];
objWebParam.username = window.username || "";
.
.
.
var strWebParam = $.toJSON(objWebParam);
$.ajax({
type: "POST",
url: "WebService.asmx/CtrlService",
dataType: "json",
data: strWebParam,
contentType: "application/json; charset=utf-8",
success: function(msg) {
ajaxCtrlFinished(msg, strId, "success", objWebParam);
},
failure: function(msg) {
ajaxCtrlFinished(msg, strId, "failure", objWebParam);
},
error: function(msg) {
ajaxCtrlFinished(msg, strId, "error", objWebParam);
}
});
}
One really cool thing to note is that all these AJAX requests are being processed and will RETURN out of the order that they were sent. I'm polling for "how many pages" many times as a report runs and runs and runs.
Just for completion sake - here is what that callback function basically looks like
Code:
function ajaxCtrlFinished(msg, sender, rtnstatus, objOptions) {
var objReturn = $.parseJSON(msg.d);
.
.
.
}
Last edited by szlamany; Jul 7th, 2022 at 02:32 PM.
-
Jul 9th, 2022, 05:18 AM
#14
Thread Starter
Frenzied Member
Re: [RESOLVED] Why does C# allow direct access to private members of an object?
Very nice and clear code. Thank you, szlamany.
If 20 years ago, I knew that JavaScript could write such neat and clear code, maybe I started learning and using JavaScript 20 years ago. At that time, all the JavaScript I saw was so messed up as spaghetti that I had no interest in learning and using it at the time, and because of that, I missed out on the best programming language in the world for up to 20 years.
-
Jul 9th, 2022, 09:18 AM
#15
Re: Why does C# allow direct access to private members of an object?
Originally Posted by SearchingDataOnly
I don't know if VisualStudio.NET can also do "Edit-Continue" while the code is running
It can. It's a per language feature (not per IDE). Dynamic compilation or not E&C is possible. . . hard but still very much possible to implement.
The feature does not depend on having private members accessible or not. In VB6 Watched windows you have all private variables *and* constants available on any instance running in the IDE (not from compiled components) i.e. instances from the interpreter heap are fully introspectable.
cheers,
</wqw>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|