accessor

What is the best way to access properties from the same class, via accessors or directly? [closed]

为君一笑 提交于 2019-11-27 22:35:39
This is something I'm not much consistent about and always curious about what other people do. How do you access internal properties (private or public)? For example you've got this property : Private _Name As String Public Property Name() As String Get Return _Name End Get Set(ByVal value As String) _Name = value End Set End Property In the same class within another function which one do you prefer? and why? _Name = "Johnny" or Name = "Johnny" Ignore the fact that I used Name instead of Me.Name. Personally I prefer to use the property where possible. This means you still get the validation,

Does it make sense to provide non-const reference getter

余生长醉 提交于 2019-11-27 18:23:13
问题 Sometimes I need to expose some of the class members. For example in the following example class Mechanic may need direct access to Engine component. I have read many times that all fields should be accessed by mutator (accessor) methods because of several reasons. But is there any advantage when providing non-const reference getter: class Car { public: Engine & engine() { return m_engine; } //as a consequence you will also need to provide const version const Engine & engine() const { return

static/Shared in VB.NET and C# visibility

风流意气都作罢 提交于 2019-11-27 15:09:36
I have faced with a situation in VB.NET and C# (.NET2) with the visibility of the static/shared members. It seems to me a little strange in VB.NET: public class A { private static A instance; public static A Instance { get { return instance; } } public string Name { get { } } } usage : A.Instance.Name // ONLY Name is "visible" VB.NET: Public Class A Private Shared _instance As A Public Shared ReadOnly Property Instance() As A Get Return _instance End Get End Property Public ReadOnly Property Name() As String Get Return "" End Get End Property End Class usage : A.Instance.Instance.Instance

Why Automatically implemented properties must define both get and set accessors

╄→尐↘猪︶ㄣ 提交于 2019-11-27 14:23:07
问题 When we define a property like public string Name {get; set;} dot net can make our properties code. but when we use public string Name {get;} public string Name {set;} we face with 'Hajloo.SomeThing.PropertyName.set' must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors. Actually why the compiler can't determine the property and make code automatically? What's the problem? 回答1: Because the auto-implemented

attr_accessor strongly typed Ruby on Rails

核能气质少年 提交于 2019-11-27 12:21:34
问题 Just wondering if anyone can shed some light on the basics of getter setters in Ruby on Rails with a view on strongly typed. I am very new to ruby on rails and predominately have a good understanding of .NET. For example, let's consider we have a .net class called Person class Person { public string Firstname{get;set;} public string Lastname{get;set;} public Address HomeAddress{get;set;} } class Address { public string AddressLine1{get;set;} public string City{get;set;} public string Country

Using reflection to set an object property

筅森魡賤 提交于 2019-11-27 11:05:35
问题 I getting class by name and i need to update them with respective data and my question is how to do it with java I want to add the method some dummy data . I don't know the class type I just getting the class name and use reflection to get his data I use this code to get the class instance and Class<?> classHandle = Class.forName(className); Object myObject = classHandle.newInstance(); // iterate through all the methods declared by the class for (Method method : classHandle.getMethods()) { //

Directly accessing an instance variable vs. Using an accessor method

独自空忆成欢 提交于 2019-11-27 09:15:25
问题 Can anyone explain the difference between accessing an instance attribute via self.attribute and by @attribute ? 回答1: self.attribute calls the method attribute . self.attribute = value calls the method attribute= with the argument value . @attribute and @attribute = value get/set the value of the instance variable @attribute . So basically they're two entirely different things. However if you call attr_accessor :attribute it defines the method attribute to return @attribute and the method

Why can't we assign a foreach iteration variable, whereas we can completely modify it with an accessor?

六眼飞鱼酱① 提交于 2019-11-27 07:32:58
I was just curious about this: the following code will not compile, because we cannot modify a foreach iteration variable: foreach (var item in MyObjectList) { item = Value; } But the following will compile and run: foreach (var item in MyObjectList) { item.Value = Value; } Why is the first invalid, whereas the second can do the same underneath (I was searching for the correct english expression for this, but I don't remember it. Under the...? ^^ ) foreach is a read only iterator that iterates dynamically classes that implement IEnumerable, each cycle in foreach will call the IEnumerable to

Why does attr_accessor clobber the existing variables in this model in Ruby on Rails?

只谈情不闲聊 提交于 2019-11-27 07:19:48
问题 I was bitten by this recently, and it'd be useful to know precisely what's happening to make this happen, so others avoid this mistake. I have a model User, with a schema like so: create_table "users", :force => true do |t| t.string "user_name" t.string "first_name" t.string "last_name" t.string "email" t.string "location" t.string "town" t.string "country" t.string "postcode" t.boolean "newsletter" In the class user.rb, I have a attr_accessor for three methods: class User < ActiveRecord:

Conventions for accessor methods (getters and setters) in C++

陌路散爱 提交于 2019-11-27 03:03:16
Several questions about accessor methods in C++ have been asked on SO, but none was able satisfy my curiosity on the issue. I try to avoid accessors whenever possible, because, like Stroustrup and other famous programmers, I consider a class with many of them a sign of bad OO. In C++, I can in most cases add more responsibility to a class or use the friend keyword to avoid them. Yet in some cases, you really need access to specific class members. There are several possibilities: 1. Don't use accessors at all We can just make the respective member variables public. This is a no-go in Java, but