Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class Property Question 2

Status
Not open for further replies.

urtlking2jo

IS-IT--Management
Jul 25, 2003
4
I'm trying to build an additional property for an "Installation" class. Sometimes "Name" goes first, sometimes "InstallationType" goes first. VStudio doesn't give me much, but says to make sure I don't have an infinite loop/recursion. Any ideas? Thanks!

public partial class Installation
{
public string FullName
{
set { FullName = value; }
get
{
if (InstallationTypeFirst)
FullName = InstallationType + " " + Name;
else
FullName = Name + InstallationType;
return FullName;
}
}
}
 
Why would you want to set the same property you are trying to get?

Try something like this:

Code:
if (InstallationTypeFirst)
    return InstallationType + " " + Name;
        else
    return Name + InstallationType;

Otherwise, try applying your formatting logic in the set.

Your naming convention doesn't seem to make a lot of sense. Seems you should be setting a "Name" property that you can assign to, then exposing a read-only "FullName" property. If you need to concatenate that InstallationType to get the full name, its' not really teh full name you are setting in "set", ya know?

Hope this helps,

Alex

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Yes vstudio is right you are having a recusive loop. try this.
always use private members to store your data in and use the properties for public access.

Code:
public partial class Installation
    {
        private string fullName;

        public string FullName
        {
            set { fullName = value; }
            get
            {
                if (InstallationTypeFirst)
                        fullName = InstallationType + " " + Name;
                else
                    fullName = Name + InstallationType;
                return fullName;
            }
        }
    }

Christiaan Baes
Belgium

The future is out there
 
Alex,

Thank you for your help. Maybe I am thinking about this backward - I am new to C#.

Here's what my "Installation" object has:
A string "Name" - ie "Hood"
A string "Type" - ie "Fort"
A bool "InstallationTypeFirst" - ie "True"

I would like to create a property (Installation.FullName) that will provide me with "Fort Hood".

Your change at least got my code to compile, but am I understanding the get/set correctly?
 
Thanks Alex and Christiaan, with your input I just got it working correctly - and I think I understand it. Christiaan, the recursiveness of my original code now makes sense to me. I'll be sure to use private members to store temporary data.
Thanks again to both!
Josh
 
BTW I think you need a readonly property which means you can leave out the set statement because the user need to set the values via the name and location properties not directly via the FullNmae property.

Christiaan Baes
Belgium

The future is out there
 
A naming convention that I use to (help) prevent loops like this is:

- Prefix class-level members with an underscore
- Properties and Methods are Pascal Case, with leading uppercase letters
- Parameters to methods are Camel Case, with leading lowercase letters

Code:
internal class Foo
{
  // class members
  private string _firstName;
  private DateTime _birthDate;

  // properties
  public string FirstName
  {
     get { return _firstName; }
     set { _firstName = value; }
  }
  public DateTime BirthDate
  {
    get { return _birthDate; }
    set { _birthDate = value; }
  }

  // constructor (with parameters)
  public Foo(string firstName, DateTime birthDate)
  {
    _firstName = firstName;
    _birthDate = birthDate;
  }
}

You'll also run across people who prefix their private members with m_, which works too, but usually tags them as either coming from C++ or a VB6 background.
:)

Chip H.


____________________________________________________________________
www.chipholland.com
 
Just because Microsoft makes something possible, doesn't mean you have to use it.

I lump anonymous methods in that category.
(I think they reduce reusability)

Chip H.


____________________________________________________________________
www.chipholland.com
 
That holds true only if you're using your properties purely to access your private members. If you need to check for valid entries or anything you still need to do it the "old" way AFAIK


[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
The IT Community of the 21st Century
 
Yes, if you're using properties simply to make the private members visible outside the class, there's little reason to use them at all, either use the v3.0 technique, or just make the members public.

But one of the reasons to use properties is to give you a place to do things like validation, setting a "_isDirty" flag, or run a business rule or two.

IOW, it's a design choice -- if you're designing for maintainability (80% of the cost of software goes into the maintenance of it)

Chip H.


____________________________________________________________________
www.chipholland.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top