Resolved two errors (name does not exist )

clemtuf

Member
Joined
Jan 7, 2023
Messages
5
Programming Experience
3-5
Hi,

This code gives two errors (bold lines). For the first error, i could add this: Stm myObj2 = new Stm()]; or change the line into myObj[0][0].show(); but i don't find it very elegant and it doesn't work for the second error (in the method show).
Thanks
R.
C#:
using System;
class Stm
{
    string name;

    static void Main(string[] args)
    {
        Stm[] myObj = new Stm[3];
        for (int i = 0; i < myObj.Length; i++)
            myObj = new Stm();

        myObj[0].name = "Macron";
        myObj[1].name = "Biden";
        myObj[2].name = "Sunak";

        myObj[0].show();
    }

    void show()
    {
        for (int i = 0; i < myObj.Length; i++)
            Console.WriteLine(name);
    }
}
 
Last edited by a moderator:
In future, please tell us what the error messages are. That tells you what's wrong so you know what you're looking for. I honestly don't see how line 10 in (the now properly formatted) code is compiling either. You have declared myObj to be type Stm[] but then you assign a Stm object to it. That's like putting an egg where an egg carton is expected. Your loop is presumably supposed to be populating the array by assigning an object to each element, but you're assigning to an element in that loop. This:
C#:
myObj = new Stm();
should presumably be this:
C#:
myObj[i] = new Stm();
This is an example of why you need to go back through your code and ask yourself what each line should be doing and what it actually is doing. That won't always work, because we often see what we think is there rather than what is actually there, but you still need to do it.
 
As for the error on line 21, myObj was declared as a local variable on line 8. It is not within the scope of the show() method.
 
Thank you for replying.
First, the error message is mentionned in the question (name does not exist ..)
Then, you are right, i must of course be myObj = new Stmannen(); at line 10. My fault.
But line 10 gives now error: Stm[] does not contain a definition for 'show'...) .

About line 21: what could be the solution?
Thanks
 
How you might fix yours, and then.. how you might be better arranging the structure:


C#:
//yours fixed
using System;
public class Stm
{
    public string Name {get;set;}         //do not use public fields; use public properties. Public Things Start With An Uppercase Letter

    static void Main(string[] args)
    {
        var leaders = new Stm[]{  
            new() { Name = "Macron"},
            new() { Name = "Biden"},
            new() { Name = "Sunak"}
        };                                //use a better name than "myObj". This is how to init an array, and how to init an object (inside or notinside an array)

        foreach(var leader in leaders)    //leaders array only available in this method
            leader.Show();                //SHow is a method of an element within the array
    }

    public void Show()
    {
        Console.WriteLine(Name);          //leaders array not available here. If you want it to be, structure your program like the massive comment block below
    }
}


//how i might do it: chiefly, don't try and load everything inside Stm. Put Main outside. Have it do init of a class that holds multiple Stm. Have Stm be focused on holding Stm data (whatever an Stm is)
/*
public class Program
{
    static void Main(string[] args)       //avoid confusion; don't put the Main method inside Stm
    {
     
        var init = new Stm[]{
            new() { Name = "Macron"},
            new() { Name = "Biden"},
            new() { Name = "Sunak"}
        };   
     
        var stmc = new StmCollection(init);
     
        stmc.ShowAll();
    }
}

public class StmCollection{               //this class' purpose is just to hold multiple Stm...

    private static Stm[] _leaders;        //class level private variables have names starting with _

    public StmCollection(Stm[] initial){
      _leaders = initial;
    }
 
    public void ShowAll(){                //...and to have a routine that shows them
        foreach(var leader in leaders)
            Console.WriteLine(leader.Name);
    }
}

public class Stm
{
    public string Name {get;set;}
}
*/
 
Back
Top Bottom