Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4626294
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:23:52+00:00 2026-05-22T03:23:52+00:00

in my Silverlight 4 project, I have a simple class implementing the interface like:

  • 0

in my Silverlight 4 project, I have a simple class implementing the interface like:

public interface IMyClass
{
  string Name { get; set; }
  anyotherclass Value { get; set; }
}

a parent class contains a collection of IMyClass elements like:

public class ParentClass
{
  ObservableCollection<IMyClass> Children { get; }
}

Now I want to make sure, that IMyClass.Name is unique in the Children collection. The user can change the IMyClass.Name – so I need to validate if the name is already in the collection. I want to use the Silverlight exception and validation mechanism, my XAML-textbox looks like:

<TextBox Text="{Binding 
                ValidatesOnExceptions=true,
                NotifyOnValidationError=true,
                Mode=TwoWay,
                Path=Name}"/>

So I need to check in the setter of the Name property like:

public string Name
{
    get { return _name; }
    set 
    {
        if (value == _name)
            return;

        if (CollectionAlreadyContainsName(this, value))
            throw new ArgumentException("The name already exists");
        else
            _name = value;

        OnPropertyChanged("Name");
    }
}

And here comes the problem: CollectionAlreadyContainsName needs to know the collection, but in an IMyClass-object I do not know in which collection I am. I would need to keep a reference to the parent like

public interface IMyClass
{
  string Name { get; set; }
  anyotherclass Value { get; set; }
  ParentClass Parent { get; }
}

which tastes bad to me because of cross referencing and the need to asure that the parent is always set properly. So I look for another way to do this. One idea was to let the ParentClass listen to a NameChanged-event of IMyClass and reverting the name change if necessary. But this would not work with the mentioned Silverlight exception and validation mechanism.

Any ideas how to address this?

Thanks in advance,
Frank

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:23:52+00:00Added an answer on May 22, 2026 at 3:23 am

    You are giving the IMyClass the wrong responsibility. It doesn’t know about the collection and it shouldn’t.

    It is the responsibility of the collection class to only add objects that differ in name.

    So override/create an add method that does this and either make the name readonly or subscribe to the change of the name in the collection and throw an error there.

    EDIT

    Do not revert the property. That is not the responsibility of the collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given i have a class like so in my Data Layer public class GenericRepository<TEntity>
In my Silverlight project I have a thread that fires every x milliseconds. In
I have a Silverlight Project which passes data using the Data Contract to a
I have a Silverlight project (with ASP.net MVC web project) Suddenly, when I press
I am just getting started with Silverlight and have recently added a Silverlight project
We have a Silverlight 2 project (game) that will require a lot of character
I have a project with two silverlight apps (foo1 and foo2) and an ASP.NET
I've got two Silverlight Controls in my project, both have properties TeamId. I would
We have a large number of projects within a solution mostly simple class libraries
This is a for a Silverlight project that isn't anchored to an .aspx file,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.