Interface problems (C#)

Morpheus

Regular
Joined
Jan 18, 2002
Messages
62
Interface problems

Hi I'm doing a C# program for customer registrations.

I need to use an interface to validate that the user input is correct (emailaddress must contain @-sign etc) but I don't really understand how I should do.

I have a save method that should look something like this:

C#:
public void newCustomer(IValidate customer)
{
    
    FileStream file = new FileStream(fileName, FileMode.Append);
    BinaryFormatter bf = new BinaryFormatter();

    try
    {
        bf.Serialize(file, customer);
    }

    catch(Exception e)
    {
    }

    finally
    {
        file.Close();
    }
}

and Customer looks like this:
C#:
[Serializable]
public struct Customer
{
    public string firstname;
    public string lastname;
    public string address;
    public string email;
    public string userID;
}

And IValidate looks like this:

C#:
public interface IValidate
{
    bool validate();
}

That is what I have done with the interface so far.

I know I have to do something with the customer struct and add a few more methods but I have no idea how to continue.

I would really appriciate help here, this is getting on my nerves.
 
Not a C# guy but it looks like you'd need to change customer to a class then declare its interface and implement the interface.
C#:
[Serializable]
public class Customer : IValidate
{
    public string firstname;
    public string lastname;
    public string address;
    public string email;
    public string userID;

     public bool validate()
    {
       ...some validation
     }
}

Oops... It looks like my understanding of a struct is not accurate for C#, I guess it means something different in C# and thus the change to a class I suggested is not needed.
 
Why do u need to use an interface

If you are just trying to confirm user input why not use properties?

For example:
Code:
protected String psEmail;
public String EMail
{
     get
     {
          return psEmail;
     }
     set
     {
          // Do some validation here
          psEmail = value;
     }
}
 
Back
Top