如何在 VB.NET 中从外部 class 访问函数
How to access a function from an outside class in VB.NET
我正在尝试从另一个 class 访问属于 Class frmamain 的函数。请谁能告诉我我必须如何实现这一目标?我是 VB.NET 的新手。提前致谢。
代码抛出以下错误
"ConvertImagerBnW() is not declared. it may be inaccessible due to its protection level"
class frmamain
Public Function ConvertImagerBnW()
Try
Dim img As Bitmap = New Bitmap((Image.FromFile("D:\imgnam.jpg")))
''Dim img As Bitmap = New Bitmap(ImageBox1.Image)
Dim c As Color
Dim i As Integer = 0
Do While (i < img.Width)
Dim j As Integer = 0
Do While (j < img.Height)
c = img.GetPixel(i, j)
Dim r As Integer = 0
r = Convert.ToInt16(c.R)
Dim g As Integer = 0
g = Convert.ToInt16(c.G)
Dim b As Integer = 0
b = Convert.ToInt16(c.B)
Dim ans As Integer = ((r _
+ (g + b)) _
/ 3)
If (ans > 128) Then
r = 255
g = 255
b = 255
Else
r = 0
g = 0
b = 0
End If
c = Color.FromArgb(r, g, b)
img.SetPixel(i, j, c)
j = (j + 1)
Loop
i = (i + 1)
Loop
''Image<Gray, Byte> normalizedimg = new Image<Gray, Byte>(img);
img.Save("D:\imgnamNew.jpg")
Dim Bmp1 As New Image(Of Bgr, [Byte])("D:\imgnamNew.jpg")
ImageFrame = Bmp1
''ImageBox1.Image = ImageFrame
''ImageBox1.Image.Save("D:\imgnamNew.jpg")
'' MessageBox.Show("SUCESSFULLY DONE")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Function
end class
Module UseThread
Sub newthread()
ConvertImagerBnW()
End Sub
End Module
ConvertImagerBnW
是 frmamain
class 的成员,因此您必须在 class 的实例上实际调用它。这个:
Module UseThread
Sub newthread()
ConvertImagerBnW()
End Sub
End Module
对frmamain
一无所知。最简单的,你需要做这样的事情:
Module UseThread
Sub newthread()
frmamain.ConvertImagerBnW()
End Sub
End Module
这将在 frmamain
的默认实例上调用该方法,假设它是一个表单。如果它不是默认实例或不是表单,那么您需要在某处访问实例表单。这可能看起来像这样:
Module UseThread
Sub newthread()
Dim obj As New frmamain
obj.ConvertImagerBnW()
End Sub
End Module
如果您想在现有实例而不是新实例上调用该方法,那么您必须从某处获取对该实例的引用。具体如何操作取决于项目的具体情况。
我正在尝试从另一个 class 访问属于 Class frmamain 的函数。请谁能告诉我我必须如何实现这一目标?我是 VB.NET 的新手。提前致谢。
代码抛出以下错误
"ConvertImagerBnW() is not declared. it may be inaccessible due to its protection level"
class frmamain
Public Function ConvertImagerBnW()
Try
Dim img As Bitmap = New Bitmap((Image.FromFile("D:\imgnam.jpg")))
''Dim img As Bitmap = New Bitmap(ImageBox1.Image)
Dim c As Color
Dim i As Integer = 0
Do While (i < img.Width)
Dim j As Integer = 0
Do While (j < img.Height)
c = img.GetPixel(i, j)
Dim r As Integer = 0
r = Convert.ToInt16(c.R)
Dim g As Integer = 0
g = Convert.ToInt16(c.G)
Dim b As Integer = 0
b = Convert.ToInt16(c.B)
Dim ans As Integer = ((r _
+ (g + b)) _
/ 3)
If (ans > 128) Then
r = 255
g = 255
b = 255
Else
r = 0
g = 0
b = 0
End If
c = Color.FromArgb(r, g, b)
img.SetPixel(i, j, c)
j = (j + 1)
Loop
i = (i + 1)
Loop
''Image<Gray, Byte> normalizedimg = new Image<Gray, Byte>(img);
img.Save("D:\imgnamNew.jpg")
Dim Bmp1 As New Image(Of Bgr, [Byte])("D:\imgnamNew.jpg")
ImageFrame = Bmp1
''ImageBox1.Image = ImageFrame
''ImageBox1.Image.Save("D:\imgnamNew.jpg")
'' MessageBox.Show("SUCESSFULLY DONE")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Function
end class
Module UseThread
Sub newthread()
ConvertImagerBnW()
End Sub
End Module
ConvertImagerBnW
是 frmamain
class 的成员,因此您必须在 class 的实例上实际调用它。这个:
Module UseThread
Sub newthread()
ConvertImagerBnW()
End Sub
End Module
对frmamain
一无所知。最简单的,你需要做这样的事情:
Module UseThread
Sub newthread()
frmamain.ConvertImagerBnW()
End Sub
End Module
这将在 frmamain
的默认实例上调用该方法,假设它是一个表单。如果它不是默认实例或不是表单,那么您需要在某处访问实例表单。这可能看起来像这样:
Module UseThread
Sub newthread()
Dim obj As New frmamain
obj.ConvertImagerBnW()
End Sub
End Module
如果您想在现有实例而不是新实例上调用该方法,那么您必须从某处获取对该实例的引用。具体如何操作取决于项目的具体情况。