How to write a component in VB .NET?

  • Thread starter Jay B. Harlow [MVP - Outlook]
  • Start date
J

Jay B. Harlow [MVP - Outlook]

Dr. Zharkov,
In addition to the Imports statement, you need to use 'Project - Add
Reference' to actually reference the assembly where the System.Drawing
namespace is. What may be confusing is the assembly is called
System.Drawing.dll.

Remember Imports deals with namespaces, while Project - References deals
with assemblies. In order to use a namespace you need a reference to the
assembly first.

Hope this helps
Jay
 
D

Dr. Zharkov

We have program Visual Basic .NET 2003 for construction of 3D-Graphics as a
surface z=f (x, y). From VB .NET 2003 we want to transfer coordinates of
this surface as myArrayVB (2000, 1) in myArrayVó [2000, 1] of VC++ .NET
2003 on scheme of "component - client". But there are error 1 and error 2.

For development of a component in VB .NET 2003 we make: File, New, Project,
Visual Basic Projects, Class Library, name of project: ComponentVB. We write
the code:



Imports System.Drawing 'Error 1. Namespace Drawing is absent.



Public Function myFunction1() As Array

Dim myArrayVB As Array = _

Array.CreateInstance(GetType(Single), 2000, 2)

'We write in myArrayVB coordinates of a surface:

. . .

Return myArrayVB

End Function

To apply procedures ScaleTransform and TranslateTransform, we need namespace
Drawing, however this namespace Drawing is absent in this project Class
Library.



For development of the client in VC++ .NET 2003 we make: File, New,
Project, Visual C++ Projects, (.NET), Windows Forms Application (.NET), name
of project: ClientVC. On Form1 we place control PictureBox (on which we want
to build a surface by means of method DrawLine) and we write the code:



#using <mscorlib.dll>

#using "Debug\ComponentVB.dll"



private:

System::Void pictureBox1_Paint(System::Object * sender,

System::Windows::Forms::paintEventArgs * e)

{

ComponentVB::Class1* myObject = new ComponentVB::Class1();



float myArrayVC __gc[,] = new float __gc[2000, 1];



for (int i = 0; i <= 2000; i++)

for (int j = 0; j <= 1; j++)

myArrayVC->SetValue(myObject->myFunction1(), i, j); //Error 2.

}



The second error consists, that we cannot transfer coordinates of a surface
from myArrayVB in myArrayVC.



Inform, please, how in project VB to import the namespace Drawing, and how
to transfer myArrayVB in myArrayVC?



Beforehand many thanks for the answer, Dr. Zharkov V.A., Moscow, Russia.
 
D

Dr. Zharkov

Dear Mr. Jay B. Harlow.

Many thanks for your very valuable consultation, which has helped us to
solve the first problem. But there was a unsolved second problem.




We want to export myArrayVB (2000, 2) of VB .NET 2003 in myArrayVó [2000, 2]
of VC++ .NET 2003 on scheme "component - client". But there is an error.

For development of a component in VB .NET 2003 we make: File, New, Project,
Visual Basic Projects, Class Library, name of project: ComponentVB. We write
the code:



Public Function myFunction1(ByVal N_i As Integer, _

ByVal N_j As Integer) As Array



N_i = 2000

N_j = 2

Dim myArrayVB As Array = _

Array.CreateInstance(GetType(Single), N_i, N_j)



For ii As Integer = 0 To N_i - 1

For jj As Integer = 0 To N_j - 1

myArrayVB(ii, jj) = 1

Next

Next



Return myArrayVB(, )

End Function



For development of the client in VC++ .NET 2003 we make: File, New, Project,
Visual C++ Projects, (.NET), Windows Forms Application (.NET), name of
project: ClientVC. On Form1 we place control Panel (on which we want to
build a surface myArrayVB with the help of method DrawLine), and we write
the code:



#using <mscorlib.dll>

#using "Debug\ComponentVB.dll"



private:

System::Void panel1_Paint(System::Object * sender,

System::Windows::Forms::paintEventArgs * e)

{

ComponentVB::Class1* myObject =

new ComponentVB::Class1();



int N_i = 2000;

int N_j = 2;

float myArrayVC __gc[,] = new float __gc[N_i, N_j];



for (int i = 0; i <= N_i - 1; i++)

for (int j = 0; j <= N_j - 1; j++)

myArrayVC[i, j] =

myArrayVC->SetValue(myObject->myFunction1(2000, 2), i, j);//Err

}



