Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hai All

I have a function

like this

C#
public void ReturnIP(string domainName)
{
         String domainName;
         String status;
}


I need to return these two string values, The method that I used is to concatenate these two string and return using split function get the two values.

Can any one give any other answers?
Is this the correct way?
Posted
Updated 20-Apr-11 7:36am
v2

Your method name doesn't accurately depict what the method does. You're actually getting the status of the specified domain. Further, you're passing in the domainName, so you don't have to return it (the calling function already knows what it is, so that means you only need to return the status. Therefore, you want to do this:

C#
public string ReturnIP(string domainName)
{
    string status;
    return status;
}


Now, this is probably not what you meant, nor what you want, but you have failed to communicate precisely enough to allow us to provide you with any real help. Lesson learned?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Apr-11 18:54pm    
Good point, my 5.
--SA
You can do either of these:

1) Return back values using ref parameters

2) Return a struct containing the multiple return values

If you have .NET 4.0, then you have this option too:

3) Return a Tuple<>

Do not do either of these:

1) Return a concatenated string

2) Return an array

Both of these are not scalable and prone to error.
 
Share this answer
 
v2
Comments
thatraja 20-Apr-11 13:37pm    
Good bunch Nish. 5!
Nish Nishant 20-Apr-11 13:40pm    
Thanks.
Sergey Alexandrovich Kryukov 20-Apr-11 18:52pm    
Not ref, out (if one really needs return semantics).
2)... 2a) same thing with class instead of structure. My 4 this time.
--SA
There are at least two other ways you could return a pair of strings.

1. Return one (or both) of them as an out parameter. Here, I'm returning both.
public void ReturnIp(string domainName, out string domainIP, out string status)
    {
    ....
    }


2. You could define a struct or class to carry the pair of strings as a single value, and return that an instance of that type (note that I'm including the domain name in the class, so that the name and IP address are always closely associated):
class DomainInfo
    {
    public string DomainName { get; set; }
    public string DomainIp { get; set; }
    public string Status { get; set; }
    }

...

public DomainInfo ReturnIp(string domainName)
    {
    ...
    return new DomainInfo { DomainName = domainName, DomainIp = ..., Status = ... };
    }


Your method of concatenating the two strings certainly works, but would be considered by many programmers to be bad form as it combines two distinct values into a single entity.

Please note - unless the status value you want to return is truly a text value, you might find the use of an enum to encode the set of possible status values is clearer (and will be definitely more reliable since it doesn't rely on text parsing / comparison to interpret).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Apr-11 18:53pm    
One of the approaches, others are in the answer by Nishants + 1 of my comments (your covered another my comment).
--SA
As the others have said, out or if you need to pass a value in that the method may change ref parameters can work - this is the way methods such as int.TryParse do it.

Create a custom class that exposes the properties required and return an instance of that.

If it is multiples of the same data type, string in this case, that you need to return then you could return an array, a List<string>, ReadOnlyCollection<string> or even IEnumerable<string> etc.

A concantenated string that requires splitting later is normally a bad idea. The only time I would do anything even similar to that is if I were creating an XML file as a string, a CSV file or a line deliminated text file.
 
Share this answer
 
v2
Comments
DaveyM69 21-Apr-11 5:20am    
So why the downvote?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900