简体   繁体   中英

Asses object created from static method

currently I am trying to use an object created from static method but it does not happen. I believe I am doing something wrong, but I don't know what. Here is my code that I use to create the object, this code is in separate class let's say it's name is CrusadersFactory.cs:

//Method for creating crusader Nate
public static void CreateNate(string name, int level, int health, int damage)
{
    Nate NateDragon = new Nate(name, level, health, damage);
    //Print the info for the new object NateDragon
    Console.WriteLine(NateDragon.name + " " + NateDragon.level + " "  + NateDragon.health + " " + NateDragon.damage);
}

And here is how I am trying to use the object, this code is in Program.cs:

CrusadersFactory.CreateNate("Nate", 100, 200, 300); //This works in order to create Nate, but It gives error if i try Console.WriteLin(CrusadersFactory.NateDragon.name)
Console.WriteLine("Nate's health is {0}", CrusadersFactory.NateDragon.health);

Thank you in advance for your answers!

Since you are able to call this without compiler error:

CrusadersFactory.NateDragon.health

That means you did actually create a static NateDragon property in your CrusadersFactory . However, you never assigned anything to this property .

public static void CreateNate(string name, int level, int health, int damage)
{
    Nate NateDragon = new Nate(name, level, health, damage);
}

What you've done here is declared and assigned a local variable . This variable only exists in scope of the CreateNate method, it does not exist outside of it.

What you should have done instead is to assign your created object to the static property you want to store it in:

public static void CreateNate(string name, int level, int health, int damage)
{
    CrusadersFactory.NateDragon = new Nate(name, level, health, damage);
}

Since the CreateNate method is also part of the CrusadersFactory , you can shorten this to:

public static void CreateNate(string name, int level, int health, int damage)
{
    NateDragon = new Nate(name, level, health, damage);
}

Very simply put, when you used Nate NateDragon = ... , you declared a new variable, rather than accessing the static class property NateDragon that you had already declared in your CrusadersFactory class.

Your method has the void return type (returns no value). You should change this to return the type Nate .

public static Nate CreateNate(string name, int level, int health, int damage)
{
    var nate = new Nate(name, level, health, damage);
    //Print the info for the new object NateDragon
    Console.WriteLine(nate.name + " " + nate.level + " "  +  nate.health + " " + nate.damage);
    return nate;
}

Your usage example can then become:

var nate = CrusadersFactory.CreateNate("Nate", 100, 200, 300);
Console.WriteLine("Nate's health is {0}", nate.health);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM