1

I have an object which I have defined , the class which I define my object from that has a variable. The type of this variable is the same as this class, see below:

public class _car
{
 public _car()
 {
 }
 _car BMW = null;
}
.
.
.

Pay attention the last line is global definition of an object machine. My question is if in a method which is not located in _car class does something like this:

public another_Class
{
 public another_class()
 {
 }
 public _car machine = new _car();
 public int this_Methode()
 {
 if (Machine.BMW == null){
 Machine.BMW = new _car();
 return 1;
 }
 return 0;
 }

public void main_Methode() {

 int i=this_Methode();
 i+=this_Methode();
 //We run main_method in somewhere in our program now you say i is 0 or 1 or2 ?
 }
}

think in this way //We run main_method now you tell me i's value? is 0 or 1 or 2?

asked Aug 17, 2013 at 7:22
7
  • As long as an instance of _car is in existence, the field BMW will exist. Whether or not it's null will depend on whether or not that instance has had a value assigned to it. By the way, I'd recommend using properties rather than fields if the value is being accessed outside the class. Commented Aug 17, 2013 at 7:40
  • What do you mean by "is alive" and "use the code"? ("is non-null", "is called" respectively?) Commented Aug 17, 2013 at 11:50
  • What do you mean by "global"? There is no such thing as global variables in C#. Your variable is not even static. That means you need an object reference to access it. And as long as there is an object reference, there will also be a reference to BMW and it will not be disposed of. Commented Aug 17, 2013 at 12:13
  • This code does not compile. Machine is not defined anywhere. Commented Aug 17, 2013 at 13:21
  • Dear all I changed the question using your tips now I'm waiting for your suggestion Commented Aug 17, 2013 at 20:32

2 Answers 2

1

To respond after your edits:

It's not clear where Machine.BMW is coming from. But if it is available at runtime, then it will be populated by the following method. So the first time it runs, it will return 1 to I.

public int this_Methode()
{
 if (Machine.BMW == null){
 Machine.BMW = new _car();
 return 1;
 }
 return 0;
}
 int i=this_Methode(); //i = 1 as new car was created.
 i+=this_Methode(); Unless there is some other code running, this_Methode() will return zero as the car was already created.

you tell me i's value? Is 0 or 1 or 2? It will be 1 based on what you have shown in the code. But if there was other cod that affected Machine.BMW and set it to null, then it would be 2.

I like to create a test project in Visual Studio to try these kinds of things out. There is a free version called Visual Studio Express that you can use. Just create a Console app and try it out. This will help answer these questions quickly as you can try it and see if it works as expected. I do this all the time when something isn't working the way I think it should.

Greg

answered Aug 18, 2013 at 17:15
1

It looks like you are trying to learn more about C# and classes. Let me give you a few things that may help you out. This is not a direct answer to your question, as more info is needed to properly answer it. But a few pointers in general may help you out and let you clarify the issue:

In your class, the property _car is not initialized with an instance of a BMW, so it will be null when new instances are created.

You then have the line public _car machine = new _car()

This line is most likely inside of a class, as you can't have it just in a C# file on it's own. If this came from a Console.App, it's probably inside the Main Program so it run when you start it, and then it would be available to the rest of the app at runtime.

In another_class, you have a method which check to see if if BMW is null, and if not, it creates a new car. BMW will always be null here, as it has not been created before.

So even though you have the "global" variable, the "another_class" has no direct reference to it, so it's not going to see it. So I think the answer to your question is that it is going to always be null, not "live."

Daniel Mann
59.3k13 gold badges106 silver badges128 bronze badges
answered Aug 17, 2013 at 16:32
1
  • I have changed the question based on your quotes now I waite for help Commented Aug 18, 2013 at 5:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.