Different errors

Hello community,

I’m following a tutorial series on youtube. I have got the following two errors:
Error: } expected, on this line: public Type Type = { private set; get; }.
I also have this error:
Error: A namespace cannot directly contain members such as fields or methods for this function:

public T Load(string path) {
	T instance;
       	using (TextReader reader = new StreamReader(path)) {
		XmlSerializer xml = new XmlSerializer(type);
		instance = (T)xml.Deserialize(reader);
	}
	return instance;
}

I’m using these namespaces:

using System;
using System.Collections.Generic; <-- Unnecessary, according to MonoDevelop
using System.Linq; <-- Unnecessary, according to MonoDevelop
using System.Text; <-- Unnecessary, according to MonoDevelop

using System.Xml.Serialization;
using System.IO;

If you need any extra information, tell me.

Are you using linux ?
Did you install monogame ?
Did you select a monogame template ?

I also have this error:
Error: A namespace cannot directly contain members such as fields or methods for this function:

In c# we have namespaces within these name spaces we build our classes.

namespace MyVideoGame 
{
  public class MyGamesMain
  {
     public int someVariable = 0;
     public void SomeMethod(){}
  }
  public class MyHelperClass
  {
    ...
  }
}

Methods and variables must be declared and called within a class not a namespace.

namespace MyVideoGame 
{
   // !!! Error !!! methods cannot be declared within the scope of a namespace.
   public void SomeMethod(){}  
}

using System.Collections.Generic; ← Unnecessary, according to MonoDevelop
using System.Linq; ← Unnecessary, according to MonoDevelop
using System.Text; ← Unnecessary, according to MonoDevelop

Don’t worry about this warning, the compiler knows and wont link them in.

Namespaces are C# and other languages like java’s way of preventing name mangling.

It allows them to avoid the need to use headers and avoids clashes between name alaising.

These namespaces are very commonly used so they are left in for convienience.
e.g. Text has classes such as stringbuilder Generics has containers such as List ect.
Linq lets you use a lot of extra extension methods and linq expressions ect ect.

1 Like

It looks like your method needs to take a type parameter so you know what you’re loading: Load<T>

1 Like

Hi,

For @willmotil
Yes, I’m using Linux. It’s not a console application, and I’ve installed MonoGame. I have not selected a MonoGame template though.

Both my method and the variable Type are declared in a class called XmlManager, which is in a namespace called CMETutorialRPG. When I hover over { private set, which has red squiggly lines underneath, it says Error: } expected. I don’t know what to do here though. I’m fairly new to C#, so I don’t know too much about it.

For @jnoyola
Does it become public T Load<T>(string path) then? Because when I just do public Load<T>(string path) it is giving me a whole bunch of other errors…

Get must come before set.
EDIT: Oh, just tried this and apparently not :stuck_out_tongue: I guess that’s just convention then.

Yes, every method except for constructors (and destructors) need a return type. Since you’re returning something of type T, the return type is T.

1 Like

Yes, the first one. The <T> tells the method that it changes depending on the type, and the T before the method denotes that it also returns an instance of that dynamic type.

1 Like

Here’s the code for the whole file: https://pastebin.com/CYy1WYYw. Maybe it’s of any use?

Regarding this, you can’t put an = sign there. If you want to initialize to a default value, you can put it after the getter and setter e.g. int MyInt { get; set; } = 5;.

1 Like

It’s working now! I’ve updated the Pastebin code. The <T> at the Method wasn’t necessary.

edit: late post was just about to tell him to post the example lol…