Top level statements in C#?

Godot Version

4.2.1

Question

In godot I’m making a tool that reads pdf files, I’m using C# for a PDF reading library, but I’ve never really used C#,
This is my Code:

using Godot;
using System;
using UglyToad.PdfPig;
using UglyToad.PdfPig.Content;

using (PdfDocument document = PdfDocument.Open(@“C:\Users\user\Documents\Game Dev\Godot\Cool_Game\Cool.pdf”)){
int pageCount = document.NumberOfPages;

// Page number starts from 1, not 0.
Page page = document.GetPage(1);

decimal widthInPoints = (decimal)page.Width;
decimal heightInPoints = (decimal)page.Height;

string text = page.Text;
Globals.text = text;

}

public static class Globals{
public static string text = “”;
}

public partial class Mian : Node2D
{

// Called when the node enters the scene tree for the first time.
public override void _Ready(){
	GD.Print(Globals.text);
}

// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta){
}

}

But I’m getting this error:
CS8805: Program using top-level statements must be an executable. C:\Users\user\Documents\Game Dev\Godot\Cool_Game\Main.cs(6,1)

If anybody could enlighten me on a way around using the “using” method or whatever it is in C#, Then I’d be much obliged :slight_smile:

You can use ``` to format your code so others can read it.

If I am wrong, someone correct me, you need to have your code in a class/method. Below is what you have:

using(PdfDocument document = PdfDocument.Open(@“C: \Users\user\Documents\Game Dev\Godot\Cool_Game\Cool.pdf”)) {
	int pageCount = document.NumberOfPages;

	// Page number starts from 1, not 0.
	Page page = document.GetPage(1);

	decimal widthInPoints = (decimal)page.Width;
	decimal heightInPoints = (decimal)page.Height;

	string text = page.Text;
	Globals.text = text;

}

It should be in a class. Below the class is SomeClass. Then the code goes in a method, below the method is Test.

public class SomeClass {
	void Test() {
		using(PdfDocument document = PdfDocument.Open(@“C: \Users\user\Documents\Game Dev\Godot\Cool_Game\Cool.pdf”)) {
			int pageCount = document.NumberOfPages;

			// Page number starts from 1, not 0.
			Page page = document.GetPage(1);

			decimal widthInPoints = (decimal)page.Width;
			decimal heightInPoints = (decimal)page.Height;

			string text = page.Text;
			Globals.text = text;

		}
	}
}
1 Like

So how would I go about calling the Test() method? I can’t seem to reference it. It just keeps saying, “its out of context” or something like that.

var x = new SomeClass();
x.Test();