Hi,
with this code, i get nothing. It's because the array in class View is empty. Yet, the method Show() starts after the array is filled in Main. How can i show those data in method Show()?
Thanks
V.
using System;
class Stman
{
public string[] Name = new string[3];
static void Main(string[] args)
{
Stman myObj = new Stman();
View dat = new View();
myObj.Name[0] = "Macron";
myObj.Name[1] = "Rutte";
myObj.Name[2] = "Sunak";
dat.Show();
}
}
class View
{
public void Show()
{
Stman myObj = new Stman();
for (int i = 0; i < myObj.Name.Length; i++)
Console.WriteLine(myObj.Name[i]);
}
}

Aravind GovindarajRank #430 · 5.8K reputationPosted Dec 9, 2022, 1:52 PM EST
please follow this
using System; class Stman { static void Main(string[] args) { //Stman myObj = new Stman(); View dat = new View(); myObj.Name[0] = "Macron"; myObj.Name[1] = "Rutte"; myObj.Name[2] = "Sunak"; dat.Show(); } } class View { public string[] Name = new string[3]; public void Show() { //Stman myObj = new Stman(); for (int i = 0; i < Name.Length; i++) Console.WriteLine(myObj.Name[i]); } }Pankajkumar PatelRank #181 · 13.4K reputationPosted Dec 12, 2022, 1:47 AM EST
Hi Valerie Meunier,
To show data in another class you can do it by passign class object to that method for this implementation as like following:
using System; public class Stman { public string[] Name = new string[3]; public static void Main() { Stman myObj = new Stman(); View dat = new View(); myObj.Name[0] = "Macron"; myObj.Name[1] = "Rutte"; myObj.Name[2] = "Sunak"; dat.Show(myObj); } } public class View { public void Show(Stman myObj) { for (int i = 0; i < myObj.Name.Length; i++) Console.WriteLine(myObj.Name[i]); } }Hope this will help you!
Valerie MeunierRank #1,526 · 1K reputationPosted Dec 10, 2022, 4:13 AM EST
Thanks for replying. There are some errors in your code. Did you test it?
It must be dat.Name[0] = "Macron";
Also the last line must be Console.WriteLine(Name[i]
But what i want to know is why mijn code doesn't work? The only explanation i found is that myObj.Name[i] in method Show not the same is as in Main.
Valerie MeunierRank #1,526 · 1K reputationPosted Dec 13, 2022, 10:56 AM EST
Thanks.