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