473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Passing Objects and protection level

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


Nov 17 '05 #1
7 949
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" <jj@gg.com> wrote in message news:8%oMe.29545$vj.3121@pd7tw1no...
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

Nov 17 '05 #2
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 )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:e1**************@TK2MSFTNGP14.phx.gbl...
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" <jj@gg.com> wrote in message news:8%oMe.29545$vj.3121@pd7tw1no...
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


Nov 17 '05 #3
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" <jj@gg.com> wrote in message news:8%oMe.29545$vj.3121@pd7tw1no...
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



Nov 17 '05 #4
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.

Nov 17 '05 #5
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" <jj@gg.com> wrote in message news:SrpMe.225868$5V4.58401@pd7tw3no...
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 )" <ignacio.machin AT dot.state.fl.us>
wrote in message news:e1**************@TK2MSFTNGP14.phx.gbl...
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" <jj@gg.com> wrote in message news:8%oMe.29545$vj.3121@pd7tw1no...
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



Nov 17 '05 #6
[Removed microsoft.public.dotnet.csharp.general, which isn't a valid
newsgroup.]

ji <jj@gg.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
ji
Oh my <turning red>. Thanks to all who replied. Ignacio, Alan, Metallikanz,
Jon.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
[Removed microsoft.public.dotnet.csharp.general, which isn't a valid
newsgroup.]

ji <jj@gg.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #8

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

Similar topics

7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
48
by: Andrew Quine | last post by:
Hi Just read this article http://www.artima.com/intv/choices.html. Towards the end of the dicussions, when asked "Did you consider including support for the concept of immutable directly in C#...
1
by: Larry | last post by:
I have a VB background and am developing a new windows app in Csharp. I'm getting the error. 'inaccessible due to its protection level' I've added a TextBox1 and a Button1 to a form. I...
3
by: xenophon | last post by:
This following innocuous code: System.IO.DirectoryInfo fff = new System.IO.DirectoryInfo(); System.IO.FileInfo ppp = fff.GetFiles( Request.MapPath(".") ); for( int ccc=0 ; ccc < ppp.Length ;...
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
10
by: Stan | last post by:
There are two ways to pass structured data to a web service: xml === <Order OrderId="123" OrderAmount="234" /> or class =====
1
by: musosdev | last post by:
Hi I've got a project I've just run through the conversion wizard, and it's giving me a few headaches. I've got a user control which has controls referrenced from its calling page...
3
by: andreas.baus | last post by:
Hello. I'm trying to exchange data between a webservice based on Apache Axis and a client written in C#, and so far it has been working with few problems (none that I couldn't solve myself with...
0
by: Vinod Sadanandan | last post by:
STANDBY DATABASE MONITORING & PROTECION MODES (9iR2) This document is written for understanding and monitoring standby database configured with diffrent protection modes . MAXIMUM PROTECTION ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.