0

I have written a separate class that holds the permissions of my website. I make calls to this class in every class I have. I am having trouble bringing back the values from my permissions class. I will show you what I have below:

Permissions Class

public class Permissions
{
     public static string selectedNumber = "";
     public static string selectedName= "";
     public static string selectedLocation= "";

     public Info SetInfo(string selectedValue)
    {           
        string selectInfo = "SELECT [Num] AS 'Number', [Name] AS 'Name', CONVERT(nvarchar(50),RTRIM(b.Num])) + ' - ' + CONVERT(nvarchar(50),b.Name) AS 'Location' "
            + "FROM [TBL_Info] a "
            + "left join [TBL_Where] b on (a.[ID] = b.[ID]) "
            + "WHERE a.ID = @ID";

        sqlCmd = new SqlCommand(selectInfo, sqlConn);
        sqlConn.Open();

        sqlCmd.Parameters.AddWithValue("@ID", selectedValue);

        SqlDataReader rdrInfo = sqlCmd.ExecuteReader();

        if (rdrInfo.HasRows)
        {
            rdrInfo.Read();

            selectedNumber = rdrInfo .GetSqlString(rdrInfo .GetOrdinal("Number")).ToString();
            selectedName= rdrInfo .GetSqlString(rdrInfo .GetOrdinal("Name")).ToString();
            selectedLocation = rdrInfo .GetSqlString(rdrInfo .GetOrdinal("Location")).ToString();
        }
        sqlCmd.Connection.Close();

        return new Info()
        {
           number= selectedNumber, 
           name= selectedName,
           location= selectedLocation

        };
    }

    public class Info
    {
        public String number{ get; set; }
        public String name{ get; set; }
        public String location{ get; set; }
    }
}

And I am currently trying to call it in another class like this:

Classes.Permissions permission = new Classes.Permissions();
permission.SetInfo(selectedUserValue);

The end product is that I set textboxes in the class I am trying to make the call from with the 3 return values from permission.SetInfo()

Currently I am not able to get anything returned.... I know I am doing something wrong, clearly. So can someone please give me some advice on how to achieve this?

Code
  • 1,969
  • 3
  • 18
  • 33
  • 1
    You are not saving object which you are returning `var info = permission.SetInfo(selectedUserValue);` After that you can use `info` variable – Sergey Berezovskiy Oct 19 '15 at 23:56
  • Right, I see that... but how will I return the 3 objects? In a list? – Code Oct 19 '15 at 23:59
  • You should open the sql connection in a `using` block so it always gets closed. – Philip Pittle Oct 20 '15 at 00:01
  • 2
    If you want to return several `Info` objects, then collection (list/array etc) is an option. If each of these objects have separate meaning then consider to create class with three properties of type `Info` with appropriate names, and return that object – Sergey Berezovskiy Oct 20 '15 at 00:01
  • 2
    So lets get this right: your function that GETs some info is actually called `SetInfo`? Do you see what is wrong with that? – JK. Oct 20 '15 at 00:16
  • LOL. Lets get this right, your username is JK. So I am assuming you are kidding – Code Oct 20 '15 at 00:19

1 Answers1

2

Other than some stylistic things I have issue with, your code looks like it should work (as long as you have data in your database). To return multiple values from a method, create a new object (ie Info) that defines the things to be returned.

So, you should be able to write something like:

Textbox txtBox;

var info = new Classes.Permissions().SetInfo(SelectedUserValue);

txtBox.Text = 
       info.number ?? "<null>" + " " + 
       info.name ?? "<null> + " " + 
       info.location ?? "<null>'

Note that I used the null coalescing operator (??) as SetInfo could return an instance of Info, where all members are null if no rows are returned from your database query.

By the way, the other way to return multiple values from a method is to use out parameters:

Textbox txtBox;
string number;
string name;
string location;

new Classes.Permissions().SetInfo(SelectedUserValue, out number, out name, out location);

And then your SetInfo would look like:

public void SetInfo(string SeelctedUserValue, 
     out string number, out string name, out string location)
{
    //assign values to number,name and location
}

To return multiple instances of the same object, than your method should return

  1. an IEnumerable (ie a List or Array),
  2. a Tuple<>,
  3. a container object.

For example, if you know you are going to return exactly three, you might want to have your method return Tuple<Info,Info,Info>:

public Tuple<Info, Info, Info> SetInfo(string SeelctedUserValue)
{
   //query db
   return new Tuple<Info, Info, Info>(
     new Info{ number = "number", name = "name", location="location},
     new Info{ number = "number", name = "name", location="location},
     new Info{ number = "number", name = "name", location="location});         
}
Philip Pittle
  • 11,821
  • 8
  • 59
  • 123