Interface. How can I fix this?

S

shapper

Hello,

I have the following class:

public class Google : ConfigurationElement, IKeyProvider {
[ConfigurationProperty("Key", IsRequired = true)]
public string Key {
get { return this["Key"] as string; }
} // Key
[ConfigurationProperty("Type", IsRequired = true)]
public string Type {
get { return this["Type"] as string; }
} // Type
} // Google

I have many classes similar to this where the common factor is the Key
so I created:
interface IKeyProvider {
string Key { get; set; }
} // IKeyProvider

However, on my Google class I get an error:
'MyApp.Configuration.Google' does not implement interface member
'MyApp.Configuration.IKeyProvider.Key.set'

How can I fix this?

Thanks,
Miguel
 
A

Ashutosh Bhawasinka

shapper said:
Hello,

I have the following class:

public class Google : ConfigurationElement, IKeyProvider {
[ConfigurationProperty("Key", IsRequired = true)]
public string Key {
get { return this["Key"] as string; }
} // Key
[ConfigurationProperty("Type", IsRequired = true)]
public string Type {
get { return this["Type"] as string; }
} // Type
} // Google

I have many classes similar to this where the common factor is the Key
so I created:
interface IKeyProvider {
string Key { get; set; }
} // IKeyProvider

However, on my Google class I get an error:
'MyApp.Configuration.Google' does not implement interface member
'MyApp.Configuration.IKeyProvider.Key.set'

How can I fix this?

Thanks,
Miguel
You are not implementing IKeyProvider.Key.set! So, if you really don't
want that, still add it and throw some exception from it. Like this

public class Google : ConfigurationElement, IKeyProvider {
[ConfigurationProperty("Key", IsRequired = true)]
public string Key {
get { return this["Key"] as string; }
set{ throw new Exception("Method not impl");}
} // Key
[ConfigurationProperty("Type", IsRequired = true)]
public string Type {
get { return this["Type"] as string; }
} // Type
} // Google
 
S

shapper

shapper said:
I have the following class:
  public class Google : ConfigurationElement, IKeyProvider {
    [ConfigurationProperty("Key", IsRequired = true)]
    public string Key {
      get { return this["Key"] as string; }
    } // Key
    [ConfigurationProperty("Type", IsRequired = true)]
    public string Type {
      get { return this["Type"] as string; }
    } // Type
  } // Google
I have many classes similar to this where the common factor is the Key
so I created:
  interface IKeyProvider {
    string Key { get; set; }
  } // IKeyProvider
However, on my Google class I get an error:
'MyApp.Configuration.Google' does not implement interface member
'MyApp.Configuration.IKeyProvider.Key.set'
How can I fix this?
Thanks,
Miguel

You are not implementing IKeyProvider.Key.set! So, if you really don't
want that, still add it and throw some exception from it. Like this

  public class Google : ConfigurationElement, IKeyProvider {
    [ConfigurationProperty("Key", IsRequired = true)]
    public string Key {
      get { return this["Key"] as string; }
        set{ throw new Exception("Method not impl");}
    } // Key
    [ConfigurationProperty("Type", IsRequired = true)]
    public string Type {
      get { return this["Type"] as string; }
    } // Type
  } // Google

I am not sure if I want or not ... :)

Basically, I have various classes of this type and I need a generic
way to filter an instance of one of these classes as follows:

foreach (IKeyProvider element in child) {
if (element.Key == providedKey) return element;
}

So basically I am looking for an element which key is equal to
providedKey.
Element can be an instance of Google, Mail, Site, ... All these
classes inherit from ConfigurationElement and have a common property:
Key.

Do I need a set?

And can you tell me how it would look?
I am moving from VB.NET to C# and sometimes I get shucked in this
things.

Thanks,
Miguel
 
A

Ashutosh Bhawasinka

shapper said:
shapper said:
Hello,

I have the following class:

public class Google : ConfigurationElement, IKeyProvider {
[ConfigurationProperty("Key", IsRequired = true)]
public string Key {
get { return this["Key"] as string; }
} // Key
[ConfigurationProperty("Type", IsRequired = true)]
public string Type {
get { return this["Type"] as string; }
} // Type
} // Google

