2

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

Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
Paks
  • 1,460
  • 6
  • 26
  • 46
  • Why would you want to return an empty string if your return type is `Ansprechpartner`? What object type does your calling code expect? – hall.stephenk Nov 21 '12 at 18:29
  • Well, you can make Ansprechpartner implicitly convert to a string. Or you can make the Partner property by of type Object. – Mike Christensen Nov 21 '12 at 18:29

6 Answers6

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

         }
    }
  • Hi i know that i can return: partner = new Ansprechpartner(); My Problem ist that i working with a Webgrid. Ich partner == null, the get Method creates a empty cell, to avoid i tried to return a empty string – Paks Nov 21 '12 at 18:34
  • @Paks i've trimmed my answer down. the idea was to return an analog `Ansprechpartner` version of the empty string – Sam I am says Reinstate Monica Nov 21 '12 at 18:38
3

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();
0

you could do something like:

get
{
  if (partner == null)
    return new Ansprechpartner() {whatever = ""};
  else
    return partner;
}
gordy
  • 9,360
  • 1
  • 31
  • 43
0

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.

Khan
  • 17,904
  • 5
  • 47
  • 59
0

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;
}
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • +1 for `return partner ?? String.Empty` not sure why the downvote? – gordy Nov 21 '12 at 18:33
  • Probably because the OP's code didn't even work with strings, which I've edited my answer to address. But it's hard to say since the one who downvoted me didn't have the spine to say why. – Jonathan Wood Nov 21 '12 at 18:35
  • holy cow it's raining downvotes in here, making a run for the door – gordy Nov 21 '12 at 18:40
  • 1
    @gordy as far as I can tell, the only answers which have recieved downvotes, are the ones that just suggested returning an empty string without changing the method signature to allow that to compile, **Which the OP has stated that he tried** – Sam I am says Reinstate Monica Nov 21 '12 at 18:42
  • I got 2 downvotes and I didn't say anything about the property signature. (And by two spineless people too!) I just answered the question asked in the subject, and also pointed out that the OP was a little confused about the data type. – Jonathan Wood Nov 21 '12 at 20:03
0

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.

Community
  • 1
  • 1
Franco Rondini
  • 10,841
  • 8
  • 51
  • 77