Where in the source code should I create an object? - objective-c

I want to create an object from a class that I have created. What I have been trying is to create the object (which can be testingclass) from a method of another class, which I will call classexample, but it says that testingclass is undeclared, which I assumed meant that it was out of the scope of the method, and the method couldn't access it. My program doesn't have a main function like in c or anything so I don't really understand where to put code that involves different objects if it can't go in methods.

Do you have the header file of the testingclass and did you import it to the file that create that object? That's all I can guess for your problems with very little information for very strange problem.

Related

Making Global Variables Locally

In VB.Net can you declare variables locally (in a method) and have them have a global scope?
I'm new-ish to VB.Net and am trying to figure out some of the ways the language works. In a previous project I did with C++ I was able to inside of a method declare a variable as global, saving memory space until the first time that method was called and the variable was instantiated.
Just curious if this is something that is possible with VB.Net.
Encapsulation in .NET can make it difficult to implement a global variable in the way you are thinking of one. The closest solution may be to declare a public variable in a module, but it isn't immediately available inside another module.
The way I usually do what you're thinking is to create a singleton class "globals" that contains member fields representing the global variables I want to move around, then I just pass my "globals" instance as an argument.
Public Dim myGlobals as GlobalClass
myGlobals.someVariable = "preserve me"
Then making them available to be accessed by the method: someMethod(myGlobals) will pass them by reference by default.
So Jacob posted an pretty good answer to my question. After a bit more researching, I think my best bet is to do something similar to the following:
Class ScopeTest
Private randVar As Object = Nothing
Sub Initialize()
randVar = New Label()
End Sub
End Class
Essentially, create the variable at the highest level scope I need it, and set it to Nothing so that no data (should be) allocated to it, but the variable name has the appropriate scope. Then I just instantiate the variable whenever I call it the first time, and then it will be implemented throughout the rest of the code.
Obviously the largest pitfall with this setup is if I go to call the object while it is equal to Nothing. This will require me to add in some If Not IsNothing statements to the code, but since I can't seem to find a better way to go about this, it's what I will be doing at the moment.

Can't reference sub-routines in other modules of VB.net program

I have a VB.net program that I received from someone else. I am trying to make modifications to it. The program consists of one main form and 6 classes (all .vb files).
In the main form, I want to call a sub-routine in one of the other modules. What is strange is that if I enter the name of the module followed by a ".", i.e.
QuoteMgr.
I don't see the names of the sub-routines in the module. I only see the Public Const's that are defined.
The sub-routine that I want to call is in a section labelled:
#Region "Methods"
What do I need to do to be able to call one of these methods?
The confusion was because of the wording you used in your original question before you edited it to say "class" instead of "module".
The two terms in VB.net mean entirely different things. A class typically must be insantiated as an object to invoke its methods.
So what you need to do is:
dim qt as new QuoteMgr
qt.Method("foo");
In this case you're creating an instance of QuoteMgr called qt and then invoking its methods. Alternatively you could modify the QuoteMgr class and set the method you're trying to call to "Shared" and then call it by simply going "QuoteMgr.Method" as you were trying before.
A module is more like a free-standing library of methods that can be called by anything in the same project (by default).

want to use block of code in all method of particular class

We are using Spring MVC, in that for one particular module one controller class is their and for different pages of that module we have different method in the same class.
Every thing works fine but we want some code (i.e. for page history and other logs) to use in all methods. Currently we are copy pasting that code in all methods but want some good solution that we reuse the code block.
Write a static method in under a class name Utils and use it everywhere
Utils.method();

Information on OOP, creating Objects

I have a problem in understanding OOP...
This is it :
Sometime's you create an object with this syntax:
Object ObjectName = new Object();
But sometimes, we don't need to do that like in Android:
Textview TextviewName;
Or in J2ME:
form formName;
I already searched it and I got some information (but not sure) that this is because of static method... is it true? I think it has a relation with Polymorphism.. is it true?
Thanks all.
PS : Sory if I made some mistakes, English is not my native languange :D
Forget static methods - they're not relevant here. I'd advocate only looking at static methods / elements when you've truly grasped what objects are.
In Java, you can do this:
Object object;
Just as well as you can do this:
Object object = new Object();
In the first example, you're creating a reference but you're not populating that reference with anything, in the second example you're creating a reference and populating it with a new object, on which you can call methods, modify values and so on.
If you try and call methods on the first declaration you won't be able to - there's nothing there. Depending on the language and how you've declared it this might produce an error at runtime or a compile time error (Java does both depending on whether it's a field or a local variable.) The principle though is the same for all OO languages, you can't dereference (call methods, fields, etc.) on a reference that hasn't been populated, because in effect you're trying to call a method on something that isn't there.
you are mixing different languages and it's not the case of static methods nor polymorphism..
i suggest to read a good book of OOP beginning with the basis.. you can find "Thinking in c++" for free on the net..
Your Textview would not be initialized. Any try at using it would result in a NullReference error. In order for an object to actually be created, you have to use the new syntax or a function that returns a valid object.
However, this is a syntax-dependent issue, so first decide what language you want to study. If your Textview had been declared in C++, it would actually create an object, on the stack.

How are classes instances handled through views?

Im really new to iOS Programming. Although I read lots of books and material I cant really find out an answer to a simple question.
I know C++ language, and I understand how variables are handled through the functions. What I really dont understand in iOS programming is how this thing is done.
For example, in C++ I create a class instance in the main function. When I call another function and I want to share this class instance all you need to do is:
myFunction(&myClassInstance);
When I switch views in iOS programming (and therefore classes) I dont know how to send a class instance that was created before. For example, if I am on the firstView of my program, and I switch to the secondView, how can I send the variables that I was currently using?
If I need to create an instance of a class that is going to be used though ALL the program, where I should create it? In C++ I would create it in the main function, in iOS Programming, where should I create it?
Thanks!
First of all what I understand from your question is
"In C++ when we have to pass instance of some other class we use reference argument. What is option in Objective C?"
So, Objective C is build up on C. So you can pass instance using pointer. like...
-(void)myFunction:(MyClass*)myClassInstance;
Other question is
"If I need some instance in all the program then where should I create?"
You can use AppDelegate class to hold that instance. Because instance of AppDelegate it accessible from all your program.
And you can create instance in "didFinishLaunchingWithOption"