Lets say I have this class called class1, class1 contains one default constructor and one that takes one parameter (say a string for example). Inside that constructor I set up a variable, lets call it “string var”. I want var to get the value from the string that I passed to the constructor while creating that object, but I want to be able to use var outside of the constructors scope, is that possible? since constructors doesen’t return values and whatnot.
To clarify here is a code example of what I want to do:
class class1
{
public class1(string songPath)
{
System.Media.SoundPlayer songPlayer = new System.Media.SoundPlayer(songPath);
}
//here I want to use my songPlayer I created with the passed string as songPath
}
You need to make
songPlayera field – it is a local variable only visible to the constructor it is declared in at the moment.