I have many classes similar to this where the common factor is the Key
so I created:
interface IKeyProvider {
string Key { get; set; }
} // IKeyProvider

However, on my Google class I get an error:
'MyApp.Configuration.Google' does not implement interface member
'MyApp.Configuration.IKeyProvider.Key.set'

How can I fix this?

Thanks,
Miguel
You are not implementing IKeyProvider.Key.set! So, if you really don't
want that, still add it and throw some exception from it. Like this

public class Google : ConfigurationElement, IKeyProvider {
[ConfigurationProperty("Key", IsRequired = true)]
public string Key {
get { return this["Key"] as string; }
set{ throw new Exception("Method not impl");}
} // Key
[ConfigurationProperty("Type", IsRequired = true)]
public string Type {
get { return this["Type"] as string; }
} // Type
} // Google

I am not sure if I want or not ... :)

Basically, I have various classes of this type and I need a generic
way to filter an instance of one of these classes as follows:

foreach (IKeyProvider element in child) {
if (element.Key == providedKey) return element;
}

So basically I am looking for an element which key is equal to
providedKey.
Element can be an instance of Google, Mail, Site, ... All these
classes inherit from ConfigurationElement and have a common property:
Key.

Do I need a set?

And can you tell me how it would look?
I am moving from VB.NET to C# and sometimes I get shucked in this
things.

Thanks,
Miguel
If you have an interface, then you need to implement all it's method. If
any method of the interface doesn't make sense to your class, you still
need to implement it and throw an exception from it.

The code you just posted (foreach) will work perfectly fine.

I added the code for the "set"...just check it!
 
S

shapper

shapper said:
shapper wrote:
Hello,
I have the following class:
  public class Google : ConfigurationElement, IKeyProvider {
    [ConfigurationProperty("Key", IsRequired = true)]
    public string Key {
      get { return this["Key"] as string; }
    } // Key
    [ConfigurationProperty("Type", IsRequired = true)]
    public string Type {
      get { return this["Type"] as string; }
    } // Type
  } // Google
I have many classes similar to this where the common factor is the Key
so I created:
  interface IKeyProvider {
    string Key { get; set; }
  } // IKeyProvider
However, on my Google class I get an error:
'MyApp.Configuration.Google' does not implement interface member
'MyApp.Configuration.IKeyProvider.Key.set'
How can I fix this?
Thanks,
Miguel
You are not implementing IKeyProvider.Key.set! So, if you really don't
want that, still add it and throw some exception from it. Like this
  public class Google : ConfigurationElement, IKeyProvider {
    [ConfigurationProperty("Key", IsRequired = true)]
    public string Key {
      get { return this["Key"] as string; }
        set{ throw new Exception("Method not impl");}
    } // Key
    [ConfigurationProperty("Type", IsRequired = true)]
    public string Type {
      get { return this["Type"] as string; }
    } // Type
  } // Google
I am not sure if I want or not ... :)
Basically, I have various classes of this type and I need a generic
way to filter an instance of one of these classes as follows:
  foreach (IKeyProvider element in child) {
    if (element.Key == providedKey) return element;
  }
So basically I am looking for an element which key is equal to
providedKey.
Element can be an instance of Google, Mail, Site, ... All these
classes inherit from ConfigurationElement and have a common property:
Key.
Do I need a set?
And can you tell me how it would look?
I am moving from VB.NET to C# and sometimes I get shucked in this
things.
Thanks,
Miguel

If you have an interface, then you need to implement all it's method. If
any method of the interface doesn't make sense to your class, you still
need to implement it and throw an exception from it.

The code you just posted (foreach) will work perfectly fine.

I added the code for the "set"...just check it!

Thanks! Sorry that I didn't see it ... I am used to ReadyOnly VB
properties ...
 
S

shapper

[...]
So basically I am looking for an element which key is equal to
providedKey.
Element can be an instance of Google, Mail, Site, ... All these
classes inherit from ConfigurationElement and have a common property:
Key.
Do I need a set?

Only you can say.

But, if you're only ever _getting_ the property, then no, you don't need a  
setter.  And if that's the case, just remove the "set;" from the interface  
declaration.

Pete

Thanks. It is working now ... finally.
 

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