0

I'm trying to return an object in C#. In JavaScript, I would do this:

function myFunction () {
 var myObj = { firstName: "John", lastName: "Smith", age: 20};
return myObj;
}

Everything I'm reading about returning an object within C# is much different than this so it's throwing me for a loop.

What I want to do is run a query to SQL to get some user info and return the users Role, Full Name, Email, etc...

Here is my current C# Code:

    public static string getUserRole(string connectionString, string userId)
    {
        string role;
        SqlConnection sqlCon = new SqlConnection(connectionString);
        SqlCommand sqlCom = new SqlCommand();
        SqlDataReader reader;

        sqlCom.CommandText = "SELECT Role FROM myDatabase.Table WHERE Email = '" + userId + "'";
        sqlCom.CommandType = CommandType.Text;
        sqlCom.Connection = sqlCon;
        sqlCon.Open();
        reader = sqlCom.ExecuteReader();

        if (reader.Read())
        {
            role = reader.GetString(0);
            sqlCon.Close();
            return role;
        }
        else
        {
            return "An error has occurred";
        }
    }

I'm assuming I need to do something like the following but it doesn't work:

    public static string getUserRole(string connectionString, string userId)
    {
        string role;
        string fullName;
        string email;
        SqlConnection sqlCon = new SqlConnection(connectionString);
        SqlCommand sqlCom = new SqlCommand();
        SqlDataReader reader;

        sqlCom.CommandText = "SELECT Role FROM myDatabase.Table WHERE Email = '" + userId + "'";
        sqlCom.CommandType = CommandType.Text;
        sqlCom.Connection = sqlCon;
        sqlCon.Open();
        reader = sqlCom.ExecuteReader();

        if (reader.Read())
        {
            public class myObject
            {
                role = reader.GetString(0);
                fullName = reader.GetString(1);
                email = reader.GetString(2);
            }
            sqlCon.Close();
            return myObject;
        }
        else
        {
            return "An error has occurred";
        }
    }

I'm probably way off but from what I'm reading, object within c# are basically classes. So that makes sense. I need to create a class that defines my properties. Is this correct? I've ready lots of posts and watched some youtube videos but none are 'clicking'

Thanks in advance for any helpful input.

mwilson
  • 12,295
  • 7
  • 55
  • 95
  • 4
    Sorry, you're doing it wrong. C# is strongly typed, not like JavaScript. You need to declare class earlier, then create instance of that class using `new` syntax. You should start with C# tutorials imo. – MarcinJuraszek Jun 10 '14 at 01:09
  • C# is statically typed. You're attempting to return two different types from your function. An object and a string. You should be getting all sorts of errors with this code since the object part is also very wrong. – Simon Whitehead Jun 10 '14 at 01:09
  • You cannot return multiple things from a function. You will have to either create a struct or a class and return that. – Ehsan Jun 10 '14 at 01:10
  • Run this code through code analysis with minimum ruleset for a detailed answer. – Mike Cheel Jun 10 '14 at 01:20
  • @Ehsan, don't forget [`out`](http://msdn.microsoft.com/library/ee332485.aspx) parameters! – Simon MᶜKenzie Jun 10 '14 at 06:28
  • @SimonMᶜKenzie not a recommended approach. just an example http://stackoverflow.com/questions/810797/which-is-better-return-value-or-out-parameter – Ehsan Jun 10 '14 at 11:04
  • @Ehsan, I was commenting on your statement that "you cannot return multiple things from a function"... – Simon MᶜKenzie Jun 10 '14 at 20:39

1 Answers1

4
public class UserInfo
{
    public string role;
        public string fullName;
        public string email;
    public string ErrorCode;
}

and then change signatures to

public static UserInfo getUserRole(string connectionString, string userId)

and then change

if (reader.Read())
        {
            public class myObject
            {
                role = reader.GetString(0);
                fullName = reader.GetString(1);
                email = reader.GetString(2);
            }
            sqlCon.Close();
            return myObject;
        }
        else
        {
            return "An error has occurred";
        }

to create an object of UserInfo and return that. Like,

UserInfo info = new UserInfo();
if (reader.Read())
        {

                info.role = reader.GetString(0);
                info.fullName = reader.GetString(1);
                info.email = reader.GetString(2);

            sqlCon.Close();

        }
        else
        {
            info.ErrorCode =  "An error has occurred";
        }
  return info;

Note: Not the best way to do it, but should get you going. Just to give you an idea.

Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • 1
    Good answer, but I recommend throwing an exception rather than returning a valid object the called has to inspect. – Dour High Arch Jun 10 '14 at 01:17
  • yea, the error will become a try/catch statement to make sure I get the actual error. just a place holder for now. Thank you! – mwilson Jun 10 '14 at 01:18
  • @DourHighArch. Totally agreed. There are many other such things that should not be done. But, the purpose is to just show him the path here. That is why i have written the note at the bottom. – Ehsan Jun 10 '14 at 01:18