السلام عليكم ورحمه الله وبركاته ,,
عندي سؤآل عن دوآل الـ string ..
,,
وهي داله المقارنه ؟؟
كيف طريقه كتآبه الكود ,,
وهل المقارنه تتم بين مصفوفتين فقط بغض النظر عن عدد أبعآدهآ ,,
ولا المقـآرنه ما تتم الإ بمصفوفه وآحدهـ
أتمنــى أحد يشرح لــي ؟؟
,,,
السلام عليكم ورحمه الله وبركاته ,,
عندي سؤآل عن دوآل الـ string ..
,,
وهي داله المقارنه ؟؟
كيف طريقه كتآبه الكود ,,
وهل المقارنه تتم بين مصفوفتين فقط بغض النظر عن عدد أبعآدهآ ,,
ولا المقـآرنه ما تتم الإ بمصفوفه وآحدهـ
أتمنــى أحد يشرح لــي ؟؟
,,,
سبحآن الله وبحمده .. سبحآن الله العظيم .. ,
مالذي تريد معرفته بالضبط ؟
The important thing is not so much
that every child should be taught,
as that every child should be given the wish to learn.
الــ string لهآآ دوآآل
ومنهآ دالــه substring و indexof
واللي آريد معرفتهآ هي دآله المقارنه ..!!
أريد شرح مبسط لكيفيه كتآبه كود هذه الدآله
وكيف تتم المقآرنه بين مصفوفتين ؟؟
أتمنــى يكون اللي آريدهـ مفهــووم ..!!
,,
سبحآن الله وبحمده .. سبحآن الله العظيم .. ,
حسناً .
لنبدأ بـ Contains,
Contains : هي عملية ذات مرجع منطقي أي ( Boolean ) تستخدم لمعرفة إذا كان المتغير المستخدم يحوي ( كائن, نص, حرف ) أو لا .
Dim MyStringVariable As String = "Visual Basic" يجب أن تعتمد على هذا المتغير في جميع الأمثلة التالية .
مثال : If MyStringVariable.Contains("Visual") Then MsgBox("The current sentence contains Visual")
EndsWith : هي عملية ذات مرجع منطقي ( Boolean ) تستخدم لمعرفة إذا كان المتغير المستخدم ينتهي بـ ( نص, حرف ) أو لا .
مثال : If MyStringVariable.EndsWith("Basic") Then MsgBox("The current sentence ends with Basic")
IndexOf : هي عملية ذات مرجع عددي ( عدد صحيح Integer أو Int32 ) تستخدم لمعرفة مكان حرف ما من النص المستخدم .
مثال : MsgBox(String.Format("IndexOf B is {0}", MyStringVariable.IndexOf(Chr(65))))
Insert : هي عملية ذات مرجع نصي ( String ) تستخدم لإضافة مقطع نصي في مكان يتم تحديد بدايتة في الخانة الأولى ( StartIndex ) .
مثال : MsgBox(MyStringVariable.Insert(7, "Is "))
LastIndexOf : تعريفها شبيه بـ IndexOf لكنها تبدأ البحث عن الحرف المطلوب معرفة مكانه من النهاية إلى البداية اي يعكس العملية ( IndexOf ) .
مثال : MsgBox(String.Format("Last Index of i Is {0}", MyStringVariable.LastIndexOf("i")))
Length : خاصية ذات مرجع عددي ( عدد صحيح Integer أو Int32 ) تستخدم لمعرفة عدد الأحرف ( طول النص ) .
مثال : MsgBox( MyStringVariable.Length )
Remove : عملية ذات مرجع نصي ( String ) تستخدم لحذف مقطع نصي من النص, يمكنك تحديد البداية و عدد الأحرف المراد حذفها, أو يمكنك الاكتفاء بتحديد البداية ( علماً أنها ستحذف جميع الأحرف من نقطة البداية إلى النهاية ( نهاية النص ) ) .
مثال 1 : MsgBox(MyStringVariable.Remove(7, 2))
مثال 2 : MsgBox(MyStringVariable.Remove(3))
Replace : عملية ذات مرجع نصي ( String ) تستخدم لاستبدال حرف أو مقطع نصي, يتم استخدامها بعد تحديد ( الحرف, المقطع النصي ) القديم و ( الحرف, المقطع النصي ) الجديد .
مثال : MsgBox(MyStringVariable.Replace("Vis", "The Vis"))
Split : عملية ذات مرجع نصي أو مصفوفة نصية ( String Array ) تستخدم في حال كان لديك نص يحوي حرف أو اشارة محددة تفصل الكلمات عن بعضها, في اغلبية الأحيان تستخدم لقراءة ملفات نصية تسمى ( CSV ), العملية ( Split ) لها عمليات مختلفة ( سأقوم بشرحها لاحقاً او قريباً ) .
مثال :
For Each SString As String In MyStringVariable.Split(" ")
MsgBox(SString)
Next SString
StartsWith : هي عملية ذات مرجع منطقي ( Boolean ) شبيها بالعملية ( EndsWith ) لكنها تستخدم لمعرفة إذا كان النص يبدأ بـ ( حرف أو مقطع نصي ) محدد .
مثال : If MyStringVariable.StartsWith("Visual") Then MsgBox("The current sentence starts with Visual")
Substring : عملية ذات مرجع نصي ( String ) تستخدم لقراءة مقطع نصي يتم تحديد بدايته وعدد الاحرف المطلوب ( قراءتها ), أو يمكنك الاكتفاء بتحديد البداية, للتقريب ( عكس العملية ( Remove ) ) .
مثال 1 : MsgBox( MyStringVariable.Substring(0, 5))
مثال 2 : MsgBox( MyStringVariable.Substring(7))
ToCharArray : عملية ذات مرجع حرفي الذي هو عبارة عن مصفوفة ( Char Array ) تستخدم هذه العملية لتحويل النص إلى مصفوفة حرفية كما ذكرنا .
مثال :
For Each SC As Char In MyStringVariable.ToCharArray
MsgBox( SC.ToString() )
Next SC
باقي العمليات معروفة كـ ( ToString, ToUpper, ToLower, Trim ) .
إذا اردت معرفة احداهم, لاتتردد في السؤال .
تم تعديل هذه المشاركة بواسطة Bashar Rabat في 29 ديسمبر 2008 في 18:39
The important thing is not so much
that every child should be taught,
as that every child should be given the wish to learn.
بالنسبة للمصفوفات, كيف تريد تقييم التساوي ؟
المشكلة أن العملية Equals ذات المرجع المنطقي ( Boolean ) لاتساعدك في هذا لأنها تقارن جميع خصائص المصفوفة بمافيها الأسم ( اسم التصريح ) .
المثال التالي يوضح الفكرة :
Dim MyStringArray1() As String = {"Bashar", "BasharVS", "BVS"}
Dim MyStringArray2() As String = {"Bashar", "BasharVS", "BVS"}
If MyStringArray1.Equals(MyStringArray2) Then
MsgBox("Got that")
End If
إذا استخدمت التالي, ستنجح العملية وتظهر لك الرسالة ( Got that )
Dim MyStringArray1() As String = {"Bashar", "BasharVS", "BVS"}
If MyStringArray1.Equals(MyStringArray1) Then
MsgBox("Got that")
End If
يمكنك استخدام التالي :
Private Function MyEqualsFunction( Byval FirstArray As Object, Byval SecondArray As Object ) As Boolean
For Each CObject As Object In FirstArray
If Not SecondArray.Contains( CObject ) Then Return False
Next CObject
Return True
End Function
الاستخدام :
Dim MyStringArray1() As String = {"Bashar", "BasharVS", "BVS"}
Dim MyStringArray2() As String = {"BasharVS", "Bashar", "BVS"}
If MyEqualsFunction( MyStringArray1, MyStringArray2 ) ThenThen
MsgBox("Got that")
End If
لتقييم مدقق :
Private Function MyEqualsFunction( Byval FirstArray As Object, Byval SecondArray As Object ) As Boolean
If FirstArray Is Nothing Or SecondArray Is Nothing Then Return False
If Not ( FirstArray.Length = SecondArray.Length ) Then Return False
For I As Integer = 0 To UBound( FirstArray )
If Not FirstArray( I ) = SecondArray( I ) Then Return False
Next I
Return True
End Function
تم تعديل هذه المشاركة بواسطة Bashar Rabat في 29 ديسمبر 2008 في 18:34
The important thing is not so much
that every child should be taught,
as that every child should be given the wish to learn.
الله يجزآآك ألــــــــــــف خيــــــر ,,
ويجعله بميزآآن حسنآتك ...
شرح بيسآعدني كثيــر في عمل مشروعــي ,,
..
بس ما فهمت هالفقره ..
ممكن توضـــحهآ ؟؟؟
Split : عملية ذات مرجع نصي أو مصفوفة نصية ( String Array ) تستخدم في حال كان لديك نص يحوي حرف أو اشارة محددة تفصل الكلمات عن بعضها, في اغلبية الأحيان تستخدم لقراءة ملفات نصية تسمى ( CSV ), العملية ( Split ) لها عمليات مختلفة ( سأقوم بشرحها لاحقاً او قريباً ) .
مثال :
For Each SString As String In MyStringVariable.Split(" ")
MsgBox(SString)
Next SString
,,,,,
و عن المقآرنـــه ..
أود لو تشرح لي أيضاً داله comperto
حيث أنهآ تقوم بالمقارنه أبجدياً ..!!
::
,,
سبحآن الله وبحمده .. سبحآن الله العظيم .. ,
مرحباً .
أود أن أشرح كل شيء ( لكن عليّ الذهاب ), وعد مني أن أشرحه قريباً ...
العملية Split, كما ذكرت تعيد مقاطع نصية من نص معين, يتم تقطيع النص وتحويله الى مصفوفة بمااستخدمته " " Whitespace أو اي ( حرف, رمز ) اخر .
في أغلبية الأحيان يتم استخدام الفاصلة, المثال التالي يطلب منك اضافة DataGridView1 و Button1 و Button2 .
الـ Button1 لفتح الملف .
لـ Button2 لحفظ المعلومات .
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal Sender As System.Object, ByVal E As System.EventArgs) Handles MyBase.Load
DataGridView1.Columns.Add("CName", "Name")
DataGridView1.Columns.Add("CMiddleN", "Middle Name")
DataGridView1.Columns.Add("CLastN", "Last Name")
DataGridView1.Columns.Add("CPhoneN", "Phone Number #")
DataGridView1.Columns.Add("CE-mail", "E-mail Address")
For I As Integer = 0 To 19
Dim NRow As String() = {"Empty", "Empty", "Empty", Rnd() * I, "Empty@Hotmail.com"}
DataGridView1.Rows.Add(NRow)
Next
End Sub
Private Sub Button1_Click(ByVal Sender As System.Object, ByVal E As System.EventArgs) Handles Button1.Click
Dim OP As New OpenFileDialog
If OP.ShowDialog = 1 Then
Dim Lines() As String = File.ReadAllLines(OP.FileName)
For Each Line As String In Lines
DataGridView1.Rows.Add(Line.Split(","))
Next
End If
End Sub
Private Sub Button2_Click(ByVal Sender As System.Object, ByVal E As System.EventArgs) Handles Button2.Click
Dim SV As New SaveFileDialog
If SV.ShowDialog = 1 Then
Dim St As New StreamWriter(SV.FileName, FileMode.OpenOrCreate)
For Each DGVR As DataGridViewRow In DataGridView1.Rows
If Not DGVR.Cells(0).Value = String.Empty Then
Dim Line As String = String.Empty
For I As Integer = 0 To 3
Line &= DGVR.Cells(I).Value & ","
Next
Line &= DGVR.Cells(4).Value
St.WriteLine(Line)
End If
Next
St.Close()
End If
End Sub
End Class
The important thing is not so much
that every child should be taught,
as that every child should be given the wish to learn.
جزآآك الله خير ع الشرح ,, لاكــن آنـآ لا أعرف مآهي DataGridView1 ..!!
لآننـــي مبتدئه في برمجه الفيجول بيسك ..
,,,,
وآود دآلــه comper to وكيف تتم المقآرنه ؟؟
وشكــــــرآآآ
,,
سبحآن الله وبحمده .. سبحآن الله العظيم .. ,
السلام عليكــم ورحمـة الله وبركاتــه
لو سمحتو انا حابه اكتب موضوع بس ما قدرت
عشان كذا بحط شوي استفسارات بعد اذن صاحبه الموضوع
انا ادرس فيجول بيسك ليفل 2
واحس اني مرره ملخبطه فيه
خصوصا الاجراءت مرره ضايعه فيها
ياليت تدلوني على دروس ممكن تفيدني
وكل مرره احط بحث في قوقل عن فيجول بيسك 2003 يطلعلي نتيجه البحث عن
فيجول بيسك الاصدار 6
مدري شو الفرق بينهم
أتمنــى أحد يشرح لي دآلــه comper to وكيف تتم المقآرنه أبجدياً ؟؟
وشكــــــرآآآ
سبحآن الله وبحمده .. سبحآن الله العظيم .. ,
اريد أن اطرح عليك سؤال :
كيف سمعت بهذه الدالة ( CompareTo ) ؟
The important thing is not so much
that every child should be taught,
as that every child should be given the wish to learn.
سمعتهـــآآ من معيدتنــآآ .. !!
وقالت ترتب الــ string أو تقــآرن أبجديــاً .....
..
سبحآن الله وبحمده .. سبحآن الله العظيم .. ,
جرب المثال التالي :
[color= #000000; font-weight: bold;]Public [color= #000000; font-weight: bold;]Class TestForm
Inherits [color= #990000;]System[color= #339933;].Windows[color= #339933;].Forms[color= #339933;].Form
[color= #666666; font-style: italic;]#Region " Windows Form Designer generated code "
[color= #000000; font-weight: bold;]Public Sub [color= #000000; font-weight: bold;]New[color= #009900;]([color= #009900;])
MyBase[color= #339933;].[color= #000000; font-weight: bold;]New[color= #009900;]([color= #009900;])
[color= #0000ff;]'This call is required by the Windows Form Designer.
[color= #0000ff;] InitializeComponent()
[color= #0000ff;] 'Add any initialization after the InitializeComponent[color= #009900;]([color= #009900;]) call
[color= #990000;]End Sub
[color= #0000ff;]'Form overrides dispose to clean up the component list.
[color= #0000ff;] Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
[color= #0000ff;] If disposing Then
[color= #0000ff;] If Not (components Is Nothing) Then
[color= #0000ff;] components.Dispose()
[color= #0000ff;] End If
[color= #0000ff;] End If
[color= #0000ff;] MyBase.Dispose(disposing)
[color= #0000ff;] End Sub
[color= #0000ff;] Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
[color= #0000ff;] Friend WithEvents bttnComparerTest As System.Windows.Forms.Button
[color= #0000ff;] 'Required by the Windows Form Designer
[color= #000000; font-weight: bold;]Private components [color= #b1b100;]As [color= #990000;]System[color= #339933;].ComponentModel[color= #339933;].Container
[color= #0000ff;]'NOTE: The following procedure is required by the Windows Form Designer
[color= #0000ff;] 'It can be modified using the Windows Form Designer[color= #339933;].
[color= #0000ff;]'Do not modify it using the code editor.
[color= #0000ff;] <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
[color= #0000ff;] Me.ListBox1 = New System.Windows.Forms.ListBox()
[color= #0000ff;] Me.bttnComparerTest = New System.Windows.Forms.Button()
[color= #0000ff;] Me.SuspendLayout()
[color= #0000ff;] '
[color= #0000ff;]'ListBox1
[color= #0000ff;] '
Me[color= #339933;].ListBox1[color= #339933;].ItemHeight [color= #339933;]= [color= #cc66cc;]18
Me[color= #339933;].ListBox1[color= #339933;].Location [color= #339933;]= [color= #000000; font-weight: bold;]New [color= #990000;]System[color= #339933;].Drawing[color= #339933;].Point[color= #009900;]([color= #cc66cc;]8[color= #339933;], [color= #cc66cc;]8[color= #009900;])
Me[color= #339933;].ListBox1[color= #339933;].Name [color= #339933;]= [color= #0000ff;]"ListBox1"
Me[color= #339933;].ListBox1[color= #339933;].Size [color= #339933;]= [color= #000000; font-weight: bold;]New [color= #990000;]System[color= #339933;].Drawing[color= #339933;].Size[color= #009900;]([color= #cc66cc;]240[color= #339933;], [color= #cc66cc;]256[color= #009900;])
Me[color= #339933;].ListBox1[color= #339933;].TabIndex [color= #339933;]= [color= #cc66cc;]1
[color= #0000ff;]'
[color= #0000ff;] 'bttnComparerTest
[color= #0000ff;]'
[color= #0000ff;] Me.bttnComparerTest.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.Fo
ntStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
[color= #0000ff;] Me.bttnComparerTest.Location = New System.Drawing.Point(24, 272)
[color= #0000ff;] Me.bttnComparerTest.Name = "bttnComparerTest"
[color= #0000ff;] Me.bttnComparerTest.Size = New System.Drawing.Size(208, 40)
[color= #0000ff;] Me.bttnComparerTest.TabIndex = 0
[color= #0000ff;] Me.bttnComparerTest.Text = "Test Custom Comparer"
[color= #0000ff;] '
[color= #0000ff;]'TestForm
[color= #0000ff;] '
Me[color= #339933;].AutoScaleBaseSize [color= #339933;]= [color= #000000; font-weight: bold;]New [color= #990000;]System[color= #339933;].Drawing[color= #339933;].Size[color= #009900;]([color= #cc66cc;]8[color= #339933;], [color= #cc66cc;]19[color= #009900;])
Me[color= #339933;].ClientSize [color= #339933;]= [color= #000000; font-weight: bold;]New [color= #990000;]System[color= #339933;].Drawing[color= #339933;].Size[color= #009900;]([color= #cc66cc;]256[color= #339933;], [color= #cc66cc;]317[color= #009900;])
Me[color= #339933;].Controls[color= #339933;].AddRange[color= #009900;]([color= #000000; font-weight: bold;]New [color= #990000;]System[color= #339933;].Windows[color= #339933;].Forms[color= #339933;].Control[color= #009900;]([color= #009900;]) [color= #009900;]{Me[color= #339933;].ListBox1[color= #339933;], Me[color= #339933;].bttnComparerTe
st[color= #009900;]}[color= #009900;])
Me[color= #339933;].Font [color= #339933;]= [color= #000000; font-weight: bold;]New [color= #990000;]System[color= #339933;].Drawing[color= #339933;].Font[color= #009900;]([color= #0000ff;]"Verdana"[color= #339933;], [color= #cc66cc;]11.25[color= #339933;]!, [color= #990000;]System[color= #339933;].Drawing[color= #339933;].FontStyle[color= #339933;].Regular[color= #339933;],
System[color= #339933;].Drawing[color= #339933;].GraphicsUnit[color= #339933;].Point[color= #339933;], CType[color= #009900;]([color= #cc66cc;]0[color= #339933;], Byte[color= #009900;])[color= #009900;])
Me[color= #339933;].Name [color= #339933;]= [color= #0000ff;]"TestForm"
Me[color= #339933;].Text [color= #339933;]= [color= #0000ff;]"Custom Comparer"
Me[color= #339933;].ResumeLayout[color= #009900;]([color= #000000; font-weight: bold;]False[color= #009900;])
[color= #990000;]End Sub
[color= #666666; font-style: italic;]#End Region
Structure Person
Dim Name [color= #b1b100;]As String
Dim BDate [color= #b1b100;]As [color= #990000;]Date
Dim EMail [color= #b1b100;]As String
[color= #990000;]End Structure
[color= #000000; font-weight: bold;]Private Sub TestComparers[color= #009900;](ByVal sender [color= #b1b100;]As [color= #990000;]System[color= #339933;].Object[color= #339933;], ByVal e [color= #b1b100;]As [color= #990000;]System[color= #339933;].EventArgs[color= #009900;]) Handl
es bttnComparerTest[color= #339933;].Click
Dim AList [color= #b1b100;]As [color= #000000; font-weight: bold;]New ArrayList[color= #009900;]([color= #009900;])
Dim p [color= #b1b100;]As Person
[color= #0000ff;]' POPULATE COLLECTION
[color= #0000ff;] p.Name = "C Person"
[color= #0000ff;] p.EMail = "PersonC@sybex.com"
[color= #0000ff;] p.BDate = #1/1/1961#
[color= #0000ff;] ' ADD FIRST PERSON
[color= #b1b100;]If Not AList[color= #339933;].Contains[color= #009900;](p[color= #009900;]) Then AList[color= #339933;].Add[color= #009900;](p[color= #009900;])
p[color= #339933;].Name [color= #339933;]= [color= #0000ff;]"A Person"
p[color= #339933;].EMail [color= #339933;]= [color= #0000ff;]"PersonA@sybex.com"
p[color= #339933;].BDate [color= #339933;]= [color= #666666; font-style: italic;]#3/3/1961#
[color= #0000ff;]' ADD SECOND PERSON
[color= #0000ff;] If Not AList.Contains(p) Then AList.Add(p)
[color= #0000ff;] p.Name = "B Person"
[color= #0000ff;] p.EMail = "PersonB@sybex.com"
[color= #0000ff;] p.BDate = #2/2/1961#
[color= #0000ff;] ' ADD THIRD PERSON
[color= #b1b100;]If Not AList[color= #339933;].Contains[color= #009900;](p[color= #009900;]) Then AList[color= #339933;].Add[color= #009900;](p[color= #009900;])
[color= #0000ff;]' PRINT COLLECTION AS IS
[color= #0000ff;] Dim PEnum As IEnumerator
[color= #0000ff;] PEnum = AList.GetEnumerator
[color= #0000ff;] ListBox1.Items.Add("Original Collection")
[color= #0000ff;] While PEnum.MoveNext
[color= #0000ff;] ListBox1.Items.Add(CType(PEnum.Current, Person).Name & _
[color= #0000ff;] vbTab & CType(PEnum.Current, Person).BDate)
[color= #0000ff;] End While
[color= #0000ff;] ' [color= #990000;]SORT BY NAME[color= #339933;], THEN [color= #990000;]PRINT COLLECTION
ListBox1[color= #339933;].Items[color= #339933;].Add[color= #009900;]([color= #0000ff;]" "[color= #009900;])
ListBox1[color= #339933;].Items[color= #339933;].Add[color= #009900;]([color= #0000ff;]"Collection Sorted by Name"[color= #009900;])
[color= #0000ff;]' USE THE PersonNameComparer TO SORT ARRAYLIST BY NAME FIELD
[color= #0000ff;] AList.Sort(New PersonNameComparer())
[color= #0000ff;] PEnum = AList.GetEnumerator
[color= #0000ff;] While PEnum.MoveNext
[color= #0000ff;] ListBox1.Items.Add(CType(PEnum.Current, Person).Name & _
[color= #0000ff;] vbTab & CType(PEnum.Current, Person).BDate)
[color= #0000ff;] End While
[color= #0000ff;] ' [color= #990000;]SORT BY AGE[color= #339933;], THEN [color= #990000;]PRINT COLLECTION
ListBox1[color= #339933;].Items[color= #339933;].Add[color= #009900;]([color= #0000ff;]" "[color= #009900;])
ListBox1[color= #339933;].Items[color= #339933;].Add[color= #009900;]([color= #0000ff;]"Collection Sorted by Age"[color= #009900;])
[color= #0000ff;]' USE THE PersonAgeComparer TO SORT ARRAYLIST BY BDATE FIELD
[color= #0000ff;] AList.Sort(New PersonAgeComparer())
[color= #0000ff;] PEnum = AList.GetEnumerator
[color= #0000ff;] While PEnum.MoveNext
[color= #0000ff;] ListBox1.Items.Add(CType(PEnum.Current, Person).Name & _
[color= #0000ff;] vbTab & CType(PEnum.Current, Person).BDate)
[color= #0000ff;] End While
[color= #0000ff;] End Sub
[color= #0000ff;] ' This [color= #000000; font-weight: bold;]Class contains a single method[color= #339933;], which is a [color= #000000; font-weight: bold;]function that
[color= #0000ff;]' implements the IComparer interface and compares two objects of the Person type.
[color= #0000ff;] ' The comparison is based on the BDate field
[color= #0000ff;]' Notice that the two objects passed to the function are casted to the Person type,
[color= #0000ff;] ' to make sure that they expose a BDate field[color= #339933;]. [color= #b1b100;]If not[color= #339933;], the code will fail
[color= #000000; font-weight: bold;]Class PersonAgeComparer [color= #339933;]: Implements IComparer
[color= #000000; font-weight: bold;]Public [color= #000000; font-weight: bold;]Function Compare[color= #009900;](ByVal o1 [color= #b1b100;]As Object[color= #339933;], [color= #990000;]_
ByVal o2 [color= #b1b100;]As Object[color= #009900;]) [color= #b1b100;]As Integer [color= #990000;]_
Implements IComparer[color= #339933;].Compare
Dim person1[color= #339933;], person2 [color= #b1b100;]As Person
Try
person1 [color= #339933;]= CType[color= #009900;](o1[color= #339933;], Person[color= #009900;])
person2 [color= #339933;]= CType[color= #009900;](o2[color= #339933;], Person[color= #009900;])
Catch compareException [color= #b1b100;]As [color= #990000;]System[color= #339933;].Exception
Throw [color= #009900;](compareException[color= #009900;])
[color= #990000;]Exit [color= #000000; font-weight: bold;]Function
[color= #990000;]End Try
[color= #b1b100;]If person1[color= #339933;].BDate [color= #339933;]< person2[color= #339933;].BDate Then
[color= #b1b100;]Return [color= #cc66cc;]-1
[color= #b1b100;]Else
[color= #b1b100;]If person1[color= #339933;].BDate [color= #339933;]> person2[color= #339933;].BDate Then
[color= #b1b100;]Return [color= #cc66cc;]1
[color= #b1b100;]Else
[color= #b1b100;]Return [color= #cc66cc;]0
[color= #990000;]End [color= #b1b100;]If
[color= #990000;]End [color= #b1b100;]If
[color= #990000;]End [color= #000000; font-weight: bold;]Function
[color= #990000;]End [color= #000000; font-weight: bold;]Class
[color= #0000ff;]' This Class contains a single method, which is a function that
[color= #0000ff;] ' implements the IComparer [color= #000000; font-weight: bold;]interface and compares two objects of the Person type[color= #339933;].
[color= #0000ff;]' The comparison is based on the Name field
[color= #0000ff;] ' Notice that the two objects passed to the [color= #000000; font-weight: bold;]function are casted to the Person type[color= #339933;],
[color= #0000ff;]' to make sure that they expose a Name field. If not, the code will fail
[color= #0000ff;] Class PersonNameComparer : Implements IComparer
[color= #0000ff;] Public Function Compare(ByVal o1 As Object, _
[color= #0000ff;] ByVal o2 As Object) As Integer _
[color= #0000ff;] Implements IComparer.Compare
[color= #0000ff;] Dim person1, person2 As Person
[color= #0000ff;] Try
[color= #0000ff;] person1 = CType(o1, Person)
[color= #0000ff;] person2 = CType(o2, Person)
[color= #0000ff;] Catch compareException As System.Exception
[color= #0000ff;] Throw (compareException)
[color= #0000ff;] Exit Function
[color= #0000ff;] End Try
[color= #0000ff;] If person1.Name < person2.Name Then
[color= #0000ff;] Return -1
[color= #0000ff;] Else
[color= #0000ff;] If person1.Name > person2.Name Then
[color= #0000ff;] Return 1
[color= #0000ff;] Else
[color= #0000ff;] Return 0
[color= #0000ff;] End If
[color= #0000ff;] End If
[color= #0000ff;] End Function
[color= #0000ff;] End Class
[color= #0000ff;]End Classمثال مأخوذ من الكتاب ( إحتراف الـ VB.Net ) يطرح فكرة استخدام الـ IComparer, لكن إذا كنت تريد ترتيب مصفوفة نصية يمكنك استخدام التالي :
Dim MyArray As String() = { "BC", "BA", "AB", "AA" }
Array.Sort( MyArray )
For Each ST As String In MyArray
Msgbox( ST )
Next ST
تم تعديل هذه المشاركة بواسطة Bashar Rabat في 1 يناير 2009 في 13:36
The important thing is not so much
that every child should be taught,
as that every child should be given the wish to learn.
الله يجزآآآك خيــــــر ,,
والمثــآل بصرآآحه فيه أشيــآء ما فهمتهـآآ ..!!
لأننآ ما بعد تعمقنــآ بالمـآده ..
لو فيه مثــآل أوضح .. !!
...
وممكن تشرح لي هالسطــر ,,
For Each ST As String In MyArray
وش معنى For Each ؟؟؟
,,
سبحآن الله وبحمده .. سبحآن الله العظيم .. ,
For Each, حلقة تكرارية لعنصر معين, شبيها بـ For لكن For تتعامل مع متغيرات عددية .
مثال لتوضيح الفكرة :
For Each Con As Control In My.Forms.MainForm.Controls
If TypeOf Con Is TextBox Then Msgbox( Con.Text )
Next Con
For I As Integer = 0 To My.Forms.MainForm.Controls.Count - 1
If TypeOf My.Forms.MainForm.Controls( I ) Is TextBox Then Msgbox( My.Forms.MainForm.Controls( I ).Text )
Next I
الفرق واضح, اليس كذلك ؟
بصراحة, لا احب استخدام طرق مشابهة للـ IComparer, دائما ً أتطلع لإنشاء طرقي الخاصة .
تم تعديل هذه المشاركة بواسطة Bashar Rabat في 31 ديسمبر 2008 في 16:47
The important thing is not so much
that every child should be taught,
as that every child should be given the wish to learn.
جزآآك الله ألــف خيـــر ....
ع شرح For Each
...
سؤآل ..
هل يوجد داله خاصه بالــ string
صيغتهآآ
string1 . comperto . string2 ..
مطلوب منــي البحث عنهآ ..
ملاحظه:مومتأكده من الصيغه ..!!
,,
سبحآن الله وبحمده .. سبحآن الله العظيم .. ,
بالتسبة للسؤالك المطروح ( ماهي الـ DataGridView ) هي تحكم خاص لقراءة بيانات في شكل جدول أي يمكنك إدراج عدد من الأعمدة وعدد من الصفوف :
إضافة عمود جديد : DataGridView1.Columns.Add( "Here's the Column Name", "Here's the Column Text ( Header ) " )
إضافة صف جديد ( يمكنك إستخدام مصفوفة نصية ) :
Dim MyRow As String() = { "Rami", "18 Years", "Male", "Systems Analyzer" )
DataGridView1.Rows.Add( MyRow )
The important thing is not so much
that every child should be taught,
as that every child should be given the wish to learn.
String.CompareTo عملية ذات مرجع عددي ( عدد صحيح Integer أو Int32 ) تستخدم لمعرفة إذا كان متغير نصي مقترن بمتغير نصي أخر .
تُرجع القيمة ( 0 ) إذا كان المتغيرين مقترنين .
تُرجع القيمة ( 1 ) إذا كان المتغيرين غير مقترنين ( أي المتغير المقترن ( بالترتيب الآبجدي ) يآتي بعد محتوى المتغير المقترن به ) .
تُرجع القيمة ( 1- ) إذا كان المتغيرين غير مقترنين ( أي المتغير المقترن ( بالترتيب الآبجدي ) يآتي قبل محتوى المتغير المقترن به ) .
أمثلة مع حلول :
Dim ST1 As String = "Caa"
ST1.CompareTo( "Caa" ) = 0
المقترن في محل المقترن به
ST1.CompareTo( "C" ) = 1
المقترن يآتي بعد المقترن به
ST1.CompareTo( "B" ) = 1
المقترن يآتي بعد المقترن به
ST1.CompareTo( "Caab" ) = -1
المقترن يآتي قبل المقترن به
تم تعديل هذه المشاركة بواسطة Bashar Rabat في 1 يناير 2009 في 13:15
The important thing is not so much
that every child should be taught,
as that every child should be given the wish to learn.
جزآآآآآك الله ألــــــف خيــــر ,,
والله يجعله بميــزآآن حسنـآتك ...
,,
سبحآن الله وبحمده .. سبحآن الله العظيم .. ,