Properties

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I have a class I set up where I can access my private variable directly or
through a property (name or Name).

I thought if it was private, I couldn't access it outside of the project?

using System;
using System.Collections.Generic;
using System.Text;

namespace TestInheritance
{
class Account
{
private double balance;
private double overDraftLimit;
private string name;

public string Name
{
get{return name;}
set { name = value; }
}
static void Main(string[] args)
{
Account a1 = new Account();
a1.Name = "WolfMan";
a1.name = "Franky"; <----- ???? shouldn't this be only
accessable inside class?
Console.Read();
}
}
}
 
tshad said:
I have a class I set up where I can access my private variable directly or
through a property (name or Name).

I thought if it was private, I couldn't access it outside of the project?

You can't access it outside the source code within the same class.

In your example, your "calling" code is in a Main method, but still in
the same class. Move it to a different class and you'll see it fail to
compile.
 
Jon Skeet said:
You can't access it outside the source code within the same class.

In your example, your "calling" code is in a Main method, but still in
the same class. Move it to a different class and you'll see it fail to
compile.

I had just figured that out and was just getting ready to post that.

Thanks,

Tom
 
Back
Top