Inform, please, how to correct this code, to export myArrayVB in myArrayVC?



Beforehand many thanks for the answer, Dr. Zharkov V.A., Moscow, Russia.
 
J

Jay B. Harlow [MVP - Outlook]

Dr. Zharkov,
Public Function myFunction1(ByVal N_i As Integer, _
ByVal N_j As Integer) As Array
Dim myArrayVB As Array = _
Array.CreateInstance(GetType(Single), N_i, N_j)

Why are you creating an array object directly? I would simple define the
array as Single().

Something like:
Public Function myFunction1(ByVal N_i As Integer, _
ByVal N_j As Integer) As Single()
Dim myArrayVB(n_i - 1, n_j -1) As Single

Not array bounds in VB.NET are upper bounds, not number of elements, hence
the -1 in defining the array.

Are you getting the error in C++?

I have not use Managed C++ enough to actually answer why you cannot accept
the array.

Hope this helps
Jay

Dr. Zharkov said:
Dear Mr. Jay B. Harlow.

Many thanks for your very valuable consultation, which has helped us to
solve the first problem. But there was a unsolved second problem.




We want to export myArrayVB (2000, 2) of VB .NET 2003 in myArrayVó [2000, 2]
of VC++ .NET 2003 on scheme "component - client". But there is an error.

For development of a component in VB .NET 2003 we make: File, New, Project,
Visual Basic Projects, Class Library, name of project: ComponentVB. We write
the code:



Public Function myFunction1(ByVal N_i As Integer, _

ByVal N_j As Integer) As Array



N_i = 2000

N_j = 2

Dim myArrayVB As Array = _

Array.CreateInstance(GetType(Single), N_i, N_j)



For ii As Integer = 0 To N_i - 1

For jj As Integer = 0 To N_j - 1

myArrayVB(ii, jj) = 1

Next

Next



Return myArrayVB(, )

End Function



For development of the client in VC++ .NET 2003 we make: File, New, Project,
Visual C++ Projects, (.NET), Windows Forms Application (.NET), name of
project: ClientVC. On Form1 we place control Panel (on which we want to
build a surface myArrayVB with the help of method DrawLine), and we write
the code:



#using <mscorlib.dll>

#using "Debug\ComponentVB.dll"



private:

System::Void panel1_Paint(System::Object * sender,

System::Windows::Forms::paintEventArgs * e)

{

ComponentVB::Class1* myObject =

new ComponentVB::Class1();



int N_i = 2000;

int N_j = 2;

float myArrayVC __gc[,] = new float __gc[N_i, N_j];



for (int i = 0; i <= N_i - 1; i++)

for (int j = 0; j <= N_j - 1; j++)

myArrayVC[i, j] =

myArrayVC->SetValue(myObject->myFunction1(2000, 2), i, j);//Err

}



Inform, please, how to correct this code, to export myArrayVB in myArrayVC?



Beforehand many thanks for the answer, Dr. Zharkov V.A., Moscow, Russia.
 
J

Jay B. Harlow [MVP - Outlook]

Dr. Zharkov,
Inform, please, how to correct this code C++, to export myArrayVB in
myArrayVC?

I (still) have not use Managed C++ enough to actually answer why you cannot
use (accept) the array in C++.

Have you tried the microsoft.public.dotnet.languages.vc newsgroup? As that
is where all the Managed C++ developers hang out. Your VB.NET look fine.

Hope this helps
Jay

Dr. Zharkov said:
Dear Mr. Jay B. Harlow.
Many thanks for your consultation. Following your guidelines, we have made:

We want to export myArrayVB (2000, 2) of VB .NET 2003 in myArrayVó [2000, 2]
of VC++ .NET 2003 on scheme "component - client". But there is an error.

For development of a component in VB .NET 2003 we make: File, New, Project,
Visual Basic Projects, Class Library, name of project: ComponentVB. We write
the code:



Public Function myFunction1()

Dim i, j As Integer

Dim N_x As Integer = 2000

Dim N_y As Integer = 1

Dim myArrayVB(N_x, N_y) As Single

For i = 0 To N_x

For j = 0 To N_y

myArrayVB(i, j) = 1

