Jump to content

Wait? Is what I 'm hearing correct? In C# (C hash tag), when you make a variable public within ...


G+_James Coleman
 Share

Recommended Posts

Wait? Is what I'm hearing correct? In C# (C hash tag), when you make a variable public within a class, it becomes a global variable that every function in every class can call without mentioning that object???

 

From what I remember, those variables are only available in side of that object, but because they are public, you can access it from the object by calling object.variable.

 

A global variable is where when you put the variable out side of the class and it is available to every function in every class.

Link to comment
Share on other sites

Volkan Paksoy Let's take this as an example.

 

https://github.com/GRMrGecko/GeckoReporter/blob/master/Classes/Framework/MGMReporter.h#L22

 

This is some code I wrote. Line 22-27 are global variables, and line 34-36 are object variables. The difference between global and object variables is that global is accessible from anywhere and object variables are only accessible to that object. In Obj-C, you can make a object variable accessible to other objects by declaring them as properties, but they do not become global like how I thought I heard in the podcast.

Link to comment
Share on other sites

James Coleman I don't know Objective-C so I cannot comment on that. I think the easiest way would be to quickly create a test application with 2 classes in it. Create private and public variables inside one and try to access them via an instance of that class. You can play around with various modifiers by checking out the link Nevrin O  sent.

Link to comment
Share on other sites

Nevrin O how do you access the variable. Can you just access it as class.name or object.name or just name?

 

If you can access it by just name it's a global variable.

If you can access it by class.name, it's a object global variable (these don't exist in objective-c, except for methods with a + in front).

If you can access it by object.name, it's a object variable that is only accessible when you allocate the class as an object.

Link to comment
Share on other sites

Volkan Paksoy So in other words they are assigned to the class and are not actually global variables. I never tried making a object static in an Objective-C class, would be interesting to see how it reacts. I know when you make a variable static in a function (if I remember correctly), it's only global to that function.

Link to comment
Share on other sites

 Share

×
×
  • Create New...