Properties

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();
}
}
}
 
J

Jon Skeet [C# MVP]

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.
 
T

tshad

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Inheritance 11
interface?! 5
Pass values b/w forms using properties 4
Var vs Object type 4
Need help with calling one class from another 13
app.config configuration file 7
Generic 3
Indexed class / list 45

Top