what is the difference between field and variable

T

Tony Johansson

Hello!

Can somebody explain the difference between field and variables.
For me they are more or less the same.
Two words for ther same thing

//Tony
 
A

andy.johnstone

Can somebody explain the difference between field and variables.
For me they are more or less the same.
Two words for ther same thing

Not quite. A variable is a value that stores a value or reference. A
field though is a member of a type which can be instance or static.
So variable is the generic catchall term, while field is something
more specific.
 
J

Jeff Johnson

Can somebody explain the difference between field and variables.
For me they are more or less the same.
Two words for ther same thing

To expound on what Andy said, a field is declared outside of all methods, so
it is accessible anywhere in the class. In VB6 we called this a
"module-level variable."
 
F

Family Tree Mike

Tony Johansson said:
Hello!

Can somebody explain the difference between field and variables.
For me they are more or less the same.
Two words for ther same thing

//Tony

Fields just hold a value that can be set or retrieved.

Properties have get and set methods, that may do more than return or set a
value. For example, a set method may trigger the raising of an ObjectChanged
event.

Properties can be used for databinding to controls, where fields, to my
knowledge, cannot be bound.

Mike
 
P

proxyuser

Tony Johansson said:
Hello!

Can somebody explain the difference between field and variables.
For me they are more or less the same.
Two words for ther same thing

A field is a member variable. A variable might be a member variable or it
might not.

class A
{

int field1; // this is a field and is also a variable (a member
variable)

void f()

{

int variable1; // this is a variable, not a field (it is not a
member of any class)

}

}
 
P

proxyuser

Not quite. A variable is a value that stores a value or reference.

It isn't accurate to call a variable a value. A variable is basically a
name for a memory location used to store a value.
 
P

Pavel Minaev

Can somebody explain the difference between field and variables.
For me they are more or less the same.
Two words for ther same thing

There is no universal definition for "variable" when it comes to
programming languages - it is different between them, though sometimes
can be similar in closely related ones. In case of C#, it's simple:
variables are local, fields belong to an object instance or a class.
Both are memory locations (lvalues in C/C++ parlance), which is why
you can use either with ref/out parameters, for example. It's really
all there is to it - it's purely a matter of the definition of the
term.
 
P

Pavel Minaev

It's worse than that.  There really is no authoritative definition for  
"variable", other than the most obvious one: something that can store a  
value that varies.  And even that can be subtly misleading; for example,  
the XSLT language has "variables", but they only "vary" in the sense that 
they can be initialized with non-constant expressions.

That's what I mean. It's not just XSLT either, it's any pure
functional language (other examples would be XQuery and Haskell). Some
of those deliberately use the term "binding" instead of "variable" to
highlight the difference, but this is far from universal.
It's not that simple.  In C# (that is, in the language specification), the  
word "variable" literally applies to any storage location that can vary.  
The specification calls out seven different kinds of variables: "static  
variables", "instance variables", "array elements", "value parameters",  
"reference parameters", "output parameters", and "local variables" (of  
course, within those categories there are variables that are very similar,  
such as the three "parameter" categories).

C# does refer to "static variables" and "instance variables" as "fields", 
so a "field" is simply a special case of a "variable".

Thanks for the clarification.
 
P

proxyuser

Jeff Johnson said:
Are there such things as static variables that AREN'T member variables?

I guess in C#, "static variables" are member fields. I figured there also
existed a similar concept to static in C++, but maybe not.
 
M

Martin Maat

Tony said:
Can somebody explain the difference between field and variables.
For me they are more or less the same.
Two words for ther same thing

The term field refers to a part of a bigger structure, like a database
record or a C# struct or class. The term variable just means that it can be
modified. Database people generally talk about fields as part of a table
definition.

Another term is "members". This also implies "tied to some bigger
structure", like field, but is a broader term for methods are also
considered members. It just depends on your angle.

In older languages you can have variables that are independent of any class.
Variables declared locally are not members, they live inside the scope of a
method ut aren't part of the class definition. Constant members and methods
are not variables.

Another comparison: arguments versus parameters. There is probably a similar
subtle difference between those. My Greek of Latin is not good enough to
explain this.

Martin.
 

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