开发者

Convert type string to type object

开发者 https://www.devze.com 2023-01-13 09:08 出处:网络
I am new to C#. I need to show a users fullname in a console application, but I keep getting this error

I am new to C#. I need to show a users fullname in a console application, but I keep getting this error

Cannot implicitly convert type 'string' to 'UserCustomerNotes.User'

My code works when I only use return user but all that it shows in the app then is "UserCustomerNotes.User" and I need to show the fullname of the user. Can an开发者_如何学Goyone please help. Here is my code:

public static User FindUser(User[] users, int noteid)
{
    foreach (User user in users)                
        if (user.ID == noteid) return user.FullName;
    return null;
}


Your method excpecting a return value of typeUser, if you want it to return name - change it to:

public static **string** FindUser(User[] users, int noteid)

or, alternatively:

if (user.ID == noteid) return user;


Your code:

public static User FindUser(User[] users, int noteid)
{       
    foreach (User user in users)                
        if (user.ID == noteid) return user.FullName;
    return null;
}

Is expecting to return a string object (the user.FullName value) when it succeeds in finding one, whereas the return type you've declared for your method is User. What you should do is this (where you're currently calling FindUser:

var ArrayOfUsersToSearch = GetAnArrayOfUserFromSomewhere();
var noteId = 3; // Or whatever value you want to use
var myUserToFind = FindUser(ArrayOfUsersToSearch, noteId);

Console.WriteLine("The users full name is: {0}", 
        myUserToFind != null ? myUserToFind.FullName : "Not Found");

And change the code in your FindUser method to be:

public static User FindUser(User[] users, int noteid)
{       
    foreach (User user in users)                
        if (user.ID == noteid) return user;
    return null;
}

The reason you see "UserCustomerNotes.User" output when you return user from your method is this:

Console.WriteLine(FindUser(users, noteId));

// instead of

Console.WriteLine(FinderUser(users, noteId).FullName);

Because the return type is UserCustomerNotes.User and it doesn't have an override of "ToString" to specify anything different, the default value that is given when Console.WriteLine asks your User object for its textual equivalent is its name. This explains why you see the "odd" output from your code currently.

Note: I've put Console.WriteLine("The users full name is: {0}", myUserToFind != null ? myUserToFind.FullName : "Not Found"); as your code can return null if it fails to find a user and you'd want to special-case the output you provide in that circumstance.

It's also worth mentioning, but unrelated to the question itself, that the format:

foreach(var x in y)
    do something;

Can be more prone to "breaking" if changes are made further down the line than if you explicitly call out the { and } around each statement block:

foreach (User user in users)                
{
    if (user.ID == noteid) 
    {
        return user.FullName;
    }
}
return null;


It is because your function is of type User. Try changing it to string.

public static string FindUser(User[] users, int noteid)
{       
    foreach (User user in users)                
        if (user.ID == noteid) return user.FullName;
    return null;
}


You may define a ToString method inside your User class:

public override string ToString()
{
    return FullName;
}

Then you can just

Console.WriteLine("User name {0}", user);
0

精彩评论

暂无评论...
验证码 换一张
取 消