C
C#β€’14mo ago
AJ

❔ System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.

I've been programming for a handful of years, but never to a level of complexity i've been comfortable with in C#, however now in university me and a friend have been taked with creating a small little programming language transpiler as a side project by our tutor. It's a simple Console application which can read information and then will (not complete yet) dump it into a C# file. Somehow I managed to do this in python before, but C# is giving me some headache. Code:
using System;
using System.IO;
using System.Collections.Generic;
internal class Program
{
public static void Main(string[] args)
{
// Create a string array for all items due to be compiled
List<string> ghouLines = new List<string>();
string buildDirectory = Directory.GetCurrentDirectory(); // Build directory
while (true) {
string input = Console.ReadLine();
// Variables
if (input.Substring(0,3) == "mut") {
int equals = input.IndexOf('=') - 1;
string variableName = input.Substring(3,equals);
int endArg = input.IndexOf(';');
string variableData = input.Substring(equals,endArg);
ghouLines.Add($"var {variableName} {variableData};");
}
ghouLines.ForEach(i => Console.Write("{0}\t", i)); // DEBUG
}
}
}
using System;
using System.IO;
using System.Collections.Generic;
internal class Program
{
public static void Main(string[] args)
{
// Create a string array for all items due to be compiled
List<string> ghouLines = new List<string>();
string buildDirectory = Directory.GetCurrentDirectory(); // Build directory
while (true) {
string input = Console.ReadLine();
// Variables
if (input.Substring(0,3) == "mut") {
int equals = input.IndexOf('=') - 1;
string variableName = input.Substring(3,equals);
int endArg = input.IndexOf(';');
string variableData = input.Substring(equals,endArg);
ghouLines.Add($"var {variableName} {variableData};");
}
ghouLines.ForEach(i => Console.Write("{0}\t", i)); // DEBUG
}
}
}
Input:
mut a = 3;
mut a = 3;
43 Replies
AJ
AJOPβ€’14mo ago
Error:
Unhandled Exception:
System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
at System.String.Substring (System.Int32 startIndex, System.Int32 length) [0x0004c] in <baaab171478d4061bd9316f48a642f06>:0
at Program.Main (System.String[] args) [0x00051] in <f464cec0fa31423aa8525a46e0cd2e84>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
at System.String.Substring (System.Int32 startIndex, System.Int32 length) [0x0004c] in <baaab171478d4061bd9316f48a642f06>:0
at Program.Main (System.String[] args) [0x00051] in <f464cec0fa31423aa8525a46e0cd2e84>:0
Unhandled Exception:
System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
at System.String.Substring (System.Int32 startIndex, System.Int32 length) [0x0004c] in <baaab171478d4061bd9316f48a642f06>:0
at Program.Main (System.String[] args) [0x00051] in <f464cec0fa31423aa8525a46e0cd2e84>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
at System.String.Substring (System.Int32 startIndex, System.Int32 length) [0x0004c] in <baaab171478d4061bd9316f48a642f06>:0
at Program.Main (System.String[] args) [0x00051] in <f464cec0fa31423aa8525a46e0cd2e84>:0
Angius
Angiusβ€’14mo ago
Well, one of the substrings is trying to access a part of the string that doesn't exist Using a debugger would help to figure out what the string is at which time
AJ
AJOPβ€’14mo ago
Hm okay thank you, i'll look into that and get back to you @ZZZZZZZZZZZZZZZZZZZZZZZZZ any specific way to do that in VSCode with Mono, i'm on macOS 🀣 - yes i do realise i shot myself in the foot there
Angius
Angiusβ€’14mo ago
Why tf are you using Mono?
AJ
AJOPβ€’14mo ago
idk was recommended by our uni
Angius
Angiusβ€’14mo ago
They're full of shit then
AJ
AJOPβ€’14mo ago
I don't do a lot of C# development πŸ’€
Angius
Angiusβ€’14mo ago
.NET nowadays is fully cross-platform, no need for workarounds like Mono
AJ
AJOPβ€’14mo ago
oh fr?
Angius
Angiusβ€’14mo ago
Just get a .NET 7 SDK and you're good For debugging, get the "C# Dev Kit" extension for VS Code
AJ
AJOPβ€’14mo ago
i assume microsoft publishes the binary links for it alright I already have the c# DEV KIT * sorry caps lock
Angius
Angiusβ€’14mo ago
Ypu're already good, then
AJ
AJOPβ€’14mo ago
anything else I need to compile & run
Angius
Angiusβ€’14mo ago
Nope Just the SDK
AJ
AJOPβ€’14mo ago
Right, is that an extension or binary?
Angius
Angiusβ€’14mo ago
Wym?
AJ
AJOPβ€’14mo ago
how would I install it on macOS is there a download page or is it a VSCode extension
Angius
Angiusβ€’14mo ago
No description
AJ
AJOPβ€’14mo ago
Cool this it infuriates me I was using mono πŸ’€ @ZZZZZZZZZZZZZZZZZZZZZZZZZ it says there is no C# project currently loaded
Angius
Angiusβ€’14mo ago
Where does it say that?
AJ
AJOPβ€’14mo ago
No description
Angius
Angiusβ€’14mo ago
Well, do you have a project? Or is it just a loose .cs file?
AJ
AJOPβ€’14mo ago
loose .cs file
Angius
Angiusβ€’14mo ago
You need a project dotnet new console to create a new console project, for example
AJ
AJOPβ€’14mo ago
right, tysm still says the same I moved the program to Program.cs
Angius
Angiusβ€’14mo ago
Close VS Code, go to the directory with your .csproj file, open VS Code there?
AJ
AJOPβ€’14mo ago
it works, but it's requiring an input and i don't know where i can place it my terminal is blank πŸ™ƒ Sorry i get these are quite nooby questions, it'd be mildly easier if i had access to standard visual studio tbh
Angius
Angiusβ€’14mo ago
There's always Rider If you're a student you can get the license for free $freerider
MODiX
MODiXβ€’14mo ago
There are two ways to get Rider for free: Being a student: https://www.jetbrains.com/community/education/#students Using it for an OSS project: https://www.jetbrains.com/community/opensource/#support
Angius
Angiusβ€’14mo ago
To be honest, dunno how to use the debugger in VS Code
AJ
AJOPβ€’14mo ago
Neither πŸ’€ I just bodge my way until it works 😺 πŸ‘
Angius
Angiusβ€’14mo ago
AJ
AJOPβ€’14mo ago
thank you It's quite late so i'll handle that tomorrow but thanks for all your help
Angius
Angiusβ€’14mo ago
Anytime Ok
Will
Willβ€’14mo ago
JetBrains
Early Access Program - Rider: Cross-platform .NET IDE
.NET IDE based on the IntelliJ platform and ReSharper. Supports C#, ASP.NET, ASP.NET MVC, .NET Core, Unity and Xamarin
g00dSoup
g00dSoupβ€’14mo ago
@AJStrong Visual Studio is the best for C# debugging. You can get the free community version
Angius
Angiusβ€’14mo ago
Not on Mac or Linux, tho
g00dSoup
g00dSoupβ€’14mo ago
Oh no rippp
AJ
AJOPβ€’14mo ago
Which are my two primary operating systems It’s fine I have a spare laptop I can throw Win11 on, that was my plan anyway
Angius
Angiusβ€’14mo ago
Get yourself Rider, you won't regret it
AJ
AJOPβ€’14mo ago
I will ❀️ I love JetBrains tbh They better have a Dracula theme >:3
Accord
Accordβ€’14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server