473,666 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fields for methods?

I was wondering if maybe allowing "fields" for methods. The reason is to
encapsulate the data that is mainly used by the method and to prevent the
need of having to create new variables every time the function is called if
you know they need not change within the method.

In a sense these would be equivilent to passing arguments to the method or
equivilent to local variables... but these arguments or variables always
have the same run-time values in the method's scope.

So we could have

public void SomeFunc(Some Params, object O)
{
....
}

public void SomeFunc(Some Params)
{
object O = new Object;
....
}
(would waste cycles because we allocate and deallocate O every function call
even if O if O is some constant value w.r.t to the method. )

object O;
public void SomeFunc(Some Params)
{
(uses object O)
....
}

etc..

But we know that 99% of the time object O does not change in the methods
call.

Why not have something like

public void SomeFunc(Some Params)
[Object O = some value]
{
(uses object O)
....
}

and we can access O by SomeFunc.O or SomeFunc().O or whatever. (I'm not
saying the syntax has to be strictly as I have shown).

One could even has getters and setters for O with an ability for default
initialization

public void SomeFunc(Some Params)
[ private Object o;
public Object O
{
init {o = initial value;}
get {return o;}
set {o = value;}
}
}
{
(uses object O)
....
}

or how ever one really wants to do this. I know you can use the above
methods to do this but it seems that one looses the ability to encapsulate.
Ofcourse with a lot of variables it could get messy quick.

In essense a method would be very similar to a class but we wouldn't have to
do something like

public static class MyFunc
{
public return value;
private Object o;
public Object O
{
init {o = initial value;}
get {return o;}
set {o = value;}
}
MyFunc(some Params)
{
(uses O)
return value;
}
}

which is essentially what the above would be equivilent to. Here we can use
MyFunc.O and call MyFunc just like a method but the return value is hard to
use(although maybe the ability to return it directly or indirectly like this
would be could so one could do MyFunc.Result or MyResult = MyFunc();)... and
one has to declare the constructur and stuff so its a little extra work.

Just seems like there could be some room for improvement?

Any ideas?

Thanks,
Jon


Oct 12 '06 #1
2 1564

Jon Slaughter wrote:
I was wondering if maybe allowing "fields" for methods. The reason is to
encapsulate the data that is mainly used by the method and to prevent the
need of having to create new variables every time the function is called if
you know they need not change within the method.
One can already do this, simply by making the field a field of the
class and having the method be the only one that uses it. All of the
behaviour that you describe can be produced using class fields.

The only difference, therefore, is scope: in your proposed extension,
the field is local to the method and cannot be used outside the method.
In the current C# the field is local to the class and can be used by
any method / property in the class.

Jon Skeet has proposed something similar: a field that is local to a
property, which is a much more common scenario: a property wraps a
field that is not used anywhere else in the class. However, the
motivation behind this is different: the idea is to encapsulate more
information about how the field is meant to be used, so that the
compiler can enforce the rule: this field is read / written only via
this property, even within this class.

In your case, you're wanting to avoid the overhead of allocating and
collecting objects across method calls. Unfortunately, it's rather more
complicated than you stated.

First, it should be a given that allowing a "temporary variable" to
live from one method call to another makes sense in only two
situations. 1) The variable is part of the class state (in which case
it's not really "temporary" at all); in this case, it belongs at the
class scope, not the method scope. 2) The variable is unchangeable, and
so will not cause different results for subsequent method calls.

Ignoring case #1 (which already has a solution), case #2 is first of
all not a common scenario, and second of all can't be guaranteed by the
language except by creating an instance of an immutable class. While
you can use the "readonly" modifier to guarantee that a particular
reference will always point to the same object instance, you cannot
guarantee that the state of that instance cannot change--that depends
entirely upon its type (class) definition. If the type definition
allows the instance's state to change, then you have the classic case
of a method call that can return different things with the same inputs
depending upon what's happening with this "temporary" variable. In
effect, the "temporary" object becomes part of the instance state and
belongs at the class scope.

This leads into the whole problem of "constant" versions of objects,
which the C# team has chosen not to address thusfar because it means
support in the CLR (and thus modifications to the runtime).

<snip>
Just seems like there could be some room for improvement?
Well, there's always room for improvement, but the question is whether
the problem is common enough and causes enough grief to warrant the
added complexity that the solution would introduce into the language.
In this case, the problem seems uncommon and easy to solve using other
means, although the solution is not 100% satisfactory. The solution
would seem to require complicated modifications to the runtime and the
compiler.

Oct 13 '06 #2

"Bruce Wood" <br*******@cana da.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
>
Jon Slaughter wrote:
>I was wondering if maybe allowing "fields" for methods. The reason is to
encapsulate the data that is mainly used by the method and to prevent the
need of having to create new variables every time the function is called
if
you know they need not change within the method.

One can already do this, simply by making the field a field of the
class and having the method be the only one that uses it. All of the
behaviour that you describe can be produced using class fields.
yeah, but you have to describe the class differently than the method I
stated. You are declaring a global variable w.r.t to the method. You cannot
enforce encapsulation of the field and other methods could change it. By
making the field local only to the method and allowing access rights you do
not have to worry about this. You also get the benifit of the similar
declarations being defined in the same place instead of possibily having
them get sepeated.
The only difference, therefore, is scope: in your proposed extension,
the field is local to the method and cannot be used outside the method.
In the current C# the field is local to the class and can be used by
any method / property in the class.
yes... and I think this is a bad idea. You have to declare the field
somewhere where its not used, you have to initalize it somewhere else and
then use it somewhere else. Why not do that all in one place instead of
them down?
Jon Skeet has proposed something similar: a field that is local to a
property, which is a much more common scenario: a property wraps a
field that is not used anywhere else in the class. However, the
motivation behind this is different: the idea is to encapsulate more
information about how the field is meant to be used, so that the
compiler can enforce the rule: this field is read / written only via
this property, even within this class.
I thought of this earlier too and asked about it but just got a lot of
nonsense replies. I think the idea is very similar to what you just said
though. You are just including a property in the method to encapsulate it.
Within the scope of the method the property is global but it is not visual
outside as a normal property is. It can be accessed though if it has the
property modifiers through the function itself so it can be changed as if it
were a global field. The point is that one would use these types of
properties when the it won't often be used as like a global field.
In your case, you're wanting to avoid the overhead of allocating and
collecting objects across method calls. Unfortunately, it's rather more
complicated than you stated.

