Click here to Skip to main content
15,906,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
in main i tried to initialze constructor but i could not

recDB.insert(new Record(22, "Mike", 2500));//------this is my error


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

namespace ClassLibrary1
{
    public class Class1
    {
        public class Record
        {
            public int ssn; // Primary Key
            public String name;
            public double pay;
            Record(int newSSN, String newName, double newPay)
            {
                ssn = newSSN;
                name = newName;
                pay = newPay;
            }
        }
        //------------------------------
        public class RecordDB
        {
            private static int maxRecords = 200;
            private int numRecords = 0;
            private Record[] recordList = new Record[maxRecords];
            //-------------------------------------------
            public void insert(Record rec)
            {
                if (numRecords == maxRecords - 1)
                {
                    return;
                }
                printAll();
            }
            //-------------------------------------------
            public void delete(int newSSN)
            {
                printAll();
            }
            //-------------------------------------------
            //public Record search(int newSSN)
            public void search(int newSSN)
            {
                int first = 0, last = numRecords - 1;
                // Print All records
                printAll();

                //Record recx = new Record(30, "JohnX", 5000);
                //return recx;

            } // end of search function
            //------------------------------------------
            private void printAll() {
                Console.Write("---------------------------------");
                for (int i = 0; i < numRecords; i++)
                {
                    Console.Write("" + i + " " + recordList[i].ssn +
                    "  " + recordList[i].name + " " + recordList[i].pay);
                }
            }
            //------------------------------------------
        public static void main(String[] args)
        {
        RecordDB recDB = new RecordDB();

        recDB.insert(new Record(22, "Mike", 2500));//<-----------------Error

        }

        }
    }
}
Posted
Updated 17-Mar-11 12:53pm
v4
Comments
furyous 17-Mar-11 16:22pm    
I may be wrong with this but for one 2500 is not a double value? if you wanted 2500 to be stored in a double value as a int like that you would need to do recDB.insert(new Record(22, "Mike", 2500d)); I could be completely wrong on this
fjdiewornncalwe 17-Mar-11 18:56pm    
Just to clarify the answers below by CPallini and TheCallMeMrJames.
The reason for the "public" keyword before your constructor is that without it the constructor is considered private by default and therefore inaccessible to any classes outside this one.

Change your constructor to this:

C#
public Record(int newSSN, String newName, double newPay)


You also might want to consider moving those nested classes out.

Cheers.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Mar-11 22:15pm    
Doesn't have to be "public", better be "internal" (nothing says it should be used in other assembly). There is nothing wrong about nested classes. My 4.
--SA
TheyCallMeMrJames 18-Mar-11 22:47pm    
Good call on the internal, but I never said I had a beef with nested classes. In this instance (Record, RecordDB and Class1 inside namespace ClassLibrary1) there is nothing to suggest any benefit in this arrangement, thus my recommendation to reconsider the class design. Cheers.
Khalid SabtanFlaviu 2 wrote:
Record(int newSSN, String newName, double newPay)
{
ssn = newSSN;
name = newName;
pay = newPay;
}


I would try with:
C#
public Record(int newSSN, String newName, double newPay)
            {
                ssn = newSSN;
                name = newName;
                pay = newPay;
            }
 
Share this answer
 
Comments
TheyCallMeMrJames 17-Mar-11 16:26pm    
living right on the edge of submit... ;)
CPallini 17-Mar-11 16:40pm    
Or 'the poor C++ coder' cannot be faster on this... :-D
TheyCallMeMrJames 17-Mar-11 16:44pm    
:o)
Khalid Sabtan 17-Mar-11 16:50pm    
More likley i will ask more question regarding this program

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