Click here to Skip to main content
15,891,733 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello People,

I am total new to C#
I haven't understood how everything is connected to each other classes and objects etc...

I have created a a simple project.
My goal it is to have two different public classes.

myfirst class(contains and list "companyInfo")

mySecondClass (calls the companyinfo list)

from static void Main(string[]args) alert all items in the companyInfo

Could someone help me?

see my code below:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace MyProgram
{
     }
     class Program
    {
        static void Main(string[] args)
        {
            {

                CompanyMain CCinfo = new CompanyMain();         
           
            }
        }


        public class myList
        {
            static List<string> CompanyInfo()
            {
                List<string> CompanyInformation = new List<string>();
                CompanyInformation.Add("test1");
                return CompanyInformation;
            }
        }

        public class CompanyMain
        {

            myList CompanyInfo = new myList();
            
        }

     }


Thank you in advance
Posted

Sorry for my english...

First you will need to change access modifier of Company Info field, because when it isn't specified, by default take private and you can't see it out the class.

C#
public class CompanyMain
        {
 
           public myList Company<big></big>Info = new myList();
            
        }



After that, in the main you can see the CompanyInfo field and you can manipulate it.

C#
static void Main(string[] args)
       {
           {
               CompanyMain CCinfo = new CompanyMain();
           foreach(var item = CCinfo.CompanyInfo.CompanyInfo()){
                 //

             }
           }
       }
 
Share this answer
 
v2
Comments
Daniel Elmnas 19-Nov-15 10:15am    
Could someone send me a corrected code? its kind of confusing sorry..
Thank you in advance..
aramateus 20-Nov-15 8:40am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace MyProgram
{

class Program
{
static void Main(string[] args)
{


CompanyMain CCinfo = new CompanyMain();
foreach(var item = CCinfo.CompanyInfo.CompanyInfo()){

Console.WriteLine(item);
}

}
}

public class myList
{
static List<string> CompanyInfo()
{
List<string> CompanyInformation = new List<string>();
CompanyInformation.Add("test1");
return CompanyInformation;
}
}

public class CompanyMain
{

public myList CompanyInfo = new myList();

}

}
In order to cycle through all the items in a list you need to use the foreach[^] syntax.

e.g.

foreach s in CCinfo.CompanyInfo()
 'do something with the company info here...
next
 
Share this answer
 
v3
Comments
Daniel Elmnas 19-Nov-15 9:42am    
doesn't work,
I have tried

CompanyMain CCinfo = new CompanyMain();
forech (var s in CCinfo.myList)
{
Console.WriteLine(s);
}
Daniel Elmnas 19-Nov-15 9:57am    
I get these errors:
1. Foreach cannot operate on a 'method group'. Did you intend to invoke the 'method group'?
2. foreach statement cannot operate on variables of type 'method group' because 'method group' does not contain a public definition for 'GetEnumerator'
Duncan Edwards Jones 19-Nov-15 10:09am    
I missed braces - try with them..
Daniel Elmnas 19-Nov-15 10:43am    
have already tried with braces.. thank you in advance.

could you send a corrected to that works?.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900