PC Review


Reply
Thread Tools Rate Thread

Class recognition

 
 
G. Seshadri
Guest
Posts: n/a
 
      16th Aug 2004
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.



 
Reply With Quote
 
 
 
 
Alexandre Moura
Guest
Posts: n/a
 
      16th Aug 2004
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" <(E-Mail Removed)>
>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: <wr2dnYzq7OxAjr3cRVn-(E-Mail Removed)>
>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 Removed)
>X-DMCA-Complaints-To: (E-Mail 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


 
Reply With Quote
 
Hari
Guest
Posts: n/a
 
      16th Aug 2004

"Alexandre Moura" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
> 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" <(E-Mail Removed)>
> >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: <wr2dnYzq7OxAjr3cRVn-(E-Mail Removed)>
> >Lines: 37
> >NNTP-Posting-Host: 24.50.157.8
> >X-Trace:

sv3-pHUxlRIVTVtNgcxTNfCgHi8HgBZisCti8wqYmoCGo4O/HI/gSdMQOWXMn221vq+uhhVcpIk0
2iR1OPJ!
>

pyry9uPsibddwrWZizyRm4raRM9NULlmWb/sk0N+qaHHL77d8QgvExWu6rSM4XIS3YS9NJCphGCT
!4pr6ILmvEp5fRQ==
> >X-Complaints-To: (E-Mail Removed)
> >X-DMCA-Complaints-To: (E-Mail 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-onlin
e.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
>
>



 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      16th Aug 2004
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.

"G. Seshadri" <(E-Mail Removed)> wrote in message
news:wr2dnYzq7OxAjr3cRVn-(E-Mail Removed)...
> 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.
>
>
>



 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      16th Aug 2004
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.

"Matt" <(E-Mail Removed)> wrote in message
news:qfVTc.29829$Jo1.25381@lakeread01...
> 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
> 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.
>
> "G. Seshadri" <(E-Mail Removed)> wrote in message
> news:wr2dnYzq7OxAjr3cRVn-(E-Mail Removed)...
> > 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.
> >
> >
> >

>
>



 
Reply With Quote
 
Hari
Guest
Posts: n/a
 
      16th Aug 2004
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" <(E-Mail Removed)> wrote in message
news:qfVTc.29829$Jo1.25381@lakeread01...
> 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.
> 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.
>
> "G. Seshadri" <(E-Mail Removed)> wrote in message
> news:wr2dnYzq7OxAjr3cRVn-(E-Mail Removed)...
> > 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.
> >
> >
> >

>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      16th Aug 2004
* "Hari" <(E-Mail Removed)> scripsit:
> "Alexandre Moura" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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.


Then remove one 'Sub Main'. Your application only needs a single entry point.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
 
Reply With Quote
 
Hari
Guest
Posts: n/a
 
      16th Aug 2004
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!


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing word recognition in Speech Recognition program. Susan Microsoft Word Document Management 0 22nd Apr 2008 04:20 PM
Can't specify a location for new class code file on class diagram>new class dialog? Andy B Microsoft C# .NET 3 14th Feb 2008 01:57 AM
call a mnuFileNew_Click method from a Main MDI class from another class (can be a MdiChild class.. )? M. G, Microsoft Dot NET Framework Forms 1 31st May 2006 06:28 AM
Microphone -- no recognition, no speech recognition tab.. =?Utf-8?B?Q2xpbmNoZXI=?= Windows XP Help 3 24th May 2006 11:44 AM
activex ,money tree dialer,RdxE Class,updaate class,YInstStarter Class damaged RUSSELL Windows XP Performance 1 23rd Nov 2003 02:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:21 AM.