oopaewem oopaewem - 9 months ago 37
C# Question

Using a private Method with return

I have recently start to practice c# and come across this problem:
I just want to save 'dataTaken' in 'public string date', so i can acces this variable directly.

class Bild

public int id;
public String source;
public string dataTaken;
public string data;

private string getAufnahmeDatum () {
string dataTaken = [.....]this.source;
return dataTaken;

**//-> data = this.getAufnahmeDatum(); // dose not work.**


I get some erros, trying this.

date : 'date' dose not exist in the current context


  • Method must have an return type

  • Bild.getAufnahmeDatum must declare a
    body because its not marked abstarct,extern or partial

  • Type Bild already defines a memeber called getAufnahmeDatum with the same
    parameter typs

I find it curios that when I dont define public string dataTaken; i get an error too ( dose not exist in th current context).

I thought string dateTaken = [...] should be enough.

PS: Is there a trick to copy that damn erros out of VisualStudio ?

Answer Source

You get exactly these error messages if you write this code inside the class but not within a method!

// fails with the described error
data = this.getAufnahmeDatum();

// works without error
private void Test()
    data = this.getAufnahmeDatum();

You could for example call the code in the constructor of the class

public Bild()
    data = this.getAufnahmeDatum();