473,687 Members | 3,084 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

property in a class

Hi ,

Can i populate the class variables in following fashion?

public class abc
{
string name;

public string Name
{
set
{
name = value;
}
}
public abc()
{
}
}
abc myclass1 = new abc()
abc.Name="googl e";

or

should I always populate through constructor or any other method of
class abc?

thanks

Vijay
Jun 27 '08 #1
4 1060
On May 28, 4:22 pm, vijaysambhe <vsamb...@gmail .comwrote:
Can i populate the class variables in following fashion?
<snip>

Absolutely - that's what most writable properties look like.
or

should I always populate through constructor or any other method of
class abc?
Well, if you want to create an immutable type you need to pass
everything to the constructor, but if you're okay with mutability,
there's nothing wrong with using a writable property.

Jon
Jun 27 '08 #2
Thanks Jon for your prompt reply.

regards
Vijay
On May 28, 4:31*pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
On May 28, 4:22 pm, vijaysambhe <vsamb...@gmail .comwrote:
Can i populate the class variables in following fashion?

<snip>

Absolutely - that's what most writable properties look like.
or
should I always populate through constructor or any other method of
class abc?

Well, if you want to create an immutable type you need to pass
everything to the constructor, but if you're okay with mutability,
there's nothing wrong with using a writable property.

Jon
Jun 27 '08 #3
I think you made a typo below. It should be:

abc myclass1 = new abc();
myclass1.Name=" google"; // apc.Name will not be correct...
"vijaysambh e" wrote:
Hi ,

Can i populate the class variables in following fashion?

public class abc
{
string name;

public string Name
{
set
{
name = value;
}
}
public abc()
{
}
}
abc myclass1 = new abc()
abc.Name="googl e";

or

should I always populate through constructor or any other method of
class abc?

thanks

Vijay
Jun 27 '08 #4
Can i populate the class variables in following fashion?
Of course, there is nothing wrong with that
>
or

should I always populate through constructor or any other method of
class abc?
It depends of the class, if the class needs to ALWAYS have a value for
a given property then you use yor constructor for that.
Also providing a constructor with parameters let you do something
like:
callAMethod( new MyClass( ......) );
without having to create an instance of MyClass (note that in 3.5 this
is no longer valid).

BTW, to a class like yours it's usually called to have a "chatty
interface"
Jun 27 '08 #5

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

Similar topics

3
2488
by: Johnny M | last post by:
using Access 2003 Pardon the subject line, but I don't have a better word for this strange behavior (or behavior I don't understand!!!) I have a class module named DepreciationFactor. One of the properties is a follows (irrelevant code omitted):
3
1775
by: Erik Harris | last post by:
I apologize if this is a stupid question - I'm relatively new to OOP. I have a property that must exist in a class in order to be used by another class. The property, however, does not change with each instance (it returns an instance of a delegate that points to the same method no matter what the instance). I thought the best way to make sure that Class1 could be used by Class2 would be to create an interface that defined this property,...
7
6620
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
0
5565
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control is used to view the state of the running jobs and schedule new jobs. The control also runs in the context of Internet Explorer (we do this so the administrators of the jobs can always receive the latest control). The property grid is used to...
3
6754
by: Marty McFly | last post by:
Hello, I have a control class that inherits from System.Web.UI.WebControls.Button. When I drag this control from the "My User Controls" tab in the toolbox onto the form, I want it to reflect the following default properties: Height = 32px, Width = 144px. I declare the Width property in my control as... \\\
0
1448
by: Jordan Bowness | last post by:
I make a similar post in another newsgroup, but this example is simplified somewhat. I have a component (cmpMyComponent) with 2 properties. The 1st property is a string value (Description) and the 2nd property is a strongly typed collection class (myCollectionProperty). The collection contains a simple class (myCustomClass) which has 1 text property (TextProperty).
2
3198
by: miben | last post by:
I need to set a variable returned by a readonly property in a class by another class. So the only way to set that value is from a specific class and function. Public Sub Main Dim setter As New GoingToSetYou Dim item As New ToBeSet setter.NewItem(item) Dim sValue As String = item.Value
2
2203
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment i'm using the treenodes and each treenode needs a unique entry into my rich text box. After sitting at home with MSDN i've managed to get this functionality by storing a RTF string in the tag property of the treenode. On every 'before update' of the...
8
2019
by: Al | last post by:
I'd like to create Class Library in VB 2005, which has a property accessible by external programs. I decided to include 1 Class with 1 property in this project. I placed this code in Class: Public Class COM Private mMyProp As String Public Property MyProp() As String
14
2411
by: Dom | last post by:
Hi all I'm developing a control, and I need to hide some properties to the user. For example, suppose I need Text property to be completely inacessible (from a Form/Code that is into another project/assembly). I tried with attributes: <Browsable(False), _ EditorBrowsable(EditorBrowsableState.Never), _ RefreshProperties(RefreshProperties.Repaint), _
0
8590
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8527
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8783
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
7618
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...
0
4542
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2961
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
3
1947
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.