Classes & Objects, Forms and Controls

Status
Not open for further replies.

Siena1383

Baseband Member
Messages
51
I'm getting confused and just want to be sure I'm understanding this correctly.

[Forgot to say, this is Visual C# .NET.]

A form is an instance of a forms class, right? Or is it an object of the class, and you could have multiple instances of the same form?

Same questions for a control. Say you have a text box on a form. That's an instance of the TextBox object... or class?

Okay, next you have a method on a form that accepts a parameter from the text box control and returns a different value to the same text box. (Example: It changes the case of the text from lower to upper case.) Since the method is on the form rather than within the text box's class (object?) code, it needs to be a public method... right? And is some kind of public method needed within the text box's code to allow the form's method to place the returned value in it?

Now, I would think a better way to do this is just to put the case-changing method right inside the text box code and make it private, BUT that's not what the exercise wants. So real-world, wouldn't that be the more efficient way to do it?

Last question: Would it be better to just use a public static void method and not return a value, since you could have a statement change the value and use a parameter (variable?) to get the new value?

You can see I'm getting confused. Please help.
 
My advice to you is to learn the C# language and object oriented programming by creating console applications before jumping into graphical user interfaces. Not that my way is the only valid way, but it will reduce the number of concepts you are dealing with simultaneously.

By definition, an object is an instance of a class. When you are using controls like a text box, you are creating an instance of the TextBox class. So the text box on your form is an object.

Okay, next you have a method on a form that accepts a parameter from the text box control and returns a different value to the same text box. (Example: It changes the case of the text from lower to upper case.) Since the method is on the form rather than within the text box's class (object?) code, it needs to be a public method... right?
No. A method only needs to be public if it is going to be invoked by some entity outside the class. In your case, this is all happening inside the form. The method on the form would need to be public if you created another class that needed to call that method on the form directly.

And is some kind of public method needed within the text box's code to allow the form's method to place the returned value in it?
It already has one. Anything that you are able to access on a TextBox object is public. Otherwise, it would be unusable.

Now, I would think a better way to do this is just to put the case-changing method right inside the text box code and make it private, BUT that's not what the exercise wants. So real-world, wouldn't that be the more efficient way to do it?
No.

Last question: Would it be better to just use a public static void method and not return a value, since yuo could have a statement change the value and use a parameter (variable?) to get the new value?
I'm not trying to insult you, but do you know what the 'static' keyword means? You could do what you have suggested, but it's probably not the best way. It would depend on the context.
 
A form is an instance of a forms class, right? Or is it an object of the class, and you could have multiple instances of the same form?

An instance is an object. You can have more objects/instances of a class. Like you write a circle class, with that class you can make objects of the class circle.

Same questions for a control. Say you have a text box on a form. That's an instance of the TextBox object... or class?

A textBox is an object/instance of the textBox class. You cant say that a control is an instance of an object, because they are the same. Indeed you say that it's an instance/object of a class.

Okay, next you have a method on a form that accepts a parameter from the text box control and returns a different value to the same text box. (Example: It changes the case of the text from lower to upper case.) Since the method is on the form rather than within the text box's class (object?) code, it needs to be a public method... right? And is some kind of public method needed within the text box's code to allow the form's method to place the returned value in it?

Poster above said it. You use access modifiers (public, protected, private) only to use or not use the methods in another object of a class.

Example.

private void methodName() can't be used in another object of another class.
public void methodName() can be used.

You are only using one class, only the class with the form. The textBox is on that form class. It's not in a different class. You can then write methods for the form class, and use the textBox inside every method you want (doesn't matter if it's private, public or protected).
 
Thanks, jaeusm, I really do appreciate your taking the time to answer my many questions!

Re your advice on learning by doing console apps first, the book I'm using is really a light overview, not the greatest intro to C#. This doesn't come naturally to me, so I thought I'd start this way, then go back and redo it it using a different approach. Seems redundant, but I need that. Once I'm done with this book, I'll be using Microsoft's book on VC# . NET. That one does start with console apps.

jaeusm said:
In your case, this is all happening inside the form.

Okay, I know the text box on the form is an object, but isn't that a member of some kind of TextBox class?

Are you saying that because a control is on a form, it's part of the form's class?

jaeusm said:
Siena1383 said:
Now, I would think a better way to do this is just to put the case-changing method right inside the text box code and make it private, BUT that's not what the exercise wants. So real-world, wouldn't that be the more efficient way to do it?

No.

Okay, but why not?
 
Okay, I know the text box on the form is an object, but isn't that a member of some kind of TextBox class?
No, it's not a member. A member is a property, field, or method. The text box on the form is an object of type TextBox.

Are you saying that because a control is on a form, it's part of the form's class?
Yes.

Okay, but why not?
Because the case-changing logic is specific to your application. You wouldn't necessarily want every text box to have that capabiilty. One goal of object oriented programming is to make code that is generically reusable. Now, if you were creating a new text box class specifically for this application, then it would be ok.
 
The TextBox object you've created on your form is an object whose class(TextBox class) is already created. You can't write any code inside this TextBox class. You have your form class. Where this TextBox is on. This form class is where you will write your code for any control you put on it e.q. your TextBox object.

(It's even so that this class textBox has already a property which involves Character Casing. You can then just call your textBox and use the .NET notation to use characterCasing. But i'm not sure if u are this far already)
 
Thanks, both of you for the clarifications. It does help!

Ryaden, I wasn't ignoring you before. I didn't see your first post when I posted mine.

It's not that I have to change the case. It's that I have to accept user input into a text box and return some other text. Changing the case just seemed like an easy way to change it.

The problem I'm having is that I can pass in something else, but I want to use return so it returns a value based on what the user input. That may be more than what's intended for this exercise, I don't know. The book's info on return is not really adequate. Why don't parameters need to be specified, so you know what it'll be returning, for instance?

By the way, jaeusm, you'd asked if I knew what a static method is. I know the basic definition, but probably not the nuances.

I've read ahead to the end of the book, with ever-decreasing understanding. So I'm at the point where I'm going to start with the other book (Microsoft's VC# .NEt book). I'm sure that's written more in depth, so it should work better. I may try to find a book on OOP, too, though again, I know the definitions and need more than just that.
 
If you want to use a return value, you should write this in your method header.

example:
private TYPE methodName()
{
return variableName;
}

if you use a method like this, it's required a return value. Inside the body of your method you put your code ending with: return variableName (of course this has to be of the type used in the method header).

You use parameters in C# for variable input. Not for variable output. The return type is the output. If a method needs some variables to for example make some calculations you can put the needed variables inside the parameters.

Example:
private TYPE methodName(parameters)
{
return variableName;
}

To get it in the textBox or in any other control, you should write another method, where you can call the return method. I think you can't let it return the variable straight into any of your controls.
 
Status
Not open for further replies.
Back
Top Bottom