Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I use an object in a different class?
    primarykey
    data
    text
    <p>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):</p> <pre><code>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(); } } } </code></pre> <p>Then</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Converse { public class Subject { public int persuasion; public Subject() { persuasion = 0; } } } </code></pre> <p>And on this last one, lines 17 and 19 are the problem:</p> <pre><code>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(); } } </code></pre> <p>}</p> <p>Can anybody tell me how to code this properly?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload