Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Not used to some DOT NET ways
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Database:
Visual FoxPro
Divers
Thread ID:
01442515
Message ID:
01442583
Vues:
93
You have been given some good answers, just thought I'd add a little history which might help further explain...

The big picture here is strong vs. weak typing. Both C# and VB .NET languages are strongly typed. You declare the types of variables in your code and these are checked at compile time. For example:
bool isChanged = false;
The isChanged variable is declared as type boolean and you can only store a true or false into the variable. If you try to store any other type into the variable, the compiler flags it as an error.

Visual FoxPro has weak typing, meaning that variables are not bound to a specific type. You can store a value of any type into a variable:
x = 1
x = "Hello"
x = .t.
In C#, when you declare a variable to be of a particular type, you can store a value of that type, OR any of its subclasses. For example, if I have a class named BusinessObject and a subclass of BusinessObject called Inventory, I can declare a variable of type BusinessObject and store a reference to a BusinessObject instance or an Inventory instance:
BusinessObject b;
b = new BusinessObject(); // Store a reference to a BusinessObject
b = new Inventory();           // Store a reference to an Inventory object which is a subclass of BusinessObject
In .NET, everything is an object--even basic data types like boolean, and integer--and all classes can trace their heritage back to the Object class. This means you can declare a variable to be of type Object and you can store any value into that variable. For example:
object o;
o = 1;
o = "Hello";
o = true;
o = new BusinessObject()
Although it might seem convenient to declare ALL of your variables as type Object so you can have the freedom to store a value of any type into them, it's not a best practice, because you are throwing strong typing out the window. Strong typing is good, because the simple act of declaring the type of a variable allows the compiler to find many of your bugs--invalid values passed as parameters, invalid values returned from methods, inconsistent values returned from methods (did you ever create a method where one code path--such as an if statement--returned a value, but the other didn't???), and so on.

In C# 1.0, there were two types of collections--arrays which are strongly typed, and collections that are not strongly typed.

For example, here is the declaration and initialization of an array of type String. It has three elements (all of type String) and as you can see, in .NET collections are zero-based (zero is the number of the first element, unlike FoxPro where it's one). Also, in C#, square brackets are used to reference members of a collection:
string[] customerIDs = new string[3];
customerIDs[0] = "ACME";
customerIDs[1] = "PICCO";
customerIDs[2] = "QUICK";
The good thing about arrays in C# is they are strongly typed. The bad thing about arrays is they are not "growable". You can't redimension an array in C#.

In C# 1.0, if you wanted a collection that is growable, you could use an ArrayList instead:
ArrayList kidsAges = new ArrayList();
customerIDs.Add("ACME");
customerIDs.Add("PICCO");
customerIDs.Add("QUICK");
You can use the Add() or Remove() method of an ArrayList at run time to add or remove items, which makes them "growable" (or "shrinkable").

The problem with ArrayList, is that it is not strongly typed. All members are of type Object. If you want to get a value out of an array list you need to perform a type conversion. For example, the following code references the first element in the ArrayList and successfully converts it from type Object to type String :
string customerID = (string)customerIDs[0];
A problem arises when you cast an ArrayList value to the wrong type. The following code tries to convert the customer ID to an integer.
int customerID = (int)customerIDs[0];
The compiler won't catch this error because the ArrayList members are of type Object and it's possible for an ArrayList member to be of any type. However, at runtime, you will get a "Type mismatch" exception because the ArrayList element contains a string and cannot be converted to an integer.

C# 2.0 introduced Generics as a way to solve this problem. One aspect of Generics (it's a broad topic) are Generic collections, which are both strongly typed and growable--taking the best features from both arrays and C# 1.0 collections.

Here is an example of a Generic collection that is strongly typed and growable:
List<string> customerIDs = new List<string>();
customerIDs.Add("ACME");
customerIDs.Add("PICCO");
customerIDs.Add("QUICK");
The angle brackets are used in this declaration to specify the Type of members in the List--in this case, String. When you retrieve a value from the collection, there is no need to perform a type conversion, because the type is specified when you declare the List:
string customerID = customerIDs[0];
And the size of the List can be changed at runtime by using the Add() and Remove() methods.

Hope this helps!

Best Regards,
Kevin McNeish
Eight-Time .NET MVP
VFP and iOS Author, Speaker & Trainer
Oak Leaf Enterprises, Inc.
Chief Architect, MM Framework
http://www.oakleafsd.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform