الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كيف ممكن أمرر مصفوفة إلى اجراء؟

بدأه Dena22 في 16 يناير 2009 · 15 رد · 1,008 مشاهدة · في Microsoft Visual Basic.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكــم ورحمـة الله وبركاتــه

أريد أن أعرف كيف ممكن أمرر مصفوفة إلى اجراء؟ لو ممكن أحد يعطيني مثال عملي لذلك لأنني لم استطع فهم الفكرة

وشكرا

#2

مرحباً ( أخي, أختي ) الكريم(ـة) .

مصفوفة من أي نوع, وهل تقصد الوصول اليها عن طريق اسم فضاء أو تمريرها كـ Param .

الوصول عن طريق اسم فضاء - مثال :

 

[color= #000000; font-weight: bold;]Public [color= #000000; font-weight: bold;]Class Form1

    Dim Strings[color= #009900;]([color= #009900;]) [color= #b1b100;]As String [color= #339933;]= [color= #009900;]{[color= #0000ff;]"I"[color= #339933;], [color= #0000ff;]"You"[color= #339933;], [color= #0000ff;]"He"[color= #339933;], [color= #0000ff;]"She"[color= #339933;], [color= #0000ff;]"We"[color= #339933;], [color= #0000ff;]"They"[color= #009900;]}

    [color= #000000; font-weight: bold;]Public MyArray [color= #b1b100;]As [color= #990000;]Array [color= #339933;]= Strings

 

    [color= #000000; font-weight: bold;]Private Sub Form1_Click[color= #009900;](ByVal sender [color= #b1b100;]As Object[color= #339933;], [color= #990000;]_

    ByVal e [color= #b1b100;]As [color= #990000;]System[color= #339933;].EventArgs[color= #009900;]) Handles Me[color= #339933;].Click

        Form2[color= #339933;].Show[color= #009900;]([color= #009900;])

    [color= #990000;]End Sub

 

[color= #990000;]End [color= #000000; font-weight: bold;]Class

الوصول :

 

[color= #000000; font-weight: bold;]Public [color= #000000; font-weight: bold;]Class Form2

 

    [color= #000000; font-weight: bold;]Private Sub Form2_Load[color= #009900;](ByVal sender [color= #b1b100;]As [color= #990000;]System[color= #339933;].Object[color= #339933;], [color= #990000;]_

    ByVal e [color= #b1b100;]As [color= #990000;]System[color= #339933;].EventArgs[color= #009900;]) Handles MyBase[color= #339933;].Load

        [color= #b1b100;]For [color= #990000;]Each St [color= #b1b100;]As String In My[color= #339933;].Forms[color= #339933;].Form1[color= #339933;].MyArray

            MsgBox[color= #009900;](St[color= #009900;])

        [color= #990000;]Next

    [color= #990000;]End Sub

 

[color= #990000;]End [color= #000000; font-weight: bold;]Class

بإستخدام Param تابع لـ Sub أو Function - مثال مع Sub :

 

[color= #000000; font-weight: bold;]Public [color= #000000; font-weight: bold;]Class Form1

    Dim Strings[color= #009900;]([color= #009900;]) [color= #b1b100;]As String [color= #339933;]= [color= #009900;]{[color= #0000ff;]"I"[color= #339933;], [color= #0000ff;]"You"[color= #339933;], [color= #0000ff;]"He"[color= #339933;], [color= #0000ff;]"She"[color= #339933;], [color= #0000ff;]"We"[color= #339933;], [color= #0000ff;]"They"[color= #009900;]}

    [color= #000000; font-weight: bold;]Public MyArray [color= #b1b100;]As [color= #990000;]Array [color= #339933;]= Strings

 

    [color= #000000; font-weight: bold;]Private Sub Form1_Click[color= #009900;](ByVal sender [color= #b1b100;]As Object[color= #339933;], [color= #990000;]_

    ByVal e [color= #b1b100;]As [color= #990000;]System[color= #339933;].EventArgs[color= #009900;]) Handles Me[color= #339933;].Click

        Form2[color= #339933;].ShowPronouns[color= #009900;](MyArray[color= #009900;])

    [color= #990000;]End Sub

 

[color= #990000;]End [color= #000000; font-weight: bold;]Class

التمرير يتم في العملية السابقة Form2.ShowPronouns ويكتمل العمل في الـ Code التالي :

 

[color= #000000; font-weight: bold;]Public [color= #000000; font-weight: bold;]Class Form2

 

    [color= #000000; font-weight: bold;]Public Sub ShowPronouns[color= #009900;](ByVal SomeArray [color= #b1b100;]As [color= #990000;]Array[color= #009900;])

        Dim Strings[color= #009900;]([color= #009900;]) [color= #b1b100;]As String [color= #339933;]= SomeArray

        [color= #b1b100;]For [color= #990000;]Each St [color= #b1b100;]As String In Strings

            MsgBox[color= #009900;](St[color= #009900;])

        [color= #990000;]Next St

    [color= #990000;]End Sub

 

[color= #990000;]End [color= #000000; font-weight: bold;]Class

إذا لم يكن أي من ما ذكر, الرجاء توضيح الفكرة, بـ Code بسيط, وشكراً .

بالتوفيق .

The important thing is not so much

that every child should be taught,

as that every child should be given the wish to learn.

#3

مثال

	Private Sub Tets(ByVal sa() As String)
		MsgBox(sa.Count)
		Dim i As Short
		For i = 0 To sa.Count - 1
			MsgBox(sa(i))
		Next
	End Sub

والاستخدام

		Dim ddf() As String = {"as", "ff", "kk"}
		Tets(ddf)

عن عائشة رضي الله عنها أن النبي صلى الله عليه وسلم قال: إن الله يحب إذا عمل أحدكم عملا أن يتقنه

نعيب زماننا والعيب فينا ... وما لزماننا عيب سوانا

ونهجو ذا الزمان بغير ذنب ... ولو نطق الزمان لنا هجانا

محمد سامر أبو سلو

#4

شكرا جزيلا أخي بشار

ما أريده مثلا هو مصفوفة تحوي 3 أرقام على سبيل المثال يتم تمريرها إلى اجراء يقوم بعملية قسمة عناصر المصفوفة على 2 مثلا

ثم استدعي الاجراء بداخل النافذة

ممكن توضح لي الفكرة كيف تتم؟

وأنا لك شاكرة جداً

#5

شكرا جزيلا اخي محمد

ولكني لم افهم المثال الذي كتبته؟ مالذي يفعله؟

#6

حسناً, اليك هذا المثال :

 

[color= #000000; font-weight: bold;]Public [color= #000000; font-weight: bold;]Class Form1

 

    [color= #000000; font-weight: bold;]Private Sub Button1_Click[color= #009900;](ByVal Sender [color= #b1b100;]As [color= #990000;]System[color= #339933;].Object[color= #339933;], [color= #990000;]_

    ByVal E [color= #b1b100;]As [color= #990000;]System[color= #339933;].EventArgs[color= #009900;]) Handles Button1[color= #339933;].Click

        [color= #b1b100;]For [color= #990000;]Each Dbl [color= #b1b100;]As Double In Divide[color= #009900;]([color= #000000; font-weight: bold;]New Double[color= #009900;]([color= #009900;]) [color= #009900;]{[color= #cc66cc;]128[color= #339933;], [color= #cc66cc;]256[color= #339933;], [color= #cc66cc;]512[color= #009900;]}[color= #009900;])

            MsgBox[color= #009900;](String[color= #339933;].Format[color= #009900;]([color= #0000ff;]"{0} ÷ 2 = {1}"[color= #339933;], CDbl[color= #009900;](Dbl [color= #339933;]* [color= #cc66cc;]2[color= #009900;])[color= #339933;], Dbl[color= #009900;])[color= #009900;])

        [color= #990000;]Next Dbl

    [color= #990000;]End Sub

 

    [color= #000000; font-weight: bold;]Private [color= #000000; font-weight: bold;]Function Divide[color= #009900;](ByVal ArrayOfDbl[color= #009900;]([color= #009900;]) [color= #b1b100;]As Double[color= #009900;]) [color= #b1b100;]As Double[color= #009900;]([color= #009900;])

        Dim DividedNumbers[color= #009900;](UBound[color= #009900;](ArrayOfDbl[color= #009900;])[color= #009900;]) [color= #b1b100;]As Double

        [color= #b1b100;]For I [color= #b1b100;]As Integer [color= #339933;]= [color= #cc66cc;]0 To UBound[color= #009900;](ArrayOfDbl[color= #009900;])

            DividedNumbers[color= #009900;](I[color= #009900;]) [color= #339933;]= CDbl[color= #009900;](ArrayOfDbl[color= #009900;](I[color= #009900;]) [color= #339933;]/ [color= #cc66cc;]2[color= #009900;])

         [color= #990000;]Next I

        [color= #b1b100;]Return DividedNumbers

    [color= #990000;]End [color= #000000; font-weight: bold;]Function

 

[color= #990000;]End [color= #000000; font-weight: bold;]Class

أو :

 

[color= #000000; font-weight: bold;]Public [color= #000000; font-weight: bold;]Class Form1

 

    [color= #000000; font-weight: bold;]Private Sub Button1_Click[color= #009900;](ByVal Sender [color= #b1b100;]As [color= #990000;]System[color= #339933;].Object[color= #339933;], [color= #990000;]_

    ByVal E [color= #b1b100;]As [color= #990000;]System[color= #339933;].EventArgs[color= #009900;]) Handles Button1[color= #339933;].Click

        Divide[color= #009900;]([color= #000000; font-weight: bold;]New Double[color= #009900;]([color= #009900;]) [color= #009900;]{[color= #cc66cc;]128[color= #339933;], [color= #cc66cc;]256[color= #339933;], [color= #cc66cc;]512[color= #009900;]}[color= #009900;])

    [color= #990000;]End Sub

 

    [color= #000000; font-weight: bold;]Private Sub Divide[color= #009900;](ByVal ArrayOfDbl[color= #009900;]([color= #009900;]) [color= #b1b100;]As Double[color= #009900;])

        [color= #b1b100;]For I [color= #b1b100;]As Integer [color= #339933;]= [color= #cc66cc;]0 To UBound[color= #009900;](ArrayOfDbl[color= #009900;])

            MsgBox[color= #009900;](String[color= #339933;].Format[color= #009900;]([color= #0000ff;]"{0} ÷ 2 = {1}"[color= #339933;], ArrayOfDbl[color= #009900;](I[color= #009900;])[color= #339933;], CDbl[color= #009900;](ArrayOfDbl[color= #009900;](I[color= #009900;]) [color= #339933;]/ [color= #cc66cc;]2[color= #009900;])[color= #009900;])[color= #009900;])

        [color= #990000;]Next I

    [color= #990000;]End Sub

 

[color= #990000;]End [color= #000000; font-weight: bold;]Class

علماً أنه ليس عليك التقيد بعدد الأرقام المدخلة, لكن لعدم مواجهة مشكلة معروفة مثل ( Index was out of range ) يجب أن تحوي المصفوفة على أقل ( عنصر واحد ) .

بالتوفيق .

The important thing is not so much

that every child should be taught,

as that every child should be given the wish to learn.

#7

عند تجربة المثال 2

أعطاني خطأين

1) Error 1 Name 'Divide' is not declared.

2) Error 2 Statement is not valid in a namespace.

وكذلك عند تجربة المثال الأول !!!!!!!!!!!!!!!! :wacko:

#8

1) Error 1 Name 'Divide' is not declared.

Private Sub Divide(ByVal ArrayOfDbl() As Double)

2) Error 2 Statement is not valid in a namespace.

من أين تحاول الوصول الى العملية ؟, وما إسم الصف الحاوي لهذه العملية ؟ .

الرجاء إدراج الـ Code الذي تستخدمه ( بشكل كامل ) .

تم تعديل هذه المشاركة بواسطة Bashar Rabat في 17 يناير 2009 في 01:35

The important thing is not so much

that every child should be taught,

as that every child should be given the wish to learn.

#9
Public Class Form1

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Divide(New Double() {128, 256, 512})
	End Sub
End Class
Private Sub Divide(ByVal ArrayOfDbl() As Double)
	For I As Integer = 0 To UBound(ArrayOfDbl)
		MsgBox(String.Format("{0} ÷ 2 = {1}", ArrayOfDbl(I), CDbl(ArrayOfDbl(I) / 2)))

	Next i
End Sub
#10

هل لصقت هذ الـ Code في صف خاص بـ Form وأضفت Button إليها ومن ثم جربتها, فظهرت لك هذه الأخطاء !!!

مستحيل !!! :S

The important thing is not so much

that every child should be taught,

as that every child should be given the wish to learn.

#11

ممكن توضح لي أكثر ماهو المطلوب؟ هل تقصد ان استخدم كلاس جديد؟

#12

حسناً, لتتأكد أن ذلك الـ Code !! يعمل, عمل التالي :

1 - شغل الـ Visual Basic, Visual Studio من جديد .

2 - ثم بدء مشروع Windows Application جديد .

3 - أصف Button لتصبح مطالب الـ Code متوفرة .

4 - افتح محرر الـ Code للـ Form1 .

5 - الصق الـ Code المذكور .

6 - جربه .

تم تعديل هذه المشاركة بواسطة Bashar Rabat في 17 يناير 2009 في 01:53

The important thing is not so much

that every child should be taught,

as that every child should be given the wish to learn.

#13

بالتأكيد لن الصق الكود بدون اضافة ال button الموجود في النموذج ( صحيح أنا مبتدئة ولكنني لست بهذا السوء في البرمجة :S )

ولكنني الآن انتبهت إلى خطأي وهو أنني كنت أعرف الاجراء خارج كلاس الفورم لذلك كان يعطيني خطأ دون ان انتبه :huh:

كماأنني فكرت بطريقة أخرى ووجدت حلها والحمد لله وهي كالتالي

	Private Sub nn(ByVal a As Integer)
		a = a + 100
		Label1.Text = a
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim n, i As Integer
		Dim X() As Integer
		N = InputBox("أدخل عدد العناصر من فضلك")
		ReDim X(N)
		For I = 1 To N
			X(i) = InputBox("أدخل عنصر من القائمة")
		Next I
	End Sub

على أساس أن عدد عناصر المصفوفة غير معروف ويتم تحديده من قبل المستخدم

شكرا جزيلا لك على المساعدة التي أفادتني كثيراً وأوحت لي بالفكرة الثانية أيضا

جزاك الله كل خير ووفقك دائما للخير

#14

أحسنت !!

لكن مالفائدة من الحلقة التكرارية إذا كانت المعلومات ستعرض وتغيير بلمح البصر ؟

اليس عليك استخدام

Label1.Text &= a & Chr(13)

________________________

وللإحتياط استخدم متغيّر للتحقق من القيمة المدخلة كي لايحدث أي خطأ اثناء عملية الإدخال, مثال :

Dim Input = InputBox( "SomeTitle" )

If Not Input Is Nothing Then

If IsNumeric( Input ) Then

' Your Code Here

End If

End If

تم تعديل هذه المشاركة بواسطة Bashar Rabat في 17 يناير 2009 في 02:53

The important thing is not so much

that every child should be taught,

as that every child should be given the wish to learn.

#15

السلام عليكــم ورحمـة الله وبركاتــه

لدي استفسار :

------------

how can I do that >>

----------------------

dim array() as string = { "1","2",...,"n"} 
dim P_report as new report 
P_report.show '

----------------------

'how i can send the array() values to the new window "P_report"

'pleas help me > its verey importatnt

' and thank's

تم تعديل هذه المشاركة بواسطة a7snone في 23 يناير 2009 في 03:24

#16

مرحباً أخي الكريم .

مالذي تريده بالضبط, كيف تقوم بإدخال التالي للرمز ( { ) ؟

dim array() as string = { "1","2",...,"n"} 
dim P_report as new report 
P_report.show '

The important thing is not so much

that every child should be taught,

as that every child should be given the wish to learn.

مواضيع مشابهة