Passing Objects and protection level

J

ji

I'm a vb programmer < flinch - duck>... trying to get into C#. All is good
up to this point but for a conceptual issue. I'm hoping somebody can help
me...

I'm having trouble modifying object properties in from a method in my main
class..
Below is a very stylized/pseudocode illustration of what I'm attempting..

ns
{
class1
{
main
{
class2 obj1 = new class2
call method2(pass obj1)
}

method2(class2)
{
obj1.property1 = <something else> //CANNOT ACCESS due to
'protection level'
}
}

class2
{
public property1
}
}//ns

I don't see what the problem is. I've pasted the schmoz of actual code..

if( YouCare && CanReadMyCludge )

just incase it is a syntax issue (which i doubt).

What am I not grasping? =(
A million thanks.

Pam


using System;

namespace Earth

{

/// <summary>

/// Summary description for Class1.

/// </summary>

///

class ProgramFlow

{

/// <summary>

/// The main entry point for the application.

/// </summary>



[STAThread]

static void Main(string[] args)

{

string Message = "So it is written...";

int currentYear = 7 * 1000;

GodSays("If a day is of a thousand years.... and today is garbage days
seven..\n(thinks hard while counting on fingers)..then..");

GodSays("The Current Year is " + currentYear);

Human myHuman = new Human();


GodSays("I have just created a new instance of the 'Human' Class.\n What
would you like to call it?");

myHuman.name = Console.ReadLine();

GodSays("The human shall be known as " + myHuman.name +".");


GodSays("I command thee to speak!");

myHuman.Talk("Uhhh..My name is " + myHuman.name+".?.?.");


GodSays("How old are you " + myHuman.name + "?");

myHuman.Talk("I am " + myHuman.Age + ".");

PassTimeYears( myHuman, 5, ref currentYear );

GodSays("How old are you now " + myHuman.name + "?");


myHuman.Talk("I am " + myHuman.Age + ".");



GodSays(Message);

Console.ReadLine();

}//Main







static void GodSays(string Report)

//A method that we use to communicate to the user.

{

Console.WriteLine("\nGod says: {0}\n", Report);


}//GodSays

public void PassTimeYears( Human AHuman, int yearz, ref int yearNow )

{

for ( int i = 0 ; i <= yearz ; i++ )

{

yearNow++;

AHuman.age++;


GodSays("Happy New Year!\nCurrent Year: " + yearNow );

}//for

}//PassTimeYears



}//Class ProgramFlow

public class Human

{

public const int MAXAGE = 120;

public string name;//public instance variable. Not good.

//An actual property..

int age = 0;

public int Age

{

get

{

return age;

}

set

{

age = (value > MAXAGE ? MAXAGE : value);

}

}//Age

//try creating an instance automagically...

//Human eve = new Human();


public void Talk(string message)

{

Console.WriteLine("{0} says: {1}",name, message);

}//Talk



}//class



}//namespace
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

It would help if you post real code :
call method2(pass obj1)

I don't think this will compile at all

Now, regarding your problem I bet it's cause property1 is read only , it
does have a get but not a set


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



ji said:
I'm a vb programmer < flinch - duck>... trying to get into C#. All is
good up to this point but for a conceptual issue. I'm hoping somebody can
help me...

I'm having trouble modifying object properties in from a method in my main
class..
Below is a very stylized/pseudocode illustration of what I'm attempting..

ns
{
class1
{
main
{
class2 obj1 = new class2
call method2(pass obj1)
}

method2(class2)
{
obj1.property1 = <something else> //CANNOT ACCESS due to
'protection level'
}
}

class2
{
public property1
}
}//ns

I don't see what the problem is. I've pasted the schmoz of actual code..

if( YouCare && CanReadMyCludge )

just incase it is a syntax issue (which i doubt).

What am I not grasping? =(
A million thanks.

Pam


using System;

namespace Earth

{

/// <summary>

/// Summary description for Class1.

/// </summary>

///

class ProgramFlow

{

/// <summary>

/// The main entry point for the application.

/// </summary>



[STAThread]

static void Main(string[] args)

{

string Message = "So it is written...";

int currentYear = 7 * 1000;

GodSays("If a day is of a thousand years.... and today is garbage days
seven..\n(thinks hard while counting on fingers)..then..");

GodSays("The Current Year is " + currentYear);

Human myHuman = new Human();


GodSays("I have just created a new instance of the 'Human' Class.\n What
would you like to call it?");

myHuman.name = Console.ReadLine();

GodSays("The human shall be known as " + myHuman.name +".");


GodSays("I command thee to speak!");

myHuman.Talk("Uhhh..My name is " + myHuman.name+".?.?.");


GodSays("How old are you " + myHuman.name + "?");

myHuman.Talk("I am " + myHuman.Age + ".");

PassTimeYears( myHuman, 5, ref currentYear );

GodSays("How old are you now " + myHuman.name + "?");


myHuman.Talk("I am " + myHuman.Age + ".");



GodSays(Message);

Console.ReadLine();

}//Main







static void GodSays(string Report)

//A method that we use to communicate to the user.

{

Console.WriteLine("\nGod says: {0}\n", Report);


}//GodSays

public void PassTimeYears( Human AHuman, int yearz, ref int yearNow )

{

for ( int i = 0 ; i <= yearz ; i++ )

{

yearNow++;

AHuman.age++;


GodSays("Happy New Year!\nCurrent Year: " + yearNow );

}//for

}//PassTimeYears



}//Class ProgramFlow

public class Human

{

public const int MAXAGE = 120;

public string name;//public instance variable. Not good.

//An actual property..

int age = 0;

public int Age

{

get

{

return age;

}

set

{

age = (value > MAXAGE ? MAXAGE : value);

}

}//Age

//try creating an instance automagically...

//Human eve = new Human();


public void Talk(string message)

{

Console.WriteLine("{0} says: {1}",name, message);

}//Talk



}//class



}//namespace
 
J

ji

Hi Ignacio. Thanks for the reply.

I did paste the actual code at the end of the post. As you may see, the
property in question (Age) is public and has a set method. Still stumped.


Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

It would help if you post real code :
call method2(pass obj1)

I don't think this will compile at all

Now, regarding your problem I bet it's cause property1 is read only , it
does have a get but not a set


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



ji said:
I'm a vb programmer < flinch - duck>... trying to get into C#. All is
good up to this point but for a conceptual issue. I'm hoping somebody can
help me...

I'm having trouble modifying object properties in from a method in my
main class..
Below is a very stylized/pseudocode illustration of what I'm attempting..

ns
{
class1
{
main
{
class2 obj1 = new class2
call method2(pass obj1)
}

method2(class2)
{
obj1.property1 = <something else> //CANNOT ACCESS due to
'protection level'
}
}

class2
{
public property1
}
}//ns

I don't see what the problem is. I've pasted the schmoz of actual code..

if( YouCare && CanReadMyCludge )

just incase it is a syntax issue (which i doubt).

What am I not grasping? =(
A million thanks.

Pam


using System;

namespace Earth

{

/// <summary>

/// Summary description for Class1.

/// </summary>

///

class ProgramFlow

{

/// <summary>

/// The main entry point for the application.

/// </summary>



[STAThread]

static void Main(string[] args)

{

string Message = "So it is written...";

int currentYear = 7 * 1000;

GodSays("If a day is of a thousand years.... and today is garbage days
seven..\n(thinks hard while counting on fingers)..then..");

GodSays("The Current Year is " + currentYear);

Human myHuman = new Human();


GodSays("I have just created a new instance of the 'Human' Class.\n What
would you like to call it?");

myHuman.name = Console.ReadLine();

GodSays("The human shall be known as " + myHuman.name +".");


GodSays("I command thee to speak!");

myHuman.Talk("Uhhh..My name is " + myHuman.name+".?.?.");


GodSays("How old are you " + myHuman.name + "?");

myHuman.Talk("I am " + myHuman.Age + ".");

PassTimeYears( myHuman, 5, ref currentYear );

GodSays("How old are you now " + myHuman.name + "?");


myHuman.Talk("I am " + myHuman.Age + ".");



GodSays(Message);

Console.ReadLine();

}//Main







static void GodSays(string Report)

//A method that we use to communicate to the user.

{

Console.WriteLine("\nGod says: {0}\n", Report);


}//GodSays

public void PassTimeYears( Human AHuman, int yearz, ref int yearNow )

{

for ( int i = 0 ; i <= yearz ; i++ )

{

yearNow++;

AHuman.age++;


GodSays("Happy New Year!\nCurrent Year: " + yearNow );

}//for

}//PassTimeYears



}//Class ProgramFlow

public class Human

{

public const int MAXAGE = 120;

public string name;//public instance variable. Not good.

//An actual property..

int age = 0;

public int Age

{

get

{

return age;

}

set

{

age = (value > MAXAGE ? MAXAGE : value);

}

}//Age

//try creating an instance automagically...

//Human eve = new Human();


public void Talk(string message)

{

Console.WriteLine("{0} says: {1}",name, message);

}//Talk



}//class



}//namespace
 
M

Metallikanz!

One place you surely will get the access violation error is the PassTimeYears() method where you are incrementing the age. The Human type you have defined has two members with the same name, different case though - age variable, which is private in this case and Age property which is public and has a Getter and Setter. You are trying to increment the value of the private variable instead of the property and hence the error. Correct the case and you will accessing the property instead and no error this time. You have followed the coding standard in which we don't directly expose class member variables to the outside world, instead wrap it around with properties the way you have done. I think you should follow a naming convention which is better than the difference in case thing you have now, prefix an _ or something for your private members, that would be much better.

HTH, Metallikanz!

ji said:
I'm a vb programmer < flinch - duck>... trying to get into C#. All is good
up to this point but for a conceptual issue. I'm hoping somebody can help
me...

I'm having trouble modifying object properties in from a method in my main
class..
Below is a very stylized/pseudocode illustration of what I'm attempting..

ns
{
class1
{
main
{
class2 obj1 = new class2
call method2(pass obj1)
}

method2(class2)
{
obj1.property1 = <something else> //CANNOT ACCESS due to
'protection level'
}
}

class2
{
public property1
}
}//ns

I don't see what the problem is. I've pasted the schmoz of actual code..

if( YouCare && CanReadMyCludge )

just incase it is a syntax issue (which i doubt).

What am I not grasping? =(
A million thanks.

Pam


using System;

namespace Earth

{

/// <summary>

/// Summary description for Class1.

/// </summary>

///

class ProgramFlow

{

/// <summary>

/// The main entry point for the application.

/// </summary>



[STAThread]

static void Main(string[] args)

{

string Message = "So it is written...";

int currentYear = 7 * 1000;

GodSays("If a day is of a thousand years.... and today is garbage days
seven..\n(thinks hard while counting on fingers)..then..");

GodSays("The Current Year is " + currentYear);

Human myHuman = new Human();


GodSays("I have just created a new instance of the 'Human' Class.\n What
would you like to call it?");

myHuman.name = Console.ReadLine();

GodSays("The human shall be known as " + myHuman.name +".");


GodSays("I command thee to speak!");

myHuman.Talk("Uhhh..My name is " + myHuman.name+".?.?.");


GodSays("How old are you " + myHuman.name + "?");

myHuman.Talk("I am " + myHuman.Age + ".");

PassTimeYears( myHuman, 5, ref currentYear );

GodSays("How old are you now " + myHuman.name + "?");


myHuman.Talk("I am " + myHuman.Age + ".");



GodSays(Message);

Console.ReadLine();

}//Main







static void GodSays(string Report)

//A method that we use to communicate to the user.

{

Console.WriteLine("\nGod says: {0}\n", Report);


}//GodSays

public void PassTimeYears( Human AHuman, int yearz, ref int yearNow )

{

for ( int i = 0 ; i <= yearz ; i++ )

{

yearNow++;

AHuman.age++;


GodSays("Happy New Year!\nCurrent Year: " + yearNow );

}//for

}//PassTimeYears



}//Class ProgramFlow

public class Human

{

public const int MAXAGE = 120;

public string name;//public instance variable. Not good.

//An actual property..

int age = 0;

public int Age

{

get

{

return age;

}

set

{

age = (value > MAXAGE ? MAXAGE : value);

}

}//Age

//try creating an instance automagically...

//Human eve = new Human();


public void Talk(string message)

{

Console.WriteLine("{0} says: {1}",name, message);

}//Talk



}//class



}//namespace
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Sorry, I did not see it

The error is pretty obvious, age (with lowercase ) is private Age (with
uppercase) is public, you are making reference to the private member, not
the public one

remember that c# is case sensitive.


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation




ji said:
Hi Ignacio. Thanks for the reply.

I did paste the actual code at the end of the post. As you may see, the
property in question (Age) is public and has a set method. Still stumped.


Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

It would help if you post real code :
call method2(pass obj1)

I don't think this will compile at all

Now, regarding your problem I bet it's cause property1 is read only , it
does have a get but not a set


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



ji said:
I'm a vb programmer < flinch - duck>... trying to get into C#. All is
good up to this point but for a conceptual issue. I'm hoping somebody
can help me...

I'm having trouble modifying object properties in from a method in my
main class..
Below is a very stylized/pseudocode illustration of what I'm
attempting..

ns
{
class1
{
main
{
class2 obj1 = new class2
call method2(pass obj1)
}

method2(class2)
{
obj1.property1 = <something else> //CANNOT ACCESS due to
'protection level'
}
}

class2
{
public property1
}
}//ns

I don't see what the problem is. I've pasted the schmoz of actual code..

if( YouCare && CanReadMyCludge )

just incase it is a syntax issue (which i doubt).

What am I not grasping? =(
A million thanks.

Pam


using System;

namespace Earth

{

/// <summary>

/// Summary description for Class1.

/// </summary>

///

class ProgramFlow

{

/// <summary>

/// The main entry point for the application.

/// </summary>



[STAThread]

static void Main(string[] args)

{

string Message = "So it is written...";

int currentYear = 7 * 1000;

GodSays("If a day is of a thousand years.... and today is garbage days
seven..\n(thinks hard while counting on fingers)..then..");

GodSays("The Current Year is " + currentYear);

Human myHuman = new Human();


GodSays("I have just created a new instance of the 'Human' Class.\n What
would you like to call it?");

myHuman.name = Console.ReadLine();

GodSays("The human shall be known as " + myHuman.name +".");


GodSays("I command thee to speak!");

myHuman.Talk("Uhhh..My name is " + myHuman.name+".?.?.");


GodSays("How old are you " + myHuman.name + "?");

myHuman.Talk("I am " + myHuman.Age + ".");

PassTimeYears( myHuman, 5, ref currentYear );

GodSays("How old are you now " + myHuman.name + "?");


myHuman.Talk("I am " + myHuman.Age + ".");



GodSays(Message);

Console.ReadLine();

}//Main







static void GodSays(string Report)

//A method that we use to communicate to the user.

{

Console.WriteLine("\nGod says: {0}\n", Report);


}//GodSays

public void PassTimeYears( Human AHuman, int yearz, ref int yearNow )

{

for ( int i = 0 ; i <= yearz ; i++ )

{

yearNow++;

AHuman.age++;


GodSays("Happy New Year!\nCurrent Year: " + yearNow );

}//for

}//PassTimeYears



}//Class ProgramFlow

public class Human

{

public const int MAXAGE = 120;

public string name;//public instance variable. Not good.

//An actual property..

int age = 0;

public int Age

{

get

{

return age;

}

set

{

age = (value > MAXAGE ? MAXAGE : value);

}

}//Age

//try creating an instance automagically...

//Human eve = new Human();


public void Talk(string message)

{

Console.WriteLine("{0} says: {1}",name, message);

}//Talk



}//class



}//namespace
 
A

alantolan

It looks like a spelling error.

In 'PassTimeYears'

you are increming 'age' not 'Age'

AHuman.age++,

rather than

AHuman.Age++



One way to avoid situations like this is to use a naming convention for
the member variables. For an amount of the C# code samples out there
you will see somthing like


Class Human {

int _age;

Public int Age {

Get{ ... }
Set{ ... }

}

};


it is not the only way and some people disagre with the practice but it
can help, especially those coming from non-case sensitive backgrounds.


Also, other items

Why is GodSays() static, whilst PassTheYears() is not? Does
PassTheYears() work?



Alan.
 
J

Jon Skeet [C# MVP]

[Removed microsoft.public.dotnet.csharp.general, which isn't a valid
newsgroup.]

ji said:
I'm a vb programmer < flinch - duck>... trying to get into C#. All is good
up to this point but for a conceptual issue. I'm hoping somebody can help
me...

<snip>

There are two problems:

1) You're trying to access the variable, not the property - the
variable is called age, the property is called Age.

2) You're trying to access PassYears as if it were a static method, but
it's not.
 
J

ji

Oh my <turning red>. Thanks to all who replied. Ignacio, Alan, Metallikanz,
Jon.

Jon Skeet said:
[Removed microsoft.public.dotnet.csharp.general, which isn't a valid
newsgroup.]

ji said:
I'm a vb programmer < flinch - duck>... trying to get into C#. All is
good
up to this point but for a conceptual issue. I'm hoping somebody can help
me...

<snip>

There are two problems:

1) You're trying to access the variable, not the property - the
variable is called age, the property is called Age.

2) You're trying to access PassYears as if it were a static method, but
it's not.
 

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

Top