ArgumentOutOfRangeException

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1581 Views - Last Post: 06 August 2010 - 10:34 AM Rate Topic: -----

#1 edu2004eu  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 27-August 07

ArgumentOutOfRangeException

Posted 04 August 2010 - 12:44 PM

I have this line of code:

char[] c = new char[100];
c = x.ToCharArray();
server.SendTo(Encoding.ASCII.GetBytes(c), recv, SocketFlags.None, (EndPoint) list[i]);


where x is a non-empty string. When I try to run the code, it gives me the ArgumentOutOfRangeException (Specified argument was out of the range of valid values. Parameter name: size) error. What seems to be the problem? The only thing I modified (from the original working code) was that I replaced

Encoding.ASCII.GetBytes(Data) //the original code; Data was a char array

with
Encoding.ASCII.GetBytes(c)


Sorry if I didn't explain this any more clearly, but this is the best I can, given my C# knowledge and avoiding any unnecessary complications.

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: ArgumentOutOfRangeException

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,661
  • Joined: 02-June 10

Re: ArgumentOutOfRangeException

Posted 04 August 2010 - 12:49 PM

I would check your list[i]
I'll bet i is 40 and you only have 20 items... as an example
Was This Post Helpful? 0
  • +
  • -

#3 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: ArgumentOutOfRangeException

Posted 04 August 2010 - 12:54 PM

View Postedu2004eu, on 04 August 2010 - 10:44 AM, said:

char[] c = new char[100];
c = x.ToCharArray();


Just like to point out that what you are doing here is creating an array of characters that is 100 long, then immediately discarding it and creating a new array of characters from x and assigning it to c.
Was This Post Helpful? 0
  • +
  • -

#4 edu2004eu  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 27-August 07

Re: ArgumentOutOfRangeException

Posted 04 August 2010 - 01:04 PM

View PosttlhIn, on 04 August 2010 - 11:49 AM, said:

I would check your list[i]
I'll bet i is 40 and you only have 20 items... as an example


Nope, i is ok, and list[i] is what it should be.

View PostMomerath, on 04 August 2010 - 11:54 AM, said:

View Postedu2004eu, on 04 August 2010 - 10:44 AM, said:

char[] c = new char[100];
c = x.ToCharArray();


Just like to point out that what you are doing here is creating an array of characters that is 100 long, then immediately discarding it and creating a new array of characters from x and assigning it to c.


I know what I'm doing, but why is that bad?
Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: ArgumentOutOfRangeException

Posted 04 August 2010 - 01:16 PM

View Postedu2004eu, on 04 August 2010 - 11:04 AM, said:

I know what I'm doing, but why is that bad?

Why create an array then immediately dispose of it? What's the point?
Was This Post Helpful? 0
  • +
  • -

#6 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: ArgumentOutOfRangeException

Posted 04 August 2010 - 01:21 PM

EDIT: Double post due to connection problem.

This post has been edited by Cuzzie: 04 August 2010 - 01:22 PM

Was This Post Helpful? 0
  • +
  • -

#7 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: ArgumentOutOfRangeException

Posted 04 August 2010 - 01:21 PM

View Postedu2004eu, on 04 August 2010 - 10:44 AM, said:

server.SendTo(Encoding.ASCII.GetBytes(c), recv, SocketFlags.None, (EndPoint) list[i]);


where x is a non-empty string. When I try to run the code, it gives me the ArgumentOutOfRangeException (Specified argument was out of the range of valid values. Parameter name: size) error. What seems to be the problem? The only thing I modified (from the original working code) was that I replaced

Just FYI, the error is saying that 'recv' is out of the valid range of values. What is 'recv'?

This post has been edited by Momerath: 04 August 2010 - 01:23 PM

Was This Post Helpful? 1
  • +
  • -

#8 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: ArgumentOutOfRangeException

Posted 04 August 2010 - 01:21 PM

Is char[] c = x.toCharArray() not allowed or something?
Was This Post Helpful? 0
  • +
  • -

#9 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: ArgumentOutOfRangeException

Posted 04 August 2010 - 01:22 PM

View PostCuzzie, on 04 August 2010 - 11:21 AM, said:

Is char c = x.toCharArray() not allowed or something?

Nothing wrong with that at all, it's the new char[100] above it that has no purpose in the code.
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,661
  • Joined: 02-June 10

Re: ArgumentOutOfRangeException

Posted 04 August 2010 - 01:41 PM

View PostMomerath, on 04 August 2010 - 12:21 PM, said:

