G+_James Coleman Posted March 14, 2014 Share Posted March 14, 2014 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 More sharing options...
G+_Volkan Paksoy Posted March 14, 2014 Share Posted March 14, 2014 It doesn't become a global variable. It just becomes a member. If you don't explicitly specify scope it is declared as "private" by default that means you can only access it from inside that class only. Link to comment Share on other sites More sharing options...
G+_James Coleman Posted March 14, 2014 Author Share Posted March 14, 2014 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 More sharing options...
G+_Nevrin O Posted March 14, 2014 Share Posted March 14, 2014 this might help http://msdn.microsoft.com/en-us/library/ms173121.aspx that link is for c# From what i remember with java is that its slightly different but similar. so probably best not to assume its exactly the same between different languages. Link to comment Share on other sites More sharing options...
G+_Volkan Paksoy Posted March 14, 2014 Share Posted March 14, 2014 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 More sharing options...
G+_James Coleman Posted March 14, 2014 Author Share Posted March 14, 2014 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 More sharing options...
G+_Volkan Paksoy Posted March 14, 2014 Share Posted March 14, 2014 To access a variable as class.variableName you have to define it as a "static" variable. http://msdn.microsoft.com/en-us/library/98f28cdx.aspx Link to comment Share on other sites More sharing options...
G+_James Coleman Posted March 14, 2014 Author Share Posted March 14, 2014 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 More sharing options...
G+_Volkan Paksoy Posted March 15, 2014 Share Posted March 15, 2014 James Coleman Static variables are shared among all instances of the class. They are not global. you were asking about accessing a variable as "class.name" that's why I mentioned it. Link to comment Share on other sites More sharing options...
Recommended Posts