-1

Basically:

class a constructs a user account object

class b instantiates the account object and holds it

class c holds a method that is referenced in class d

class d wants to reference a user account from class b for a method from class c

//constructs object
public class A
{
    public string firstname;
    public string lastname;

    public A(string givenname, string surname)
    {
        firstname = givenname;
        lastname = surname;
    }
}

//instantiates object from class A
public static class B
{
    public static void Accounts()
    {
        A PBeenis = new A("Paul", "Beenis");
    }
}

//holds a method that displays info about object
public static class C
{
    public static void Get_UserInfo(ref A name)
    {
        Console.WriteLine("Name: {0} {1}", name.firstname, name.lastname);
    }
}

//calls a method in class C and references object from class B
public static class D
{
    static void Main(string[] args)
    {
        C.Get_UserInfo(ref PBeenis);
    }
}

How do you reference the object PBeenis from Class B using the Method from class C in class D?

C.Get_UserInfo(ref PBeenis);

CS0103 The name 'Pbeenis' does not exist in the current context

Dylan Grove
  • 493
  • 1
  • 7
  • 17

2 Answers2

0

You should read about scope.

It's impossible to reference Pbeenis from that part of your code because it's out of the scope.

To overcome this, you need a way of getting the reference inside the scope.

For example, making Pbeenis a property of class A and accessing it like:

B.Pbeenis

Hope that helps.

AMZ
  • 540
  • 4
  • 15
0

Not sure what you are trying to achieve, but why not make the instantiation outside of the method ? Also no need to use the ref keyword.

public static class B
{
    public static A PBeenis = new A("Paul", "Beenis");
}

//calls a method in class C and references object from class B
public static class D
{
    static void Main(string[] args)
    {
        C.Get_UserInfo(B.PBeenis);
    }
}

Other option is to return the object in the method

//instantiates object from class A
public static class B
{
    public static A Accounts()
    {
        return new A("Paul", "Beenis");
    }
}
//calls a method in class C and references object from class B
public static class D
{
    static void Main(string[] args)
    {
        C.Get_UserInfo(B.Accounts());
    }
}

Greetings.

bluedot
  • 628
  • 8
  • 24
  • First option is what I was looking for. I'm still learning the basics so this really just a test so I can further understand classes. Thank you very much. – Dylan Grove Mar 15 '17 at 22:47