Just FYI, the error is saying that 'recv' is out of the valid range of values. What is 'recv'?


Its in *your* line of code
server.SendTo(Encoding.ASCII.GetBytes(c), recv, SocketFlags.None, (EndPoint) list[i]);

Why are you asking *us* what it is?

Lookup the SendTo method on MSDN and learn what you are supposed to be sending it, rather than just copy/paste someone else's code into your application and expect it to work.


Quote

I know what I'm doing, but why is that bad?
Apparently not.
Was This Post Helpful? 0
  • +
  • -

#11 edu2004eu  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 27-August 07

Re: ArgumentOutOfRangeException

Posted 04 August 2010 - 01:42 PM

View PostMomerath, on 04 August 2010 - 12:21 PM, said:

View Postedu2004eu, on 04 August 2010 - 10:44 AM, said:

server.SendTo(Encoding.ASCII.GetBytes(c), recv, SocketFlags.None, (EndPoint) list[i]);


where x is a non-empty string. When I try to run the code, it gives me the ArgumentOutOfRangeException (Specified argument was out of the range of valid values. Parameter name: size) error. What seems to be the problem? The only thing I modified (from the original working code) was that I replaced

Just FYI, the error is saying that 'recv' is out of the valid range of values. What is 'recv'?


I just spent 10 minutes thinking about how you figured that out, only now I see that the second parameter of the method (or whatever it's called) is called "size". Thank you for that, I thought the error is somewhere in the 1st parameter. I'm not sure what recv is, but I'll figure it out. Thanks.

Edit: any idea what recv could be from this code?
server.ReceiveFrom(data, ref remoteEP);


And about the thing where I create an array and then dispose it, it's because the function can handle only char array parameters.

This post has been edited by edu2004eu: 04 August 2010 - 01:44 PM

Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,661
  • Joined: 02-June 10

Re: ArgumentOutOfRangeException

Posted 04 August 2010 - 01:48 PM

Quote

And about the thing where I create an array and then dispose it, it's because the function can handle only char array parameters.


You're not getting with he's trying to tell you.

char[] c = new char[100];
c = x.ToCharArray();



Line 1 makes a new char[] of 100 elements and assigns it to c
Line 2 reassigns x.ToCharArray() to c

Line 2 completely obliterates the new char[] made in line 1. Making the new char[100] is pointless and just eats up memory.

Just do this

char[]c = x.ToCharArray();

Was This Post Helpful? 0
  • +
  • -

#13 edu2004eu  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 27-August 07

Re: ArgumentOutOfRangeException

Posted 04 August 2010 - 02:01 PM

View PosttlhIn, on 04 August 2010 - 12:48 PM, said:

Quote

And about the thing where I create an array and then dispose it, it's because the function can handle only char array parameters.


You're not getting with he's trying to tell you.

char[] c = new char[100];
c = x.ToCharArray();



Line 1 makes a new char[] of 100 elements and assigns it to c
Line 2 reassigns x.ToCharArray() to c

Line 2 completely obliterates the new char[] made in line 1. Making the new char[100] is pointless and just eats up memory.

Just do this

char[]c = x.ToCharArray();


I need at least 100 chars in that array (x has variable length), so that's why I'm creating 100 items and then assigning it to something.

And whoah on the hostility there. Where in the DIC rules does it say that I have to write 100% of the code? Believe me, I spent enough time writing a lot of code on this program and I said from the beginning that this wasn't *my* code.
Was This Post Helpful? 0
  • +
  • -

#14 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: ArgumentOutOfRangeException

Posted 04 August 2010 - 02:08 PM

Quote

I need at least 100 chars in that array (x has variable length), so that's why I'm creating 100 items and then assigning it to something.


No, you are not. The first assignment to c creates a new array. The second assignment assigns an existing array to c. No elements are copied between the two arrays as they exist independent of each other.
Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,661
  • Joined: 02-June 10

Re: ArgumentOutOfRangeException

Posted 04 August 2010 - 02:15 PM

No hostility. Just frustration at trying to help you, but you keep arguing that the advice we are giving you is wrong, doesn't apply, or that you know better.

Nobody said you had to write all of your own code. But if you are going to copy/paste someone else's code then you should take the time to understand what you are borrowing instead of asking others ... "Hey, what is this variable that I am using?"

I am now unsubscribing from this thread. Since you well know all about what you are trying to do and are correcting the advice being given to you then you know more about this that I do. Best of luck.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2