IndexOutOfRangeException error when running MSDN example

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
This code, which I got from http://msdn.microsoft.com/en-us/library/tst0kwb1(v=VS.100).aspx http://msdn.microsoft.com/en-us/library/tst0kwb1(v=VS.100).aspx has been kicking up an error message (and a warning) when I try to use it.
<br/>

The first one is a warning: Function Main doesnt return a value on all code paths. Are you missing a Return statement?
<br/>

<br/>

The second is an error that comes up when I try to run it:
<br/>


System.IndexOutOfRangeException was unhandled
Message=Index was outside the bounds of the array.
Source=ConsoleApplication3
StackTrace:
at ConsoleApplication3.Program.Main(String[] args) in C:...ConsoleApplication3Module1.vb:line 12
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

<br/>

<br/>

The code is as follows:
<span style="font-family:Segoe UI,Verdana,Arial; font-size:13px
<pre style="font-family:Consolas,Courier,monospace; word-break:break-all; word-wrap:break-word; font-style:normal; font-weight:normal; overflow-x:auto; overflow-y:auto; padding:5px; margin:0px <span style="color:blue Imports System
<span style="color:blue Imports System.Net
<span style="color:blue Imports System.Net.Sockets
<span style="color:blue Imports System.Text

<span style="color:blue Public <span style="color:blue Class Program

<span style="color:blue Overloads <span style="color:blue Public <span style="color:blue Shared <span style="color:blue Function Main(args() <span style="color:blue As [<span style="color:blue String]) <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Dim s <span style="color:blue As <span style="color:blue New Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp)
<span style="color:blue Dim broadcast <span style="color:blue As IPAddress = IPAddress.Parse(<span style="color:#a31515 "192.168.1.255")
<span style="color:blue Dim sendbuf <span style="color:blue As <span style="color:blue Byte() = Encoding.ASCII.GetBytes(args(0))
<span style="color:blue Dim ep <span style="color:blue As <span style="color:blue New IPEndPoint(broadcast, 11000)
s.SendTo(sendbuf, ep)
Console.WriteLine(<span style="color:#a31515 "Message sent to the broadcast address")
<span style="color:blue End <span style="color:blue Function <span style="color:green Main
<span style="color:blue End <span style="color:blue Class [/code]


View the full article
 
Back
Top