מבנה נתונים ב- c#

יטרנר

New member
מבנה נתונים ב- c#

ניסיתי להשתמש בפונקציה כדי להדפיס את המיד בstruct ולא הצלחתי. איך עושים זאת? אשמח לטיפים
sing System; using System.Collections.Generic; using System.Text; namespace @struct.personaldetails { struct address { public string street, town; public int no; } struct person { public string lname, fname; public address adr; } class Program { static void Main(string[] args) { address a; a.street = "professor shor"; a.town = "Holon"; a.no = 6; person p; p.lname = "Levy"; p.fname = "Ifat"; p.adr = a; PrintDetails (p); } public void PrintDetails (person p) { Console.WriteLine("person details:\n {0}\n {1}\n {2}\n {3}\n {4}\n", p.lname, p.fname, p.adr.street, p.adr.town, p.adr.no); } } }​
 

blue25

New member
מה לא הצלחת ?

בכל מקרה PrintDetails צריכה להיות static.
 

blue25

New member
OK

קודם כל אם יש לך שגיאת קומפילציה אז תראה אותה, כדי שאנשים יוכלו לעזור לך. דבר שני, רשמתי לך הפונקציה PrintDetails צריכה להיות static.
 

יטרנר

New member
תודה על התגובה

תיקנתי והמשכתי את התרגיל בניסיון לשמור את הנתונים של המבנה, טעיתי בהגדרת משתנה text, ואין לי מושג איך לתקן זאת. אשמח לקבל הארה. תודה רבה ..
using System; using System.Collections.Generic; using System.Text; using System.IO; namespace @struct.personaldetails { struct address { public string street, town; public int no; } struct person { public string lname, fname; public address adr; } class Program { static void Main(string[] args) { address a; a.street = "Brosh"; a.town = "Holon"; a.no = 20; person p; p.lname = "Levy"; p.fname = "Ifat"; p.adr = a; PrintDetails(p); SaveDetails(p);//saving the text in a file } static void PrintDetails(person p) { Console.WriteLine("person details:\n {0}\n {1}\n {2}\n {3}\n {4}\n", p.lname, p.fname, p.adr.street, p.adr.town, p.adr.no); } static string SaveDetails(person p) { Console.WriteLine("enter filename"); string filename = Console.ReadLine(); string text= (string)person p; string writing=writetxt (namefile,text); Console.WriteLine("the saved content:{0}",writing); } static string writing(string filename, string text) { StreamWriter sw = new StreamWriter(filename); sw.WriteLine(text); sw.Close (); return text; } } }​
 

יטרנר

New member
תודה על התגובה

תיקנתי והמשכתי את התרגיל בניסיון לשמור את הנתונים של המבנה, טעיתי בהגדרת משתנה text, ואין לי מושג איך לתקן זאת. אשמח לקבל הארה. תודה רבה ..
using System; using System.Collections.Generic; using System.Text; using System.IO; namespace @struct.personaldetails { struct address { public string street, town; public int no; } struct person { public string lname, fname; public address adr; } class Program { static void Main(string[] args) { address a; a.street = "Brosh"; a.town = "Holon"; a.no = 20; person p; p.lname = "Levy"; p.fname = "Ifat"; p.adr = a; PrintDetails(p); SaveDetails(p);//saving the text in a file } static void PrintDetails(person p) { Console.WriteLine("person details:\n {0}\n {1}\n {2}\n {3}\n {4}\n", p.lname, p.fname, p.adr.street, p.adr.town, p.adr.no); } static string SaveDetails(person p) { Console.WriteLine("enter filename"); string filename = Console.ReadLine(); string text= (string)person p; string writing=writetxt (namefile,text); Console.WriteLine("the saved content:{0}",writing); } static string writing(string filename, string text) { StreamWriter sw = new StreamWriter(filename); sw.WriteLine(text); sw.Close (); return text; } } }​
 
למעלה