473,491 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is this really a good construction

Hello!

A have this small FileInformation class
public class
{
public string Id { get; set; }
}

In some other place in the code I have this kind of statement
anObject.Add(new FileInformation {Id = strCurrentFileId});

The definition of Id in FileInformation is completely new to me because this
doesn't
exist in 2.0.

Does this mean that you define both a field and a propety(get and set) with
name id with access right as
public ?

I have been learned that defining a field as public is not a good thing to
use OOP

//Tony

Sep 16 '08 #1
2 917
On Sep 16, 2:14*pm, "Tony Johansson" <t.johans...@logica.comwrote:
A have this small FileInformation class
public class
{
* * public string Id { get; set; }

}

In some other place in the code I have this kind of statement
anObject.Add(new FileInformation {Id = strCurrentFileId});

The definition of Id in FileInformation is completely new to me because this
doesn't exist in 2.0.

Does this mean that you define both a field and a propety(get and set) with
name id with access right as public ?
The field is private, the property is public. This is called an
automatically implemented property.
I have been learned that defining a field as public is not a good thing to
use OOP
Indeed, but an automatically implemented property doesn't do that.

The way of creating the FileInformation instance is called an object
initializer btw, and is also new with C# 3. It doesn't rely on the
property being an automatically implemented one though.

Jon
Sep 16 '08 #2
"Tony Johansson" <t.*********@logica.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
Hello!

A have this small FileInformation class
public class
{
public string Id { get; set; }
}

In some other place in the code I have this kind of statement
anObject.Add(new FileInformation {Id = strCurrentFileId});

The definition of Id in FileInformation is completely new to me because
this doesn't
exist in 2.0.

Does this mean that you define both a field and a propety(get and set)
with name id with access right as
public ?

I have been learned that defining a field as public is not a good thing to
use OOP
Its short hand syntax for :-

string m_Id;
public string Id
{
get { return m_Id; }
set { m_Id = value; }
}

Note the field is not defined public its private but the property methods
get and set are available publically.

An alternative would be:-

public class FileInformation
{
public FileInformation(string id) { Id = id; }
public string Id { get; private set; }
}

anObject.add( new FileInformation(strCurrentField));

This would allow for the construction of the object given an ID but the id
would then be read-only to code external to the class.

--
Anthony Jones - MVP ASP/ASP.NET

Sep 16 '08 #3

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

Similar topics

761
27978
by: Neo-LISPer | last post by:
Hey Recently, I researched using C++ for game programming and here is what I found: C++ game developers spend a lot of their time debugging corrupted memory. Few, if any, compilers offer...
3
2440
by: Timothy Madden | last post by:
I know I can call destructors if I want (probably with their fully qualified name). However I don't see the point here. When and where would I want to call destructors ? Destructors are not...
2
1452
by: grahamo | last post by:
Hi, I realise that c++ knows nothing about threads however my question is related to an (excellent) article I was reading about threads and C++. For all intents and purposes we can forget the...
72
5732
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
7
1355
by: Pelle Beckman | last post by:
Hi, I've just finished writing a template (whew!) and would like some opinions on the design. The goal was to be able to do fairly simple singletons (no excplicit thread-safety, etc) and...
13
1607
by: Burt Johnson | last post by:
I have to use a COM DLL written by another person, and am having trouble. Part of the problem is that I am writing inside another DLL (non-COM), and do not know what has been CoInitialized or...
131
6008
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
19
8587
by: Dr Mephesto | last post by:
Hi! I would like to create a pretty big list of lists; a list 3,000,000 long, each entry containing 5 empty lists. My application will append data each of the 5 sublists, so they will be of...
17
5786
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
7154
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,...
0
7190
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...
1
6858
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...
0
5451
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,...
1
4881
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...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1392
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 ...
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
280
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...

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.