Paks
Paks

Reputation: 1470

How to return an empty string?

I have the following getter and setter method:

    private Ansprechpartner partner;
    public virtual Ansprechpartner Partner
    {

        get
        {
            if (partner == null)
            {
               // something like partner = " ";
            }
            return partner;  
        }
        set
        {
            partner = value;
        }
    }

In the if clause i want to set partner = " ". But of course this isn't working, cause partner is a Typ a the class Ansprechpartner.

Is there a way to do something equivalent, so that partner returns an empty string if (partner == null)?

Please help

Upvotes: 2

Views: 3988

Answers (6)

Franco Rondini
Franco Rondini

Reputation: 11001

In my opinion there is a straightforward way to get exacly what you ask, that is to ensure that it is syntactically correct the folloging:

get
{
    if (partner == null)
    {
       return = "";
    }
    return partner;  
}

The way is to provide an implicict cast operator for the class. Thanks to implicit cast operator, the String.Empty or "" can be automatically casted to Ansprechpartner type, then it is perfectly legal the sysntax you use for the getter.

but what is a implicict cast operator ? You can even see the question: How do I provide custom cast support for my class? for more detail.

I preferred, however, directly test the code for your class: the code used to successfully test it is the following:

    private  Ansprechpartner partner;
    public virtual Ansprechpartner Partner
    {
        get
        {    
            // legal assignment thanks to **public static implicit operator Ansprechpartner(string s)**
            return partner ?? String.Empty;            
        }
        set
        {
            partner = value;
        }
    }

We also try to make the inverse: thanks to public static implicit operator string(Ansprechpartner a) we see that is possible to assign an Empty string to a Ansprechpartner instance variabile

    public void test_method()
    {
        Ansprechpartner s = String.Empty;         
    }

In the Ansprechpartner class we define cast operators

class Ansprechpartner
{
    public static implicit operator Ansprechpartner(string s) {
        // put your conversion logic here 
        //    .. i.e: you can simply pass string s to a Ansprechpartner constructor
        Ansprechpartner a = new Ansprechpartner();
        return a;
    }

    public static implicit operator string(Ansprechpartner a)
    {
        if (a == null)
            return ""; 
        else 
            return a.ToString(); 
    }

    public Ansprechpartner()
    {
    }

    public override string ToString()
    {
        return Value;
    }
}

That's it, leave a comment if something has not been explained.

Upvotes: 0

MrL Machado
MrL Machado

Reputation: 31

You could override the ToString method from the Ansprechpartner and use a flag attribute like this:

public override ToString()
{
   if (FlagAtrribute == null) //Or if it is a string, you could String.IsNullOrEmpty(FlagAtrribute)
   {
      return "";
   }
   return FlagAtrribute.ToString();
}

And in your getter just return a new empty instance of the Ansprechpartner class

get
{
   if (partner == null)
   {
       partner = new Ansprechpartner();
   }
   return partner;
}

And in your code, do something like this:

MyClass.Partner.ToString();

Upvotes: 3

is Ansprechpartner your own class?

If it is, than you can return your own defenition of an "empty" Ansprechpartner

return Ansprechpartner.Empty;

and then define the empty property

public class Ansprechpartner
{

    public static Ansprechpartner Empty
    {
         get
         {

          //generate an empty Ansprechpartner and return it here

         }
    }

Upvotes: 3

gordy
gordy

Reputation: 9786

you could do something like:

get
{
  if (partner == null)
    return new Ansprechpartner() {whatever = ""};
  else
    return partner;
}

Upvotes: 0

Jonathan Wood
Jonathan Wood

Reputation: 67193

Your property doesn't actually appear to be working with strings, in which case returning a string would be an error.

However, answering your question directly of how to return a string, try something like this:

get
{
    if (partner == null)
        return String.Empty;
    else
        return partner;  
    }
}

Or, better yet:

get
{
    return partner ?? String.Empty;
}

Upvotes: 0

Khan
Khan

Reputation: 18142

If you change your return type from Ansprechpartner to object you can return anything you would like that derives from object. But I would strongly disagree with taking this approach. If you will want to rethink you're entire approach.

Upvotes: 0

Related Questions