I'm having difficulty calling a class method and making use of its return value. This code is in the main cs file:
SomeApp.Players SomeAppPlayer = new SomeApp.Players();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
*** Error here: txtLastName.Text = SomeAppPlayer.GetPlayer();
}
In another class file is this code:
// This is in another file.
namespace SomeApp
{
public class Players
{
public Player GetPlayer()
{
Player player = new Player("SMITH");
return (player);
}
}
public class Player
{
private string lastName;
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public Player(string lastName)
{
LastName = lastName;
}
}
}
I've omitted some code for simplicity, but when I step thru the code, everything seems to work ok. The problem is when I attempt to assign the return value to the text property of a textbox. This is the error I receive: Cannot implicitly convert type SomeApp.Player to type string.
I'm new to C#, and any guidance would be appreciated.
Loading
RonPosted Jan 23, 2012, 1:20 PM
Satyapriya NayakPosted Jan 23, 2012, 1:12 PM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Players SomeAppPlayer = new Players();
textBox1.Text = SomeAppPlayer.GetPlayer().LastName.ToString();
}
public class Players
{
public Player GetPlayer()
{
Player player = new Player("SMITH");
return player;
}
}
public class Player
{
private string lastName;
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public Player(string lastName)
{
LastName = lastName;
}
}
}
}
Thanks
RonPosted Jan 23, 2012, 12:51 PM
Thanks.