First, it should be a given that allowing a "temporary variable" to
live from one method call to another makes sense in only two
situations. 1) The variable is part of the class state (in which case
it's not really "temporary" at all); in this case, it belongs at the
class scope, not the method scope. 2) The variable is unchangeable, and
so will not cause different results for subsequent method calls.
Actually I'm refering to case 1. It maybe be true that it has a solution.
This does not nessarily mean that is the best solution. Sure the language
allows for what I'm talking about... just as you can do C++ templates
without C++ by doing everything "manually". Its not necessarily the case
that it does it better though. Now my idea is not such a grand leap as
templates was but it does offer just an extra bit. Ofcourse it doesn't
prevent one from using the old ways but just allows for one to do something
in a new way that might be potentially better. IMO the argument that just
cause you can do it now already is not a valid one. You can do just about
anything you want in any language if you tried hard enough. Its about ease
of use and creating features that make programming in it easier. I think if
one had the mentality that you seem to be proposing(and a lot of people seem
to say the same thing) then we still would be using machine language or
punch cards. Ofcourse what I'm saying is not a big deal. It doesn't offer
much but does offer something new. It might not be worth it for many though
as I suppose that tradition and habits are much stronger.

Ignoring case #1 (which already has a solution), case #2 is first of
all not a common scenario, and second of all can't be guaranteed by the
language except by creating an instance of an immutable class. While
you can use the "readonly" modifier to guarantee that a particular
reference will always point to the same object instance, you cannot
guarantee that the state of that instance cannot change--that depends
entirely upon its type (class) definition. If the type definition
allows the instance's state to change, then you have the classic case
of a method call that can return different things with the same inputs
depending upon what's happening with this "temporary" variable. In
effect, the "temporary" object becomes part of the instance state and
belongs at the class scope.

This leads into the whole problem of "constant" versions of objects,
which the C# team has chosen not to address thusfar because it means
support in the CLR (and thus modifications to the runtime).
I really don't see how 2 is any different from a static variable?
<snip>
>Just seems like there could be some room for improvement?

Well, there's always room for improvement, but the question is whether
the problem is common enough and causes enough grief to warrant the
added complexity that the solution would introduce into the language.
In this case, the problem seems uncommon and easy to solve using other
means, although the solution is not 100% satisfactory. The solution
would seem to require complicated modifications to the runtime and the
compiler.
I understand. What I feel is an improvement might not be to someone else.
And I suppose if your used to doing it one way and even if there comes about
a slightly better way you still will use the old way rather than learn the
new. Ofcourse I think this does limit potential growth I do understand it.
You can't keep learning new things because then you never get to use them.

Thanks,
Jon
Oct 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2408
by: Koda | last post by:
What is the difference between class fields and class properties? Thanks Mike
0
1152
by: Boniek | last post by:
If exists any specification about names (in subject) ? I mean spefication in C#. I often see names, like that: fields m_(short of type)NameFields local variables (short of type)NameVariable but in other sources: fields _NameFields .. etc. methods and properties. I know specifications in other languages but how is in .Net, C# Thanks Sorry for language :)
4
2288
by: glenn | last post by:
Is there a way to loop through a classes fields without first knowing what fields are there? Trying to write a generic class that could build a query based on a classes fields and types. This would save me having to write a method for each class I write to handle its database access and could have a single function to handle all classes. Not thinking it can be done but figured I'd ask... Thanks,
5
14020
by: Genboy | last post by:
My "VIS" Website, which is a C# site created in VS.NET, Framework 1.1, is no longer compiling for me via the command line. As I have done 600 times in the last year and a half, I can compile to VIS.DLL via Visual Studio, with no problems: ------ Rebuild All started: Project: VIS, Configuration: Debug .NET ------ Preparing resources... Updating references...
9
6172
by: Charles Law | last post by:
Sorry for asking this one again, but with the newsgroup now holding things for an ever shorter time, I can't find my original question (and the answers) from a few months ago. I know that structures in VB.NET don't have bit fields, but I am looking for a convenient way to reproduce the convenience afforded in C/C++. If I have typedef struct _MyStruct {
12
3023
by: Dennis | last post by:
I am very clear on Properties and Methods and I think I understand Fields but not sure about this. Do I have the items marked properly in the below class? Any clarification would be appreciated. Thanks. Public Class MyClass 'This is a field Public myfield as string 'This is a property
60
5021
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this. consistently may make the code easier to understand for someone else, etc. Using "this." makes it immediately clear, for example, that the variable being referred to is a member of the same class and is not declared in the method as a local variable. ...
22
1589
by: WXS | last post by:
Sometimes a method in a class requires the use of class instance variables/fields that will not be used outside of the method itself. Currently this means you must create a instance field in the class such that from a maintenance stand point it is disconnected from the method, and also affords the opportunity for other methods to mess with the variable when they never should. For example: public class MyClass
7
2106
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9 ----------------------------------------------------------------------------- This is a consortium of ideas from another thread on topic ----------------------------------------------------------------------------- One of the big issues of organizing items within a class, is there are many...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8638
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7381
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6191
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.