Class problem

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have the following class:

public class BoxLeaf {
public Box Box { get; set; }
public Validator Validator { get; set; }

public bool Validate() {
this.Validator.Add(Rule.NonEmpty(this.Box.Name), "Fill the
name");
return this.Validator.Success;
}
}

If I have something like:

BoxLeaf leaf = new BoxLeaf();
leaf.Validate();

Validate is not recognized.

If I change my code to:

public static bool Validate() {
this.Validator.Add(Rule.NonEmpty(this.Box.Name), "Fill the
name");
return this.Validator.Success;
}

Then I get the following error:
Keyword 'this' is not valid in a static property, static method, or
static field initializer

How should I do this?

Thanks,
Miguel
 
shapper said:
Hello,

I have the following class:

public class BoxLeaf {
public Box Box { get; set; }
public Validator Validator { get; set; }

public bool Validate() {
this.Validator.Add(Rule.NonEmpty(this.Box.Name), "Fill the
name");
return this.Validator.Success;
}
}

If I have something like:

BoxLeaf leaf = new BoxLeaf();
leaf.Validate();

Validate is not recognized.

If I change my code to:

public static bool Validate() {
this.Validator.Add(Rule.NonEmpty(this.Box.Name), "Fill the
name");
return this.Validator.Success;
}

Then I get the following error:
Keyword 'this' is not valid in a static property, static method, or
static field initializer

How should I do this?

Thanks,
Miguel
The code you have provided should work. The reason you cannot use 'this'
is because static items do not have instances so you cannot refer to a
specific instance.
I copy pasted your code into VS and did the same thing and it works and
compiles (not the static )
Make sure that your namespace is correct and that you are using the
correct namespace. but then BoxLeaf leaf = new BoxLeaf() should not work
either.
What is the exact error you get when you say Validate is not recognized?
 
The code you have provided should work. The reason you cannot use 'this'
is because static items do not have instances so you cannot refer to a
specific instance.
I copy pasted your code into VS and did the same thing and it works and
compiles (not the static )
Make sure that your namespace is correct and that you are using the
correct namespace. but then BoxLeaf leaf = new BoxLeaf() should not work
either.
What is the exact error you get when you say Validate is not recognized?

Somehow it started working ... I probably had some mistake and
corrected without noticing it.

Thanks,
Miguel
 
Back
Top