Class recognition

  • Thread starter Thread starter G. Seshadri
  • Start date Start date
G

G. Seshadri

In .NET programming I noticed that if I make a file called Box1.vb with the
following code:

Imports System

Class Box1
Shared Sub Main()
End Sub

Function test (ByVal num As Integer) As Integer
Return num*2
End Function
End Class

then I can't take advantage of it in any other file. For example, If I made
another class called Box2.vb and put it in the same directory and typed in
the following code, the compiler would give me an error:

Imports System

Class Box2
Shared Sub Main()
Dim b1 As New Box1
Console.WriteLine(b1.test(4))
End Sub
End Class

What do I need to do so that Box2 recognizes the class Box1?

P.S.
However, if I put the code of Box2.vb inside Box1.vb and deleted Box2.vb
altogether, then it would compile successfully. However, I would need to
call the file Box2.vb, not Box1.vb, since Box2.vb's main function is going
to be executed.
 
How are you compiling this code? for me if I compile with "vbc box1.vb box2.vb" what I get is an error for having two shared sub main statements.
You may want to read up on building libraries, references, or getting VB .Net Express.

Hope that helps,
Alex (MS VB QA)

--------------------
NNTP-Posting-Date: Sun, 15 Aug 2004 20:54:37 -0500
From: "G. Seshadri" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.vb
Subject: Class recognition
Date: Sun, 15 Aug 2004 21:54:38 -0400
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
Message-ID: <[email protected]>
Lines: 37
NNTP-Posting-Host: 24.50.157.8
X-Trace: sv3-pHUxlRIVTVtNgcxTNfCgHi8HgBZisCti8wqYmoCGo4O/HI/gSdMQOWXMn221vq+uhhVcpIk02iR1OPJ! pyry9uPsibddwrWZizyRm4raRM9NULlmWb/sk0N+qaHHL77d8QgvExWu6rSM4XIS3YS9NJCphGCT!4pr6ILmvEp5fRQ==
X-Complaints-To: (e-mail address removed)
X-DMCA-Complaints-To: (e-mail address removed)
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.13
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-online.de!border2.nntp.dca.giganews.com! border1.nntp.dca.giganews.com!nntp.giganews.com!local1.nntp.dca.giganews.com!nntp.adelphia.com!news.adelphia.com.POSTED!not-for-mail
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:223149
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

In .NET programming I noticed that if I make a file called Box1.vb with the
following code:

Imports System

Class Box1
Shared Sub Main()
End Sub

Function test (ByVal num As Integer) As Integer
Return num*2
End Function
End Class

then I can't take advantage of it in any other file. For example, If I made
another class called Box2.vb and put it in the same directory and typed in
the following code, the compiler would give me an error:

Imports System

Class Box2
Shared Sub Main()
Dim b1 As New Box1
Console.WriteLine(b1.test(4))
End Sub
End Class

What do I need to do so that Box2 recognizes the class Box1?

P.S.
However, if I put the code of Box2.vb inside Box1.vb and deleted Box2.vb
altogether, then it would compile successfully. However, I would need to
call the file Box2.vb, not Box1.vb, since Box2.vb's main function is going
to be executed.

Thanks & Regards
Santhosh Somayajulu
Internet Information Services Team,
Microsoft
 
Alexandre Moura said:
How are you compiling this code?

In my command prompt compiler, I type in vbc Box2.vb. This should compile
Box1.vb since Box2.vb has a reference to Box1.vb. In java, thats exactly how
I compiled it, except using javac and a .java extension.

for me if I compile with "vbc box1.vb box2.vb" what I get is an error for
having two shared sub main statements.
 
If I understand your problem, I think this is the solution to your problem:

Class Box1
Sub New()
End Sub

Function test (ByVal num As Integer) As Integer
Return num*2
End Function
End Class

Imports System
Class Box2
Shared Sub New()
Dim b1 As NewBox1
Console.WriteLine(b1.test(4))
End Sub
End Class

As far as I know, VB.NET doesn't look for Sub Main() to initialize a class.
It looks for Sub New(). In fact, you don't even need to include Sub New()
in your class unless you have special code to execute at initialization.
 
Sorry, I have added a correction to the code below. The "Shared" modifier
should not be used for Sub New() in either class, but you may want you make
sure you specify that both New() sub routines are Public.
 
Are you sure you don't even need a Shared Sub Main in the Box2.vb? What
would the compiler execute? However, I haven't tried your code out yet, and
I am completely new to VB.NET so I'm not sure.
Matt said:
If I understand your problem, I think this is the solution to your problem:

Class Box1
Sub New()
End Sub

Function test (ByVal num As Integer) As Integer
Return num*2
End Function
End Class

Imports System
Class Box2
' shouldn't you have a Shared Sub Main() clause here?
Shared Sub New()
Dim b1 As NewBox1
' i think its a typo, should be New Box1.
 
* "Hari said:
In my command prompt compiler, I type in vbc Box2.vb. This should compile
Box1.vb since Box2.vb has a reference to Box1.vb. In java, thats exactly how
I compiled it, except using javac and a .java extension.

for me if I compile with "vbc box1.vb box2.vb" what I get is an error for
having two shared sub main statements.

Then remove one 'Sub Main'. Your application only needs a single entry point.
 
Thanks everyone for all of your help, I solved the problem:

What I needed to compile it differently than how I was. I compiled the box1
and box2 seperately, but really I should have compiled it as below:

vbc box1.vb box2.vb

like you guys suggested instead of

vbc box1.vb (enter)
vbc box2.vb (enter)

If I do it the second way, then I get an error that box1 doesn't have a
main method.

Once again, thanks all!
 

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

Back
Top