Interface question

Last post 07-17-2008 2:33 AM by Zong-Qing Li - MSFT. 2 replies.

Sort Posts:

  • Interface question

    07-15-2008, 4:59 PM
    • Member
      25 point Member
    • rscott
    • Member since 08-23-2006, 6:10 PM
    • Posts 30

    I have a question about interfaces.

    I have an interface:

    public interface myInterface {
     void setField(string fieldName, object value);
    } 

    Then a class that implements it:

    public void MyClass: myInterface {
     public void setField(string fieldName, object fieldValue) {
     }
    } 

    The programmer would then use the class like so:

    MyClass t = new MyClass();

    t.setField("userID", "10");

    This is all find an dandy... but what I *really* want is for the user to be presented with a list of fields to choose from instead of needing to know the field name and be depended upon to spell it correctly.

    The idea that would work perfectly would be an enumeration:

    So now, my class definition becomes this:

    public void MyClass: myInterface {

     public enum Fields {
      id,
      userID,
      firstName
     }

     public void setField(Fields fieldName, object fieldValue) {

     }
    }

    And to use it:

    MyClass t = new MyClass();

    t.setField(MyClass.Fields.userID, "10");


    Now my question is... how can I do that and make it work from the interface side of things? Is there a way to use generics? or some other way?

    Thanks

    Filed under: ,
  • Re: Interface question

    07-16-2008, 2:43 AM
    • Star
      7,995 point Star
    • MelvynHarbour
    • Member since 06-06-2008, 9:33 AM
    • Cambridge, UK
    • Posts 1,288

    Easier than solving the specific problem would be for you to suggest why you want to do this. I have a strong feeling that there's a different way round this!

  • Re: Interface question

    07-17-2008, 2:33 AM
    Answer
     Hi,

    As far as I can see, you want to update the field's value and get the field's name "intelisense" to avoid spelling mistake. However, based on my research, the enum couldn't be defined in interface and the generics couldn't achieve the "intelisense". So I suggest to use {set,get} visitor to update the field's value, it seems to look like the "intelisense".

    The code is shown below.

    myInterface.cs

        public interface myInterface 
        {
             void upDateField(Table t);
        }

     Table.cs

      public class Table
        {
            private string field;
            public string Field
            {
                set
                {
                    field=value;
                }
                get
                {
                    return field;
                }
            }
            
        }

     MyClass.cs

       public class MyClass:myInterface
        {
            public void upDateField(Table t)
            {
                
            }
          
        }

     invoke.cs

                MyClass c = new MyClass();
                Table t = new Table();
                t.Field = "123";
                c.upDateField(t);

     If I misunderstood the problem, please inform of me.

    I look forward to receiving your test results.

    Sincerely,
    Zong-Qing Li
    Microsoft Online Community Support
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
    Answer” if a marked post does not actually answer your question.

     

    Sincerely,
    Zong-Qing Li
    Microsoft Online Community Support
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
    Answer” if a marked post does not actually answer your question.
Page 1 of 1 (3 items)