Reputation: 2664
I've been messing around in C# a little bit and I've stumbled into a problem for which I haven't managed to find an answer. I created two objects in main from another .cs file and I would like to assign a value for a variable associated with the object in another class. After about an hour of looking, I have no idea how to do it. Here is my code (I know it's not perfect; I'm just starting to learn):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Converse
{
class Program
{
static void Main(string[] args)
{
Subject subject1 = new Subject();
Subject subject2 = new Subject();
Generate.generation();
}
}
}
Then
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Converse
{
public class Subject
{
public int persuasion;
public Subject()
{
persuasion = 0;
}
}
}
And on this last one, lines 17 and 19 are the problem:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Converse
{
public static class Generate
{
public static void generation()
{
Console.WriteLine("Control over Subject 1's personality? ");
Console.WriteLine("1) None");
Console.WriteLine("2) Minimal");
Console.WriteLine("3) Considerable");
subject1.persuasion = Console.ReadKey();
switch (subject1.persuasion)
{
case 1:
break;
case 2:
GenerationFunctions.lightGen();
break;
case 3:
GenerationFunctions.heavyGen();
break;
default:
break;
}
Console.ReadKey();
}
}
}
Can anybody tell me how to code this properly?
Thanks
Upvotes: 1
Views: 102
Reputation: 17223
There are multiple issues with your code. You need to pass a reference to your subject to the generation void.
public static void generation(Subject subject)
{
Console.WriteLine("Control over Subject 1's personality? ");
Console.WriteLine("1) None");
Console.WriteLine("2) Minimal");
Console.WriteLine("3) Considerable");
subject.persuasion = Convert.ToInt32(Console.ReadKey());
switch (subject.persuasion)
{
case 1:
break;
case 2:
GenerationFunctions.lightGen();
break;
case 3:
GenerationFunctions.heavyGen();
break;
default:
break;
}
}
then to call:
Subject subject1 = new Subject();
Generate.generation(subject1);
Upvotes: 1