نمایش نتایج 1 تا 5 از 5

نام تاپیک: خطا در استفاده از Function

  1. #1
    کاربر دائمی آواتار پرستو پارسایی
    تاریخ عضویت
    آبان 1392
    محل زندگی
    تهران
    پست
    458

    خطا در استفاده از Function

    با سلام من در یک پروژه از این 3 Function استفاده میکنم و کاملا بی عیب کار میکنه ولی در ویژوال استودیو در قسمت Error List با ارور های در عکس روبرو میشم لطفا در صورت اطلاع راهنمایی بفرمایید.


    در خط دوم این فانکشن زیر Exit Function خط سبز هست

     Private Function DrawPic(ByVal ZoomX As Single, ByVal ZoomY As Single) As Boolean 
    If IsNothing(srcBitmap) Then Exit Function


    If srcHDC.Equals(IntPtr.Zero) Then
    srcHDC = CreateCompatibleDC(IntPtr.Zero)
    HBitmapSrc = srcBitmap.GetHbitmap()
    SelectObject(srcHDC, HBitmapSrc)
    End If


    If desHDC.Equals(IntPtr.Zero) Then
    If IsNothing(Gr) Then
    Gr = Host.CreateGraphics
    End If
    desHDC = Gr.GetHdc()
    SetStretchBltMode(desHDC, 3)
    End If






    Xout = False
    Yout = False


    If Host.Width > srcBitmap.Width * Zfactor Then
    Mrec.X = 0
    Mrec.Width = srcBitmap.Width
    Brec.X = (Host.Width - srcBitmap.Width * Zfactor) / 2
    Brec.Width = srcBitmap.Width * Zfactor


    BitBlt(desHDC, 0, 0, Brec.X, Host.Height, srcHDC, 0, 0, TernaryRasterOperations.BLACKNESS)
    BitBlt(desHDC, Brec.Right, 0, Brec.X, Host.Height, srcHDC, 0, 0, TernaryRasterOperations.BLACKNESS)
    Else
    Mrec.X = Mrec.X + ((Host.Width / oldZfactor - Host.Width / Zfactor) / ((Host.Width + 0.001) / ZoomX))
    Mrec.Width = Host.Width / Zfactor
    Brec.X = 0
    Brec.Width = Host.Width
    End If


    If Host.Height > srcBitmap.Height * Zfactor Then
    Mrec.Y = 0
    Mrec.Height = srcBitmap.Height
    Brec.Y = (Host.Height - srcBitmap.Height * Zfactor) / 2
    Brec.Height = srcBitmap.Height * Zfactor


    BitBlt(desHDC, 0, 0, Host.Width, Brec.Y, srcHDC, 0, 0, TernaryRasterOperations.BLACKNESS)
    BitBlt(desHDC, 0, Brec.Bottom, Host.Width, Brec.Y, srcHDC, 0, 0, TernaryRasterOperations.BLACKNESS)
    Else
    Mrec.Y = Mrec.Y + ((Host.Height / oldZfactor - Host.Height / Zfactor) / ((Host.Height + 0.001) / ZoomY))
    Mrec.Height = Host.Height / Zfactor
    Brec.Y = 0
    Brec.Height = Host.Height
    End If


    oldZfactor = Zfactor
    '-----------------------------------


    If Mrec.Right > srcBitmap.Width Then
    Xout = True
    Mrec.X = (srcBitmap.Width - Mrec.Width)
    End If


    If Mrec.X < 0 Then
    Xout = True
    Mrec.X = 0
    End If


    If Mrec.Bottom > srcBitmap.Height Then
    Yout = True
    Mrec.Y = (srcBitmap.Height - Mrec.Height)
    End If


    If Mrec.Y < 0 Then
    Yout = True
    Mrec.Y = 0
    End If


    StretchBlt(desHDC, Brec.X, Brec.Y, Brec.Width, Brec.Height, _
    srcHDC, Mrec.X, Mrec.Y, Mrec.Width, Mrec.Height, _
    TernaryRasterOperations.SRCCOPY)


    Gr.ReleaseHdc(desHDC)
    desHDC = Nothing
    End Function



    در این کد در آخرین خط زیر End Function خط سبز هست

     Public Function Dispose()
    If Not IsNothing(srcBitmap) Then
    srcBitmap.Dispose()
    srcBitmap = Nothing
    End If


    If Not srcHDC.Equals(IntPtr.Zero) Then
    DeleteDC(srcHDC)
    srcHDC = Nothing
    End If


    If Not IsNothing(Gr) Then
    Gr.Dispose()
    Gr = Nothing
    End If


    GC.Collect()
    End Function



    در این کد در آخر خط در Returnl.Handle.ToInt64 زیر l خط سبز داره
     Public Function lang_changer_selectable(ByVal lang As String) As Int64 'load the main form's lamguage        Dim l As InputLanguage
    Dim ll As InputLanguageCollection
    Dim a As String
    Try
    ll = InputLanguage.InstalledInputLanguages
    For Each l In ll
    a = Microsoft.VisualBasic.Left(l.LayoutName, 1)


    Select Case lang
    Case "EN"
    If a = "E" Or a = "e" Or a = "u" Or a = "U" Then
    Application.CurrentInputLanguage = l
    Exit For
    End If
    Case "FA"
    If a = "F" Or a = "f" Or a = "P" Or a = "p" Then
    Application.CurrentInputLanguage = l
    Exit For
    End If
    Case "67699721" 'us
    If l.Handle.ToString = lang Then
    Application.CurrentInputLanguage = l
    Exit For
    End If
    Case "69796905" 'fa
    If l.Handle.ToString = lang Then
    Application.CurrentInputLanguage = l
    Exit For
    End If
    End Select
    Next
    FileClose(1)
    Catch ex As Exception
    End Try
    Return l.Handle.ToInt64
    End Function



    سپاسگزارم
    عکس های ضمیمه عکس های ضمیمه
    آخرین ویرایش به وسیله پرستو پارسایی : سه شنبه 25 شهریور 1399 در 13:01 عصر

  2. #2

    نقل قول: خطا در استفاده از Function

    نقل قول نوشته شده توسط پرستو پارسایی مشاهده تاپیک
    با سلام من در یک پروژه از این 3 Function استفاده میکنم و کاملا بی عیب کار میکنه ولی در ویژوال استودیو در قسمت Error List با ارور های در عکس روبرو میشم لطفا در صورت اطلاع راهنمایی بفرمایید.


    در خط دوم این فانکشن زیر Exit Function خط سبز هست

     Private Function DrawPic(ByVal ZoomX As Single, ByVal ZoomY As Single) As Boolean 
    If IsNothing(srcBitmap) Then Exit Function


    If srcHDC.Equals(IntPtr.Zero) Then
    srcHDC = CreateCompatibleDC(IntPtr.Zero)
    HBitmapSrc = srcBitmap.GetHbitmap()
    SelectObject(srcHDC, HBitmapSrc)
    End If


    If desHDC.Equals(IntPtr.Zero) Then
    If IsNothing(Gr) Then
    Gr = Host.CreateGraphics
    End If
    desHDC = Gr.GetHdc()
    SetStretchBltMode(desHDC, 3)
    End If






    Xout = False
    Yout = False


    If Host.Width > srcBitmap.Width * Zfactor Then
    Mrec.X = 0
    Mrec.Width = srcBitmap.Width
    Brec.X = (Host.Width - srcBitmap.Width * Zfactor) / 2
    Brec.Width = srcBitmap.Width * Zfactor


    BitBlt(desHDC, 0, 0, Brec.X, Host.Height, srcHDC, 0, 0, TernaryRasterOperations.BLACKNESS)
    BitBlt(desHDC, Brec.Right, 0, Brec.X, Host.Height, srcHDC, 0, 0, TernaryRasterOperations.BLACKNESS)
    Else
    Mrec.X = Mrec.X + ((Host.Width / oldZfactor - Host.Width / Zfactor) / ((Host.Width + 0.001) / ZoomX))
    Mrec.Width = Host.Width / Zfactor
    Brec.X = 0
    Brec.Width = Host.Width
    End If


    If Host.Height > srcBitmap.Height * Zfactor Then
    Mrec.Y = 0
    Mrec.Height = srcBitmap.Height
    Brec.Y = (Host.Height - srcBitmap.Height * Zfactor) / 2
    Brec.Height = srcBitmap.Height * Zfactor


    BitBlt(desHDC, 0, 0, Host.Width, Brec.Y, srcHDC, 0, 0, TernaryRasterOperations.BLACKNESS)
    BitBlt(desHDC, 0, Brec.Bottom, Host.Width, Brec.Y, srcHDC, 0, 0, TernaryRasterOperations.BLACKNESS)
    Else
    Mrec.Y = Mrec.Y + ((Host.Height / oldZfactor - Host.Height / Zfactor) / ((Host.Height + 0.001) / ZoomY))
    Mrec.Height = Host.Height / Zfactor
    Brec.Y = 0
    Brec.Height = Host.Height
    End If


    oldZfactor = Zfactor
    '-----------------------------------


    If Mrec.Right > srcBitmap.Width Then
    Xout = True
    Mrec.X = (srcBitmap.Width - Mrec.Width)
    End If


    If Mrec.X < 0 Then
    Xout = True
    Mrec.X = 0
    End If


    If Mrec.Bottom > srcBitmap.Height Then
    Yout = True
    Mrec.Y = (srcBitmap.Height - Mrec.Height)
    End If


    If Mrec.Y < 0 Then
    Yout = True
    Mrec.Y = 0
    End If


    StretchBlt(desHDC, Brec.X, Brec.Y, Brec.Width, Brec.Height, _
    srcHDC, Mrec.X, Mrec.Y, Mrec.Width, Mrec.Height, _
    TernaryRasterOperations.SRCCOPY)


    Gr.ReleaseHdc(desHDC)
    desHDC = Nothing
    End Function



    در این کد در آخرین خط زیر End Function خط سبز هست

     Public Function Dispose()        If Not IsNothing(srcBitmap) Then
    srcBitmap.Dispose()
    srcBitmap = Nothing
    End If


    If Not srcHDC.Equals(IntPtr.Zero) Then
    DeleteDC(srcHDC)
    srcHDC = Nothing
    End If


    If Not IsNothing(Gr) Then
    Gr.Dispose()
    Gr = Nothing
    End If


    GC.Collect()
    End Function



    در این کد در آخر خط در Returnl.Handle.ToInt64 زیر l خط سبز داره
     Public Function lang_changer_selectable(ByVal lang As String) As Int64 'load the main form's lamguage        Dim l As InputLanguage
    Dim ll As InputLanguageCollection
    Dim a As String
    Try
    ll = InputLanguage.InstalledInputLanguages
    For Each l In ll
    a = Microsoft.VisualBasic.Left(l.LayoutName, 1)


    Select Case lang
    Case "EN"
    If a = "E" Or a = "e" Or a = "u" Or a = "U" Then
    Application.CurrentInputLanguage = l
    Exit For
    End If
    Case "FA"
    If a = "F" Or a = "f" Or a = "P" Or a = "p" Then
    Application.CurrentInputLanguage = l
    Exit For
    End If
    Case "67699721" 'us
    If l.Handle.ToString = lang Then
    Application.CurrentInputLanguage = l
    Exit For
    End If
    Case "69796905" 'fa
    If l.Handle.ToString = lang Then
    Application.CurrentInputLanguage = l
    Exit For
    End If
    End Select
    Next
    FileClose(1)
    Catch ex As Exception
    End Try
    Return l.Handle.ToInt64
    End Function



    سپاسگزارم
    Function ها بر خلاف Sub ها مقدار بازگشتی دارند و باید مقداری رو Return کنند.
    در زبان ویژوال بیسیک صریحا Return کردن یک عمل اختیاری است و می توانید مقداری رو صریحا Return نکنید تا مقدار پیشفرض اون نوع داده برگردونده بشه.
    اما اینکار توصیه نمیشه حداقل به این دلیل که کامپایلر در این مورد بهتون اخطار میده چون گمان می کنه که شاید اینکار شما عمدی نیست و برگرداندن مقدار را فراموش کرده اید.
    بهتره که بجای اون Exit Function خالی، مقداری رو Return کنید تا مشخص بشه که برگرداندن مقدار رو فراموش نکرده اید و روی مقدار بازگشتی تسلط دارید.

    بر مبنای منطق کامپایلر متغیر l ابتدا در For Each l In ll مقدار دهی شده و پیش از اون مقداری نداره.
    پس اگر مجموعه ll خالی باشه که اونوقت For Each کاری انجام نمیده یا پیش از مقدار دهی l در For Each به دلیلی خطایی در Try پیش بیاد، در Return l.Handle.ToInt64 با l ای روبرو میشه که مقدار اولیه نداره و Nothing ئه. وقتی Nothing باشه دیگه l.Handle وجود نداره و خطا رخ میده.
    برای همین کامپایلر داره اخطار میده که Return l.Handle.ToInt64 رو بدون احتیاط استفاده می کنید و در نظر نمی گیرید که ممکنه l ئه Nothing باشه و در دسترسی به l.Handle خطا رخ بده.
    راه حل های زیادی هست، مثلا می توانید قبل از l.Handle یک شرط If l IsNot Nothing Then اضافه کنید تا صرفا زمانی سراغ l.Handle بره که l مقدار داره.

  3. #3
    کاربر دائمی آواتار پرستو پارسایی
    تاریخ عضویت
    آبان 1392
    محل زندگی
    تهران
    پست
    458

    نقل قول: خطا در استفاده از Function

    سپاسگزارم از پاسخ شما طبق توصیه شما مشکل در کد اول و دوم حل شد ولی در مورد l.Handle.ToInt64 موفق نشدم

  4. #4

    نقل قول: خطا در استفاده از Function

    نقل قول نوشته شده توسط پرستو پارسایی مشاهده تاپیک
    سپاسگزارم از پاسخ شما طبق توصیه شما مشکل در کد اول و دوم حل شد ولی در مورد l.Handle.ToInt64 موفق نشدم

    Public Function lang_changer_selectable(ByVal lang As String) As Int64 'load the main form's language
    Dim l As InputLanguage = Nothing
    Dim ll As InputLanguageCollection
    Dim a As String
    .
    .
    .
    FileClose(1)
    Catch ex As Exception
    End Try
    If l IsNot Nothing Then Return l.Handle.ToInt64 Else Return 0
    End Function

  5. #5
    کاربر دائمی آواتار پرستو پارسایی
    تاریخ عضویت
    آبان 1392
    محل زندگی
    تهران
    پست
    458

    نقل قول: خطا در استفاده از Function

    سپاسگزارم از پاسخ کامل و آموزنده شما

تاپیک های مشابه

  1. سوال: کار نکردن function و return false هنگام فراخواندن function با OnSubmit
    نوشته شده توسط shayan.nabavi در بخش طراحی وب (Web Design)
    پاسخ: 0
    آخرین پست: جمعه 18 تیر 1395, 02:55 صبح
  2. استفاده از sp درون function
    نوشته شده توسط ehsanlionel در بخش T-SQL
    پاسخ: 1
    آخرین پست: سه شنبه 01 اسفند 1391, 16:41 عصر
  3. حرفه ای: با PHP چطوری میشه یه همچین چیزی ساخت؟ <class:function function=methods>
    نوشته شده توسط paayab در بخش PHP
    پاسخ: 7
    آخرین پست: سه شنبه 18 بهمن 1390, 17:29 عصر
  4. نحوه استفاده از function
    نوشته شده توسط amirtofighi در بخش مباحث عمومی دلفی و پاسکال
    پاسخ: 6
    آخرین پست: دوشنبه 12 بهمن 1388, 16:21 عصر

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •