Hi . I'm relatively new to programming I just finished a class in VB6 and am now taking vb.net to stay as current as possible.
I'm trying to create program that accepts a number (one at a time) and updates 4 fields with each new number. The fields are Maximum, minimum, average and sum. With each number an array is to be reindexed and the list of inputted numbers is to be displayed in a textbox. I'm supposed to use an array and classes to do this. I'm so lost.
Would someone be able to give me a good place to start? I have a bunch of examples but I can't seem to make the connection between the class and the array.
Thanks to anyone who can help me.
Mike GoldPosted Jan 13, 2008, 1:29 AM
You are correct in saying everything can be done in the form's event handler, but you shouldn't do it this way, because this is bad design.
A Form, by the way is also a class, in fact everything in VB.NET is a class. Even VB6 had a primitive of class structure. Forms for example had functions and subroutines, similar to methods in classes. They could also have properties and fields, just like a class in VB.NET. VB6 also had the concept of encapsulation with private, protected, and public members.
Where VB6 and VB.NET really differ is that everything in VB.NET is a class. Arrays and other .NET library elements are classes, Forms are classes. Commonly used types are classes such as int, float, string, double, byte, bool.
In VB6 an Integer and a String are not classes.
int and string are classes in VB.NET because they have methods, properties and fields.
For example you could convert a string to an int by calling the Parse method on the string
int val = int.Parse("1");
In VB6 you would have to use a function to do the same thing. This is what distinguishes VB.NET from VB6 in that VB6 is mostly a structured language and not an object oriented language.
This is a big hurdle for former VB programmers to overcome in moving to .NET. Just as it would be for a C programmer going to C++. (Although VB6 has Object-Oriented features built into it, I rarely in my career ever saw any VB program take advantage of the Object-Oriented features)
You need to start thinking in terms of classes, objects, and methods, and properties belonging to classes. You'll also need to think in terms of class reuse (inheritance) as opposed to just function reuse.
So to summarize, the major difference between programming in VB.NET as opposed to VB6 is to start thinking in terms of black boxes called classes rather than think in terms of black boxes called functions.
VB.NET programmers ask questions like,
1) "How can I create a class or group of classes to implement this functionality?".
2) "How can I reuse an existing class and create new functionality?"
3) "How can I utilize the .NET library, and 3rd party class libraries inside my classes to simplify my tasks?"
4) And always ask, "How can I refactor my existing code to streamline it into better designed class structures, methods, and properties?".
As far as Arrays are concerned, an array is just a collection of objects that you can access through an integer index (note: an object is an instance of a class)
You'll need to keep a list of numbers entered in memory in order to perform statistics on them. An Array or ArrayList is a perfect class for doing this because you can add numbers entered into the array. You can also loop through the array to calculate your statistic.
Hope this provides further help, Naomi. I would also read up on the Array class in MSDN. I would also look at Mahesh's article on Arrays here on the site. It will give you a good understanding on how to use arrays in your problem:
http://www.c-sharpcorner.com/UploadFile/mahesh/WorkingWithArrays11232005064036AM/WorkingWithArrays.aspx
NaomiPosted Jan 11, 2008, 12:25 AM
Mike GoldPosted Jan 10, 2008, 11:16 PM
Normally, I wouldn't answer a homework question, but I feel its okay to give a "jumpstart" to get you going.
1) I would start by drawing the textboxes on the form in the designer. A textbox for each statistic (set the property in the textbox to ReadOnly since it's only for display)
2) Draw another TextBox for inputing the number and add a button next to it.
3) double click on the button to get the event handler
4) do your coding initially in the event handler.
If you want to get fancy, you could take advantage of object-oriented programming to use a strategy pattern with a calculate method. So create a class for each of your statistic with a method calculate and override for the particular statistic.
If you want to get even fancier, bind your statistic TextBoxes to a property in the form. Manipulate the property so the TextBoxes automatically change
Following steps 1-4 should get you started though. Not sure what the array is for if you are adding a number at a time, because you can just keep a running statistic. If the teacher wants you keep each statistic in a different element of the array, then you could put each object that calculates a statistic into an array and call calculate. If you have a Max, Min, Avg, etc. class all inheriting from a base class called statistic and all of them override the calculate function, then you could create an instance for each of these classes and place them all in an array. Then you can use polymorphism to loop through each of the elements of the array and call calculate. I suspect this is what the teacher is driving at.
If a lot of these terms seem confusing, I suggest you do some reading up on OO to learn some of the basic concepts. The four main concepts are:
Abstraction, Encapsulation, Polymorphism, Inheritance (Generalization, Specialization),
Here is on article I found that relates some of these concepts to VB.NET and VB which may help:
http://www.informit.com/articles/article.aspx?p=25857&seqNum=5
Best,