Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

this is a conceptual question, but if i initialize a variable in c# as public will it be able to be used in the class as I want a variable "player1" and a variable "player2" where either could be one of the two (or more)

  1. Player
  2. Computer

Here is what i would like to know how I would do:

public class Game  
{   
    //equivalent of initializing it here
    public bool islogging;  
    public string[] log;  
    public int draws, round;  

    public Game(player p1, computer p2)  
    {  
        public player player1 = p1;  
        public computer player2 = p2;                                                           
    }
    //other overloads here...
    ...
}

trying this it doesn't work as the public keyword is rejected

public Game(player p1, player p2 )
{  
     public  player player1, player2;
     player player3 = p1;
     player player4 = p2;

 }      
share|improve this question

put on hold as unclear what you're asking by Jesse C. Slicer, jk., MichaelT, Snowman, gnat yesterday

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
This isn't quite clear what you're trying to do. –  Telastyn 2 days ago
1  
Why don't you try it instead of asking us. –  Cameron McKay 2 days ago
    
I need to have a variable player 1 & 2 but depending on the way it's initialized it may be one of two types resulting in 4 potential outcomes pvp, pvpc, pcvpc and pcvp but if i were to use the var keyword i have to give it a starting value but that is impossible –  user181782 2 days ago
    
So, your problem is that you don't know whether the argument being passed in is a Computer or Player? What does your object hierarchy look like? –  Cameron McKay 2 days ago
    
computer : player –  user181782 2 days ago

2 Answers 2

up vote 2 down vote accepted

It's a fundamental matter of scope.

Variables declared within a function are bound to the lifespan and accessibility of that function.*

C# doesn't allow you to declare variables that will persist as part of the class from within a function.

*Lambda expression can sometimes invalidate that statement, but that's well outside the scope of what you're asking.


To accomplish what you're asking, use this syntax.

public class Game  
{   
    //equivalent of initializing it here
    public bool islogging;  
    public string[] log;  
    public int draws, round;  
    public player player1;
    public computer player2

    public Game(player p1, computer p2)  
    {  
        player1 = p1;  
        player2 = p2;                                                           
    }
    //other overloads here...
    ...
}

or you could also try the following. But keep in mind that player1 and player2's scope will end with the constructor function.

public class Game  
{   
    //equivalent of initializing it here
    public bool islogging;  
    public string[] log;  
    public int draws, round;  
    public player player1;
    public computer player2

    public Game(player p1, computer p2)  
    {  
        player player1 = p1;  
        computer player2 = p2;                                                           
    }
    //other overloads here...
    ...
}
share|improve this answer
    
Thank you. I will have to do it in separate classes but thank you for letting me know –  user181782 2 days ago
    
+1 To clarify the difference between the two it is often useful to put this.player1 = p1; when you are using instance fields, properties and methods of the class to show you're not using local variables. –  Nathan Cooper 2 days ago
    
user181782, your comment makes it sound like you don't understand. –  Cameron McKay 2 days ago

To answer your question about declaring their type; the variables' types have to be known at compile-time, even before it knows which constructor it will use.

When declaring those variables, to be able to call the methods you'd like (Jump(), Move(), Die()) on both of them, you need to declare them as being the most-specific object type you know of them; but not SO specific that it will restrict you from declaring them as the opposite type. Here's an example:

Shape shape1;
Shape shape2;

shape1 = new Rectangle(); // This works, because a Rectangle extends off of Shape.
shape2 = new Circle(); // This also works, because a Circle extends off of Shape.
shape1 = new Circle(); // Now shape1 has been changed to be a circle, too. The original
// Rectangle just basically disappeared.

As for defining the classes "Shape", "Rectangle", and "Circle", like your "Player" class? Well, if all of this sounds new to you, you definitely need to read up on the concept of Polymorphism. I've given you kind of an abstract overview just to give you an idea of what it allows, but mainly it's meant to keep you from needing to copy-paste lots of code, or have large "if-is-type" or "if-is-state" checks of your objects.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.