How to tame CS0108 ("new" required) errors

M

Michael Steidl

I use XMLspy for handling XML files and started testing its feature to generate
C# code from XML Schemas. Now I got stuck in a strange situation: (I have to
add being not a C# geek, only currently looking deeper into C# considering
making more use of it)

I created C# code from a schema with XMLspy 2005 rel 1. This code worked
flawlessly.

Then I moved to XMLspy 2005 rel. 3 and created C# code from exactly the same
schema again - and ran into a ton of CS0108 errors:

This generated code creates derived classes (as XML Schema types might be
derived from another type). VS2003 raises warnings on a method called Validate
which exists in the base class and in the derived class and VS2003 tells the
"new" keyword is required since the Validate method of the derived class hides
the Validate class of the initial class. This sounds reasonable.

BUT: how does it come to get the rel.1 code compiled without this warning as
the code looks completely the same as the rel.3 code? Is there a VS2003 switch
(in the Project settings ?) to let the methods of the derived class hide the
ones of the base class automatically?

Thanks for any glue

Michael
 
D

Daniel O'Connell [C# MVP]

BUT: how does it come to get the rel.1 code compiled without this warning
as
the code looks completely the same as the rel.3 code? Is there a VS2003
switch
(in the Project settings ?) to let the methods of the derived class hide
the
ones of the base class automatically?

Could you possibly post some sample code? My best guess is\was that there is
a bug in XMLSpy(either in rel1 or rel3) that is causing this issue.

Now, the compiler will hide methods automatically already, thats why CS0108
is awarning, not an error. You have two options. One is to set your warning
level to level 1 warnings and thus ignoring many valueable warnings, but
that would be a very bad thing. The second is the Supress Sepcific Warnings
setting; you should be able to add 108 to that list and the compiler won't
issue that warning.

Both of these options can be found in project properties under Configuration
Properties->Build
 
M

Michael Steidl

Daniel O'Connell said:
Could you possibly post some sample code? My best guess is\was that there is
a bug in XMLSpy(either in rel1 or rel3) that is causing this issue.

Now, the compiler will hide methods automatically already, thats why CS0108
is awarning, not an error. You have two options. One is to set your warning
level to level 1 warnings and thus ignoring many valueable warnings, but
that would be a very bad thing. The second is the Supress Sepcific Warnings
setting; you should be able to add 108 to that list and the compiler won't
issue that warning.

This is an excerpt from the code generated by XMLspy: the base class
ShortStringType and a class CarBrandsType derived from it:
(the base class SchemaString is part of the Altova/XMLspy types library, this
class has no "Validate" method )
Both projects are set to a warning level of 4

***************************************************************************
XMLspy 2005 Release 1: (This code builds WITHOUT warning CS0108)
// BASE class ShortStringType
using Altova.Types;
namespace CarML_2_0.fw
{
public class ShortStringType : SchemaString
{
public ShortStringType() : base()
{
}
public ShortStringType(string newValue) : base(newValue)
{
Validate();
}
public ShortStringType(SchemaString newValue) : base(newValue)
{
Validate();
}
public void Validate()
{
if (Value.Length > GetMaxLength())
throw new System.Exception("Too long");
}
public int GetMaxLength()
{
return 50;
}
}
}
// DERIVED class CarBrandsType
using Altova.Types;
namespace CarML_2_0
{
public class CarBrandsType : fw.ShortStringType
{
public CarBrandsType() : base()
{
}
public CarBrandsType(string newValue) : base(newValue)
{
Validate();
}
public CarBrandsType(SchemaString newValue) : base(newValue)
{
Validate();
}
public void Validate()
{
}
}
}
***************************************************************************
XMLspy 2005 Release 3: (Building this code asserts warning CS0108, see below)
// BASE class ShortStringType
using Altova.Types;
namespace CarML_2_0.fw
{
public class ShortStringType : SchemaString
{
public ShortStringType() : base()
{
}
public ShortStringType(string newValue) : base(newValue)
{
Validate();
}
public ShortStringType(SchemaString newValue) : base(newValue)
{
Validate();
}
public void Validate()
{
if (Value.Length > GetMaxLength())
throw new System.Exception("Too long");
}
public int GetMaxLength()
{
return 50;
}
}
}
// DERIVED class CarBrandsType
using Altova.Types;
namespace CarML_2_0
{
public class CarBrandsType : fw.ShortStringType
{
public CarBrandsType() : base()
{
}
public CarBrandsType(string newValue) : base(newValue)
{
Validate();
}
public CarBrandsType(SchemaString newValue) : base(newValue)
{
Validate();
}
public void Validate() //(warning CS0108 asserted here)
{
}
}
}
**************************************************

I can't see any difference in the code, the warning level is the same - still
puzzled.

Michael
 
J

Jon Skeet [C# MVP]

Michael Steidl said:
This is an excerpt from the code generated by XMLspy: the base class
ShortStringType and a class CarBrandsType derived from it:
(the base class SchemaString is part of the Altova/XMLspy types library, this
class has no "Validate" method )
Both projects are set to a warning level of 4

The old code should generate a warning too. Have you tried doing a full
rebuild with it? Just to prove that the base method is present in the
old code, try calling base.Validate() from CarBrandsType.Validate().
 
M

Michael Steidl

Jon Skeet said:
The old code should generate a warning too. Have you tried doing a full
rebuild with it? Just to prove that the base method is present in the
old code, try calling base.Validate() from CarBrandsType.Validate().

Right, a full rebuild showed the same warnings. In the meantime I got a notice
from the
makers of XMLspy saying this is a bug with their code generator and they will
fix it.

Thanks for your help

Michael

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top