Next

Next

Return myArrayVB

End Function



For development of the client in VB .NET 2003 we make: File, New, Project,
Visual Basic Projects, Windows Application, name of project: ClientVB. On
Form1 we place control PictureBox. We add: Project, Add Reference,
ComponentVB.dll. And we write the code:



Private Sub PictureBox1_Paint(ByVal sender As Object, _

ByVal e As System.Windows.Forms.PaintEventArgs) _

Handles PictureBox1.Paint

Dim i, j As Integer

Dim N_x As Integer = 2000

Dim N_y As Integer = 1

Dim myArrayVB_Client(N_x, N_y) As Single

Dim myObject As New ComponentVB.Class1

For i = 0 To N_x

For j = 0 To N_y

myArrayVB_Client(i, j) = myObject.myFunction1(i, j)

Next

Next

MsgBox("myArrayVB_Client(0,0 ) = " _

& myArrayVB_Client(0, 0)) ' = 1, ok.

End Sub



Export of an array from VB in VB we have fulfilled without errors.



For development of the client in VC++ .NET 2003 we make: File, New, Project,
Visual C++ Projects, (.NET), Windows Forms Application (.NET), name of
project: ClientVC. On Form1 we place control PictureBox, and we write the
code:



#using <mscorlib.dll>

#using "Debug\ComponentVB.dll"



private:

System::Void pictureBox1_Paint(System::Object * sender,

System::Windows::Forms::paintEventArgs * e)

{

ComponentVB::Class1* myObject = new ComponentVB::Class1();

int i, j;

int N_x = 2001;

int N_y = 2;

float myArrayVC __gc[,] = new float __gc[N_x, N_y];

for (int i = 0 ; i <= N_x - 1; i++)

for (int j = 0 ; j <= N_y - 1; j++)

myArrayVC[i, j] = myObject->myFunction1(i, j); //Error.

}



Inform, please, how to correct this code C++, to export myArrayVB in
myArrayVC?



Beforehand many thanks for the answer, Dr. Zharkov V.A., Moscow, Russia.
 
T

Tom Leylan

Well I'm reaching back more than a dozen years to my C/C++ work but here is
what I see...

No problems in the VB function.
Public Function myFunction1()
Dim i, j As Integer
Dim N_x As Integer = 2000
Dim N_y As Integer = 1
Dim myArrayVB(N_x, N_y) As Single
For i = 0 To N_x
For j = 0 To N_y
myArrayVB(i, j) = 1
Next
Next
Return myArrayVB
End Function

No problem with the test in VB but... the way you have it written you are
having myFunction execute every time in the loop. It calls the method and
refills the value 1 into all the elements over and over again.
Private Sub PictureBox1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

Dim i, j As Integer
Dim N_x As Integer = 2000
Dim N_y As Integer = 1
Dim myArrayVB_Client(N_x, N_y) As Single
Dim myObject As New ComponentVB.Class1

For i = 0 To N_x
For j = 0 To N_y
myArrayVB_Client(i, j) = myObject.myFunction1(i, j)
Next
Next

MsgBox("myArrayVB_Client(0,0 ) = " _
& myArrayVB_Client(0, 0)) ' = 1, ok.

End Sub


Your C++ test has two things which are "odd" the first is the same as with
your VB test that you call myFunction over and over which is just a waste of
time since the values never change. And you are declaring int i, j twice.
Once in the body of the procedure and again locally in the loop "int i = 0"
as far as I know CSharp complains about it with Option Strict on but I don't
know what C++ thinks about it.
System::Void pictureBox1_Paint(System::Object * sender,
System::Windows::Forms::paintEventArgs * e)

{
ComponentVB::Class1* myObject = new ComponentVB::Class1();
int i, j;
int N_x = 2001;
int N_y = 2;

float myArrayVC __gc[,] = new float __gc[N_x, N_y];

for (int i = 0 ; i <= N_x - 1; i++)
for (int j = 0 ; j <= N_y - 1; j++)
myArrayVC[i, j] = myObject->myFunction1(i, j); //Error.

}

Other than that I don't see anything but then I didn't try writing it in
C++. It worked fine in CSharp. It would help to know what the error was.

