Answered by:
How to Use Set Get into array and display it

Question
-
How to use Setter and getter on to an array?
some how the user would enter few userinfo and save it to the array;
input:
name: a
age: 12
gender: Male
name: b
age: 15
gender: female
can input multiple records
then able to display it
- Edited by Azukachitomori Thursday, August 8, 2013 10:18 AM
Thursday, August 8, 2013 8:51 AM
Answers
-
Here is one method
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { userinfo info1 = new userinfo("a",12,"Male"); string[] getinfo1 = info1.ToList(); userinfo info2 = new userinfo("b",15,"female"); string[] getinfo2 = info2.ToList(); } class userinfo { public string name { set; get; } public int age { set; get; } public string gender { set; get; } public userinfo() { } public userinfo(string nname, int nage, string ngender) { name = nname; age = nage; gender = ngender; } public string[] ToList() { string[] results = { name, age.ToString(), gender }; return results; } } } }
jdweng
- Marked as answer by Azukachitomori Saturday, August 10, 2013 7:54 AM
Thursday, August 8, 2013 10:01 AM -
Hi Azukachitomori,
I agree with jdweng. We are not sure where you want to display it and the format is.
lay it in the console, you need to add some code in jdweng’s code.
static void Main(string[] args) { userinfo info1 = new userinfo("a", 12, "Male"); string[] getinfo1 = info1.ToList(); userinfo info2 = new userinfo("b", 15, "female"); string[] getinfo2 = info2.ToList(); //below is the add code Console.WriteLine("{0}"+" "+"{1}"+" "+"{2}",getinfo1[0],getinfo1[1],getinfo1[2]); Console.WriteLine("{0}" + " " + "{1}" + " " + "{2}", getinfo2[0], getinfo2[1], getinfo2[2]); Console.ReadLine(); }
Could you give us more info about your problem?
Thanks
Eason_H
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Azukachitomori Saturday, August 10, 2013 7:54 AM
Friday, August 9, 2013 9:36 AM -
Hi,
You can use C# class to create objects and can store these kind of values.Since going to set;get; for array is not a good practice.Good Object Orientation is to use class object only.
Example :
public class Program
{
public static void Main(string[] args)
{
cUser objUser;
List<cUser> lUsers = new List<cUser>();
objUser = new cUser();
objUser.sName = " Get Name";
objUser.iAge = 1;
objUser.sGender = " Get gender";
lUsers.Add(objUser);
}
}
public class cUser
{
public string sName = string.Empty;
public int iAge = 0;
public string sGender = string.Empty;
}You can achieve the functionality easily.
- Marked as answer by Azukachitomori Saturday, August 10, 2013 7:54 AM
Thursday, August 8, 2013 9:34 AM
All replies
-
Hi,
You can use C# class to create objects and can store these kind of values.Since going to set;get; for array is not a good practice.Good Object Orientation is to use class object only.
Example :
public class Program
{
public static void Main(string[] args)
{
cUser objUser;
List<cUser> lUsers = new List<cUser>();
objUser = new cUser();
objUser.sName = " Get Name";
objUser.iAge = 1;
objUser.sGender = " Get gender";
lUsers.Add(objUser);
}
}
public class cUser
{
public string sName = string.Empty;
public int iAge = 0;
public string sGender = string.Empty;
}You can achieve the functionality easily.
- Marked as answer by Azukachitomori Saturday, August 10, 2013 7:54 AM
Thursday, August 8, 2013 9:34 AM -
and how to display them?Thursday, August 8, 2013 9:37 AM
-
Here is one method
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { userinfo info1 = new userinfo("a",12,"Male"); string[] getinfo1 = info1.ToList(); userinfo info2 = new userinfo("b",15,"female"); string[] getinfo2 = info2.ToList(); } class userinfo { public string name { set; get; } public int age { set; get; } public string gender { set; get; } public userinfo() { } public userinfo(string nname, int nage, string ngender) { name = nname; age = nage; gender = ngender; } public string[] ToList() { string[] results = { name, age.ToString(), gender }; return results; } } } }
jdweng
- Marked as answer by Azukachitomori Saturday, August 10, 2013 7:54 AM
Thursday, August 8, 2013 10:01 AM -
and the display?Thursday, August 8, 2013 10:03 AM
-
I return an array of string. I wasn't sure where you wanted it displays and the format.
jdweng
Thursday, August 8, 2013 10:20 AM -
Hi Azukachitomori,
I agree with jdweng. We are not sure where you want to display it and the format is.
lay it in the console, you need to add some code in jdweng’s code.
static void Main(string[] args) { userinfo info1 = new userinfo("a", 12, "Male"); string[] getinfo1 = info1.ToList(); userinfo info2 = new userinfo("b", 15, "female"); string[] getinfo2 = info2.ToList(); //below is the add code Console.WriteLine("{0}"+" "+"{1}"+" "+"{2}",getinfo1[0],getinfo1[1],getinfo1[2]); Console.WriteLine("{0}" + " " + "{1}" + " " + "{2}", getinfo2[0], getinfo2[1], getinfo2[2]); Console.ReadLine(); }
Could you give us more info about your problem?
Thanks
Eason_H
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Azukachitomori Saturday, August 10, 2013 7:54 AM
Friday, August 9, 2013 9:36 AM -
i think i get the part where i set the records by my self as default but
how about not by default and multiple input such as more than 5
Saturday, August 10, 2013 7:53 AM -
SOme how like this
and implement it to C#
public static void main(String[] args) { System.out.println("Welcome to the Student Scores Application."); System.out.print("Amount of students:"); int studentCount = scan.nextInt(); students = new Student[studentCount]; for (int i = 0; i < students.length; i++) { students[i] = new Student(); System.out.print("Enter first name:"); String firstName = scan.next(); students[i].setFirstName(firstName); System.out.print("Enter last name:"); String lastName = scan.next(); students[i].setLastName(lastName); System.out.print("Enter score:"); int score = scan.nextInt(); students[i].setScore(score); }
Saturday, August 10, 2013 8:08 AM