C
C#2y ago
Phonem

✅ classes giving me an error for no reason

namespace Phonem
{
class Program
{
static void Main(string[] args)
{
Book book1 = new Book();
book1.title = "phonem book";
book1.author = "Phonem";
book1.pages = 400;
Console.WriteLine(book1.title.Length);
}
}
}
namespace Phonem
{
class Program
{
static void Main(string[] args)
{
Book book1 = new Book();
book1.title = "phonem book";
book1.author = "Phonem";
book1.pages = 400;
Console.WriteLine(book1.title.Length);
}
}
}
namespace Phonem
{
class Book
{
public string author;
public string title;
public int pages;
}
}
namespace Phonem
{
class Book
{
public string author;
public string title;
public int pages;
}
}
i made this little code using a tutorial im watching, everything was the same except i wanted to try using "book1.title.Length); method. but for some reason its giving me an error which is on the file. please help. thanks
25 Replies
canton7
canton72y ago
Can you post your C#.csproj?
Phonem
PhonemOP2y ago
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net7.0</TargetFramework> <RootNamespace>C_</RootNamespace> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> </Project>
canton7
canton72y ago
That's fine. Is your Book.cs in the same C# folder as your Program.cs?
Phonem
PhonemOP2y ago
idk if this helps
Phonem
PhonemOP2y ago
but it is yeah
canton7
canton72y ago
$vscode
MODiX
MODiX2y ago
Follow the instructions here on getting started with DevKit for C# in VSCode: https://code.visualstudio.com/docs/csharp/get-started
Get started with C# and .NET in Visual Studio Code
Getting Started with C# and .NET Development in Visual Studio Code
canton7
canton72y ago
Hmm, not quite the one I was after. There are some gotchas for setting up vs code with C# IIRC -- Visual Studio is a lot easier What command are you running to try and build?
Phonem
PhonemOP2y ago
on terminal i use "dotnet run" to run my programs could it be something with extensions?
canton7
canton72y ago
Can you zip your project up and post it here? There's something I'm missing, but I can't think what it is. What you have should be fine
Phonem
PhonemOP2y ago
uhhsorry but... how do i convert files into zip? i have never done that before >.< lemme try
canton7
canton72y ago
Right click on your C# folder -> Send To -> Zipped (compressed) folder
Phonem
PhonemOP2y ago
its giving me a virus threat thing for some reason oh wait i got it
Phonem
PhonemOP2y ago
canton7
canton72y ago
OK, it doesn't work here either. Let me find out why... Your Book.cs is empty
Phonem
PhonemOP2y ago
what does that mean?
canton7
canton72y ago
The file Book.cs literally has nothing in it I think you forgot to save it
Phonem
PhonemOP2y ago
... uh oh
Phonem
PhonemOP2y ago
i mean... i got the thing im looking for but this time i got a warning
Phonem
PhonemOP2y ago
(im so dumb 💀)
canton7
canton72y ago
Yeah. That's expected. You've got Nullable Reference Types enabled If you're just starting, set <Nullable>disable</Nullable> in your .csproj You'll appreciate that stuff when you're a bit more experienced. Right now it's just a distraction
Phonem
PhonemOP2y ago
does nullables change anything? a friend told me that i could add ! next to the things to disable them
canton7
canton72y ago
It tells the compiler to give you warnings in some cases Yes you can use ! to silence warnings in some cases... But it's better to properly learn about it so that you can use it effectively and if you're only just starting, you've got more important things to learn
Phonem
PhonemOP2y ago
i pasted the link my friend gave me so i could read it later but yeah, it seems pretty advanced thing for now thanks for the help though! (i really have to not forget saving files i always forget them)
canton7
canton72y ago
No worries!
Want results from more Discord servers?
Add your server