I doubt that it will help much (if at all) but I often suggest that when one
is trying to solve a problem such as this that one eliminate all the
unnecessary code and reduce the problem to as small a package as possible.
That means moving some test code outside of the picturebox paint event. I
can pretty much guarantee that your problem is not related to the paint
event but my point is that you don't want to add "anything" that could
possibly be affecting it if you don't have to. Shrink the sample array to
something less than 2001 by 2... again it won't be the cause of the problem
but is doesn't help solve it either.

You can run simple tests using small functions, procedures and classes with
an array of 2 by 2 until you get it working. Once solved you resize the
array and move it back into the event handler.

I probably can't be of much help but what was the error message?

Tom Leylan
 
D

Dr. Zharkov

Dear Mr. Jay B. Harlow.
Many thanks for your consultation. Following your guidelines, we have made:

We want to export myArrayVB (2000, 2) of VB .NET 2003 in myArrayVó [2000, 2]
of VC++ .NET 2003 on scheme "component - client". But there is an error.

For development of a component in VB .NET 2003 we make: File, New, Project,
Visual Basic Projects, Class Library, name of project: ComponentVB. We write
the code:



Public Function myFunction1()

Dim i, j As Integer

Dim N_x As Integer = 2000

Dim N_y As Integer = 1

Dim myArrayVB(N_x, N_y) As Single

For i = 0 To N_x

For j = 0 To N_y

myArrayVB(i, j) = 1

Next

Next

Return myArrayVB

End Function



For development of the client in VB .NET 2003 we make: File, New, Project,
Visual Basic Projects, Windows Application, name of project: ClientVB. On
Form1 we place control PictureBox. We add: Project, Add Reference,
ComponentVB.dll. And we write the code:



Private Sub PictureBox1_Paint(ByVal sender As Object, _

ByVal e As System.Windows.Forms.PaintEventArgs) _

Handles PictureBox1.Paint

Dim i, j As Integer

Dim N_x As Integer = 2000

Dim N_y As Integer = 1

Dim myArrayVB_Client(N_x, N_y) As Single

Dim myObject As New ComponentVB.Class1

For i = 0 To N_x

For j = 0 To N_y

myArrayVB_Client(i, j) = myObject.myFunction1(i, j)

Next

Next

MsgBox("myArrayVB_Client(0,0 ) = " _

& myArrayVB_Client(0, 0)) ' = 1, ok.

End Sub



Export of an array from VB in VB we have fulfilled without errors.



For development of the client in VC++ .NET 2003 we make: File, New, Project,
Visual C++ Projects, (.NET), Windows Forms Application (.NET), name of
project: ClientVC. On Form1 we place control PictureBox, and we write the
code:



#using <mscorlib.dll>

#using "Debug\ComponentVB.dll"



private:

System::Void pictureBox1_Paint(System::Object * sender,

System::Windows::Forms::paintEventArgs * e)

{

ComponentVB::Class1* myObject = new ComponentVB::Class1();

int i, j;

int N_x = 2001;

int N_y = 2;

float myArrayVC __gc[,] = new float __gc[N_x, N_y];

for (int i = 0 ; i <= N_x - 1; i++)

for (int j = 0 ; j <= N_y - 1; j++)

myArrayVC[i, j] = myObject->myFunction1(i, j); //Error.

}



Inform, please, how to correct this code C++, to export myArrayVB in
myArrayVC?



Beforehand many thanks for the answer, Dr. Zharkov V.A., Moscow, Russia.
 
D

Dr. Zharkov

Many thanks for your help in the decision of my problems on export of an
array from Visual Basic in Visual C++. Mr. Cor has very much helped me to
write an array on a disk in project of VB and to read an array in project of
VC++. Mr. Jay B. Harlow has very much helped me to write correctly an array
as component of VB. Mr. Emad Barsoum from group : vc.libraries for 15.2.04
has very much helped me to write correctly a code for reading an array for
client of VC++ in the following kind:

int i, j;

int N_x = 2001;

int N_y = 2;

Array* myArray = (Array*)myObject->myFunction1();

float myArrayVC __gc[,] = new float __gc[N_x, N_y];

for (i = 0 ; i <= N_x - 1; i++)

for (j = 0 ; j <= N_y - 1; j++)

myArrayVC[i, j] =

System::Convert::ToSingle(myArray->GetValue(i,j));



Big many thanks. Dr. Zharkov V.A., Moscow, Russia.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top