PDA

View Full Version : آموزش: خصوصیات کلاس ها



ROSTAM2
چهارشنبه 13 مهر 1401, 19:18 عصر
با این کد نام تمامی خصوصیات یک کلاس طبق تعیین نوع آن فقط خواندنی(ReadOnly) - فقط نوشتنی(WriteOnly) و همه خصوصیات نماش داده می شه:

154047

بعد از دستور Properties برای نمایش همه all برای نمایش خصوصیات فقط خواندنی ReadOnly و برای خصوصیات فقط نوشتنی WriteOnly تایپ کنید و Enter.


Imports System.Reflection
Module KeyStoreModule
Sub Main()
Dim Words() As String = {""}, Params() As String = {""}
Do Until Words(0).ToLower = "quit"
Console.Write("System.Reflection>")
Words = Console.ReadLine.Split(Space(1))
Select Case Words(0).ToLower
Case "properties"


Console.WriteLine()
If Words.Length = 1 Then
Properties: For Each P As String In PropertyNames()
Console.WriteLine("{0}{1}", vbTab, P)
Next
ElseIf Words.Length = 2 Then
Select Case Words(1).ToLower
Case "all" : GoTo properties
Case "readonly"
For Each P As String In PropertyNames(XMLManager(Of Global.LINQ_To_XML.UserProfileItem).IncludePropert iesOptions.ReadOnly)
Console.WriteLine("{0}{1}", vbTab, P)
Next
Case "writeonly"
For Each P As String In PropertyNames(XMLManager(Of Global.LINQ_To_XML.UserProfileItem).IncludePropert iesOptions.WriteOnly)
Console.WriteLine("{0}{1}", vbTab, P)
Next
End Select
End If
Console.WriteLine()
End Select
Loop


End Sub
Dim PInfo() As PropertyInfo
Public Enum IncludePropertiesOptions
All = 0
ReadAndWrite = 1
[ReadOnly] = 2
[WriteOnly] = 3
End Enum
Function PropertyNames(Optional IncludeOption As IncludePropertiesOptions = IncludePropertiesOptions.All) As IEnumerable(Of String)
Dim Expr As New List(Of String)
Dim Index() As Object = {}
PInfo = GetType(Console).GetProperties()
For Each P As PropertyInfo In PInfo
Select Case IncludeOption
Case IncludePropertiesOptions.All
Expr.Add(P.Name)
Case IncludePropertiesOptions.ReadAndWrite
If P.CanRead = True And P.CanWrite = True Then
Expr.Add(P.Name)
End If
Case IncludePropertiesOptions.ReadOnly
If P.CanRead = True AndAlso P.CanWrite = False Then
Expr.Add(P.Name)
End If
Case IncludePropertiesOptions.WriteOnly
If P.CanRead = False AndAlso P.CanWrite = True Then
Expr.Add(P.Name)
End If
End Select
ContinueFor:
Next
Return Expr.ToArray
End Function
End Module

ROSTAM2
جمعه 15 مهر 1401, 15:44 عصر
مرتب سازی آیتم های یک لیست با نوع دیتای یک کلاس (که خصوصیتهای متعددی دارد) بر اساس مقدار خصوصیت انتخابی توسط نام خصوصیت

تذکر: این کد توی کلاسی قرار می گیرد که از یک لیست با نوع دیتای خاص وراثت گرفته است
مثال:

Public Class ItemCollection
Inherites List (Of <MyClasss>)
<Here>
End Class





Private PropertyName As String = ""


Public Shadows Sub Sort(PropertyName As String)
Me.PropertyName = PropertyName
Dim Comparer As New Comparison(Of T)(AddressOf CompareItems)
MyBase.Sort(Comparer)
End Sub


Private Function CompareItems(x As T, y As T) As Integer
Dim P As PropertyInfo = Nothing
Dim index() As Object = {}
Dim Expersions(1) As Object
If Me.PropertyName.Length > 0 Then
P = x.GetType.GetProperty(Me.PropertyName)
Expersions(0) = P.GetValue(x, index)
P = y.GetType.GetProperty(Me.PropertyName)
Expersions(1) = P.GetValue(y, index)
If IsNumeric(Expersions(0)) = True Then
Return Val(Expersions(0)).CompareTo(Expersions(1))
End If
Return Expersions(0).ToString.CompareTo(Expersions(1))
Else
Return x.ToString.CompareTo(y)
End If
End Function

ROSTAM2
یک شنبه 17 مهر 1401, 08:52 صبح
سلام به همه.

هر وقت هروقت بخوایم یک کلاس یا متود رو طوری تعریف کنیم که بشه به متغیرها و خصوصیات و آیتم های یک کالکشن از کلاس و ورودی های توابع هر نوع دیتا یا کلاسی رو بدیم از کلمه کلیدی Of استفاده می کنیم:

مثال:



Public Class MyClass(Of T [As {New,...}])
Private MyList As New List(Of T)
End Class

Public Function MyFunction(Of T [As {New,...}])()
Dim Obj As New T
End Function



این مقاله رو هم مطالعه کنید: جالب انگیزه
https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/generic-types