Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, March 25, 2012 12:27 PM
When calling a method from another class, which requires variables from the calling class, I'd normally send the variables in the method parameters.
But lately, I've learnt how to call the public get;set; methods of a variable and so I've been using this instead.
An example is below:
MyWindow class has a get;set; method for myInt. It calls 2 methods from Class2, one which uses parameters (myInt) and one which calls the MyWindow.MyInt get;set; method.
Which is the best way?
public partial class MyWindow : Window
{
private static int myInt;
public static int MyInt;
{
get { return myInt; }
set { myInt = value; }
}
public MyWindow()
{
InitinializeComponent();
Class2.MyMethod(MyInt);
Class2.MyMethod();
}
}
class Class2
{
// method with parameters
public static void MyMethod(int intIn)
{
Console.Writeln("" + intIn);
}
// method which calls get;set; method
public static void MyMethod()
{
Console.Writeln("" + MyWindow.MyInt);
}
}
All replies (4)
Sunday, March 25, 2012 12:46 PM ✅Answered | 1 vote
To use properties is a fine manner, and you can use it all over the place if you like - but its pointless in cases like
- that you need some variable only in some method (why then having a public property that can be accesed form every where; so property here not simply not needed).
- one more think to mention, property, like you declared it, it should be SET (write some value into it), and then GET (read value from it). If you will not do both, its again pointell to use it.
I actually dont see from your code that use SET it. You only GET it. But when calling 2nd method MyMethod (the parameterless one), it will return nothing, since you did not set the property.
--
The priority in use of property is that youu set it in one class, ang get it in another class. In this case you set the GET accessor as private. So from another class where you will read it, it cannot be changed (for example in your and mine example) we can get and set the property from any where.
To use your code,
public partial class MyWindow : Window
{
private static int myInt;
public static int MyInt;
{
get { return myInt; }
set { myInt = value; }
}
public MyWindow()
{
InitinializeComponent();
//1st: SET IT:
MyInt = 2;
Class2 c2 = new Class2();
c2.MyMethod();
}
}
class Class2
{
public void MyMethod()
{
Console.Writeln("My number is: " + MyWindow.MyInt); //2nd GET IT!
}
}
Mitja
Sunday, March 25, 2012 4:30 PM ✅Answered
Hi,
Few of my words,
Do you have concern on performance?
JIT compiler inlines the code of property's Set and Get accessor methods so performance wise same as using fields.
Usage wise,
With MyMethod(int intIn), method you are passing value, property value will be copied to local variableand whatever changes happen to this will not effect to original property value.Since, you define property, MyInt, with public Set and Get, you can directly access in Class2.
If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
Sunday, March 25, 2012 4:55 PM ✅Answered
Here is a general rule of thumb. Data that is passed into a constructor should define properties, fields and behaviors that generally will not change over the course of the lifetime of an object instance of the class. If the value, object or data will most likely change over the lifetime of an object instance, then use a Property and initialize it as a property.
Hope this helps.
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."
http://thesharpercoder.blogspot.com/
Sunday, March 25, 2012 5:16 PM ✅Answered
Class Design. I have not posted this in many, many months. Food for thought.
Treat class names as nouns, and method names as verbs. When you need to write code, you start of with a description of what it needs to do. Sometimes it is formally written out in plain English by you or your customer. That description is an excellent source for what classes you need and what methods you should deploy where. Use this acid test on your class and method names.
The ClassName, MethodName itself. < Apply this to classes that you use to create object instances.
You should be able to create a sensible sentence out of it. You may have to add stuff like "for", "to" or even the letter s. The result may even sound like broken Englilsh, but the meaning should still be clear. You can even apply this rule to properties....
The ClassName, GetPropertyName itself.
The ClassName, SetPropertyName itself.
...because properties get inlined into class member methods by the compiler. You can turn it around to determine how to name and design static methods and classes.
The MethodName, ClassName instance. < Apply this to classes that you use to define static methods.
But you are asking about passing variables and properties, and which to use.
Propertiy names are adjectives that can describe the object. For example, a House class may have a Color property. house.Color = Color.Red; (A house color is not likely to change, but setting Color as a property might be more intuitive to read.)
A House class may have an UnlockDoor or OpenWindow methods, which could accept parameters that define which door or window. house.UnlockDoor(house.FrontDoor); (The house knows where the door or window is located and knows how to manipulate it itself.
A House class may have a FireAlarm or IntrusionDetected events. house.FireAlarm += house_FireAlarm; Event names usually contained a conjugated verb that describes what occured or is occurring.
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."