I have a class in a separate file. Then I have a form with some controls.
How can I get access to the controlls in my class? Tried Form1.richTextBox1 but that doesn´t work.
Second question:
I am creating object dynamicly.
In a form I use textBoxes to create values for the constructor.
Then in a button event I create an object and add it to a List, something like this.
car jaguar = new car(textBox1.Text, textBox2.Text);
carList.Add(car);
I know this works but how?
Everytime a new object is created it gets the same name. Does it work because you put it in the list and is it then impossible to find it in the list using the name of the object?
Lars PerssonPosted Apr 1, 2012, 2:16 PM
Again thanks!
Sam HobbsPosted Apr 1, 2012, 2:07 PM
In C# a new reference type is created by each use of the new keyword. A reference type can be copied to someplace such as into a collection. If no more uses of a reference type exist then the garbage collector assumes it is not needed and then the object is destroyed. In C++ there is a one-for-one correspondence of items in the heap and execution of the new keyword and the equivalent applies to C# reference types. In C++ there must be one delete keyword used for every new keyword but in C# objects are automatically destroyed when there are no more references to an object. Putting an object in a collection creates (copies) a reference thereby preserving the object from garbage collection even if the original variable for the object goes out of scope.
Lars PerssonPosted Apr 1, 2012, 1:09 PM
In the future I will only post one question in a thread.
As for the lines:
car jaguar = new car(textBox1.Text, textBox2.Text);
carList.Add(car);
it was only an example and you are right. I wouldn't work. Of course it should be "jaguar" that I add and not car.
I have experience with a lot of languages like Java, C/C++/C#/Objective-C but not a full understanding of any of the languages I would say.
I understand the concept of reference-type and pointers altough it can be confusing sometimes.
So if I understand it correctly it is like this:
If you create three objects in say a console-project the have to have different names, otherwise the interpreter would be confused. If you put the code I wrote in an event for a button, everytime a new object is created with the same name. That doesn´t matter thought because the interpreter can still keep track of every object. However, it is not possible to access an object by name?
Sam HobbsPosted Apr 1, 2012, 12:46 PM
As Vulpes indicates, there are many ways to access controls in a form. Vulpes's suggestion is easy to explain. If this thread was about just that question and if you had provided more information then we could suggest something more relevant. The easy soluton that Vulpes shows is not object-oriented.
I do not understand how the source code works that you say works. You have "car jaguar =" and then car is added. However car is the type; the class or struct. Are you sure that works?
It would help to know what experience you have. If you have experience with another language then it will help to compare C# to the other language. In most other languages, a variable is equivalent to the data it contains. C# is different in that, for objects of a class, the variable is a reference. This concept is confusing for us when we are familiar with other languages. In C# when a reference type is asssigned, it is usuallly only the reference (32-bits or 64-bits) that is copied around. The variable only represents the reference. So when you add the object to a list or array, just the reference is used. There is just one instance of the data and the variables only contain references to the data.
Lars PerssonPosted Apr 1, 2012, 8:42 AM
VulpesPosted Apr 1, 2012, 8:36 AM