Модераторы: Akina
  

Поиск:

Ответ в темуСоздание новой темы Создание опроса
> Создание своего типа переменной 
:(
    Опции темы
cardinal
Дата 21.12.2005, 16:48 (ссылка) | (нет голосов) Загрузка ... Загрузка ... Быстрая цитата Цитата


Инженер
****


Профиль
Группа: Экс. модератор
Сообщений: 6003
Регистрация: 26.3.2002
Где: Германия

Репутация: 19
Всего: 99



Вот что написано в MSDN:

Creating Your Own Data Types

You can combine variables of several different types to create user-defined types (known as structs in the C programming language). User-defined types are useful when you want to create a single variable that records several related pieces of information.

You create a user-defined type with the Type statement, which must be placed in the Declarations section of a module. User-defined types can be declared as Private or Public with the appropriate keyword. For example:
Код
Private Type MyDataType

-or-
Код
Public Type MyDataType

For example, you could create a user-defined type that records information about a computer system:
Код
' Declarations (of a standard module).
Private Type SystemInfo
   CPU As Variant
   Memory As Long
   VideoColors As Integer
   Cost As Currency
   PurchaseDate As Variant
End Type

Declaring Variables of a User-Defined Type
You can declare local, private module-level, or public module-level variables of the same user-defined type:
Код
Dim MySystem As SystemInfo, YourSystem As SystemInfo

The following table illustrates where, and with what scope, you can declare user-defined types and their variables.


Procedure/Module      You can create a user-defined type as...          Variables of a user-defined type can be declared... 
Procedures                         Not applicable                                                                    Local only                       
Standard modules              Private or public                                                                 Private or public 
Form modules                     Private only                                                                       Private only 
Class modules                    Private or public                                                                 Private or public 


Note   If declared using the Dim keyword, user-defined types in Standard or Class modules will default to Public. If you intend a user-defined type to be private, make sure you declare it using the Private keyword.

Assigning and Retrieving Values
Assigning and retrieving values from the elements of this variable is similar to setting and getting properties:
Код
MySystem.CPU = "486"
If MySystem.PurchaseDate > #1/1/92# Then

You can also assign one variable to another if they are both of the same user-defined type. This assigns all the elements of one variable to the same elements in the other variable.
Код
YourSystem = MySystem

User-Defined Types that Contain Arrays
A user-defined type can contain an ordinary (fixed-size) array. For example:
Код
Type SystemInfo
   CPU As Variant
   Memory As Long
   DiskDrives(25) As String   ' Fixed-size array.
   VideoColors As Integer
   Cost As Currency
   PurchaseDate As Variant
End Type

It can also contain a dynamic array.
Код
Type SystemInfo
   CPU As Variant
   Memory As Long
   DiskDrives() As String      ' Dynamic array.
   VideoColors As Integer
   Cost As Currency
   PurchaseDate As Variant
End Type

You can access the values in an array within a user-defined type in the same way that you access the property of an object.
Код
Dim MySystem As SystemInfo
ReDim MySystem.DiskDrives(3)
MySystem.DiskDrives(0) = "1.44 MB"

You can also declare an array of user-defined types:
Код
Dim AllSystems(100) As SystemInfo

Follow the same rules to access the components of this data structure.
Код
AllSystems(5).CPU = "386SX"
AllSystems(5).DiskDrives(2) = "100M SCSI"

Passing User-Defined Types to Procedures
You can pass procedure arguments using a user-defined type.
Код
Sub FillSystem (SomeSystem As SystemInfo)
   SomeSystem.CPU = lstCPU.Text
   SomeSystem.Memory = txtMemory.Text
   SomeSystem.Cost = txtCost.Text
   SomeSystem.PurchaseDate = Now
End Sub

Note   If you want to pass a user-defined type in a form module, the procedure must be private.

You can return user-defined types from functions, and you can pass a user-defined type variable to a procedure as one of the arguments. User-defined types are always passed by reference, so the procedure can modify the argument and return it to the calling procedure, as illustrated in the previous example.

Note   Because user-defined types are always passed by reference, all of the data contained in the user-defined type will be passed to and returned from the procedure. For user-defined types that contain large arrays, this could result in poor performance, especially in client/server applications where a procedure may be running on a remote machine. In such a situation, it is better to extract and pass only the necessary data from the user-defined type.

For More Information   To read more about passing by reference, see "Passing Arguments to Procedures" in "Programming Fundamentals."

User-Defined Types that Contain Objects
User-defined types can also contain objects.
Код
Private Type AccountPack
   frmInput as Form
   dbPayRollAccount as Database
End Type

Tip   Because the Variant data type can store many different types of data, a Variant array can be used in many situations where you might expect to use a user-defined type. A Variant array is actually more flexible than a user-defined type, because you can change the type of data you store in each element at any time, and you can make the array dynamic so that you can change its size as necessary. However, a Variant array always uses more memory than an equivalent user-defined type.

Nesting Data Structures
Nesting data structures can get as complex as you like. In fact, user-defined types can contain other user-defined types, as shown in the following example. To make your code more readable and easier to debug, try to keep all the code that defines user-defined data types in one module.
Код
Type DriveInfo
   Type As String
   Size As Long
End Type

Type SystemInfo
   CPU As Variant
   Memory As Long
   DiskDrives(26) As DriveInfo
   Cost As Currency
   PurchaseDate As Variant
End Type

Dim AllSystems(100) As SystemInfo
AllSystems(1).DiskDrives(0).Type = "Floppy"


Если проблемы с англ., то надо чтобы кто-то это немного перевел. smile


--------------------
Немецкая оппозиция потребовала упростить натурализацию иммигрантов
В моем блоге: Разные истории из жизни в Германии

"Познание бесконечности требует бесконечного времени, а потому работай не работай - все едино".  А. и Б. Стругацкие
PM   Вверх
  
Ответ в темуСоздание новой темы Создание опроса
Правила форума "VB6"
Akina

Запрещается!

1. Публиковать ссылки на вскрытые компоненты

2. Обсуждать взлом компонентов и делиться вскрытыми компонентами

  • Литературу по VB обсуждаем здесь
  • Действия модераторов можно обсудить здесь
  • С просьбами о написании курсовой, реферата и т.п. обращаться сюда
  • Вопросы по реализации алгоритмов рассматриваются здесь
  • Используйте теги [code=vb][/code] для подсветки кода. Используйтe чекбокс "транслит" (возле кнопок кодов) если у Вас нет русских шрифтов.


Если Вам понравилась атмосфера форума, заходите к нам чаще! С уважением, Akina.

 
0 Пользователей читают эту тему (0 Гостей и 0 Скрытых Пользователей)
0 Пользователей:
« Предыдущая тема | VB6 | Следующая тема »


 




[ Время генерации скрипта: 0.0889 ]   [ Использовано запросов: 23 ]   [ GZIP включён ]


Реклама на сайте     Информационное спонсорство

 
По вопросам размещения рекламы пишите на vladimir(sobaka)vingrad.ru
Отказ от ответственности     Powered by Invision Power Board(R) 1.3 © 2003  IPS, Inc.