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.

7
  • 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. Commented Jun 10, 2014 at 1: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. Commented Jun 10, 2014 at 1:09
  • You cannot return multiple things from a function. You will have to either create a struct or a class and return that. Commented Jun 10, 2014 at 1:10
  • Run this code through code analysis with minimum ruleset for a detailed answer. Commented Jun 10, 2014 at 1:20
  • @Ehsan, don't forget out parameters! Commented Jun 10, 2014 at 6:28

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Good answer, but I recommend throwing an exception rather than returning a valid object the called has to inspect.
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!
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.