Base class string cannot be accessed due to its protection level!

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1347 Views - Last Post: 26 November 2011 - 07:59 PM Rate Topic: -----

#1 dougyno1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 06-May 11

Base class string cannot be accessed due to its protection level!

Posted 13 October 2011 - 11:47 AM

Hey sharpies,

we had our first C# practical the other day and one of the tasks we had was to convert a java program into a c# program. I am very new to the language but am quite competent with java. The message I am getting from MS visual c# express 2010 is that the string from the base class is in accessable due to its protection level, but unable to think of why that could be, any input would be much appreciated, thanks very much. I will include all of the program classes.

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

namespace Practical_1
{
    abstract public class Fish
    {

        public String fishType;
        public static int count;
        Fish(String name)
        {
            fishType = name;
            count++;
        }
        public static int getCount()
        {
            return count;
        }
        public String getFishType()
        {
            return fishType;
        }
        abstract public void move();
    }
}









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

namespace Practical_1
{
   public class FlyingFish : Fish 
    {
       public int height;
// the constructor is where I am getting the error message 
	public FlyingFish(int h, String n)
	{
		 base(n);
		height = h;
	}
	public void move()
	{
		System.Console.WriteLine("I fly cos I can.");
	}
	public int getHeight()
	{
			return height;
	}
}


    }





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

namespace Practical_1
{
    public class Shark : Fish
    {
        public int noOfKills;
// the constructor is where I am getting the error message
        public Shark(int kill, String n)
        {
            base(n);
            noOfKills = kill;
        }
        public void move()
        {
            System.Console.WriteLine("I swim cos I can.");
        }
        public void getKill()
        {
            System.Console.WriteLine("I kill cos I can." + noOfKills);
        }

    }
}






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

namespace Practical_1
{

    public class Sea
    {
        public static void main(String[] args)
        {
            FlyingFish percy = new FlyingFish(1000, "A Flying Fish");
            Shark jaws = new Shark(1100, "Jaws the shark");
            System.Console.WriteLine(percy.getFishType());
            System.Console.WriteLine("My height is " + percy.getHeight());
            percy.move();
            System.Console.WriteLine(jaws.getFishType());
            jaws.move();
            jaws.getKill();
        }
    }

}




Is This A Good Question/Topic? 0
  • +

Replies To: Base class string cannot be accessed due to its protection level!

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Base class string cannot be accessed due to its protection level!

Posted 13 October 2011 - 12:00 PM

Try using this syntax -

    public FlyingFish(int h, string n)
        : base(n)
    {
        height = h;
    }


There's also more info here in the second example particularly.

This post has been edited by Ryano121: 13 October 2011 - 12:00 PM

Was This Post Helpful? 1
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Base class string cannot be accessed due to its protection level!

Posted 13 October 2011 - 12:03 PM

Ryano is correct. You can't call a constructor like that. This syntax says to call the base constructor first, then enter the child constructor.
Was This Post Helpful? 1
  • +
  • -

#4 dougyno1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 06-May 11

Re: Base class string cannot be accessed due to its protection level!

Posted 15 October 2011 - 12:37 PM

Thanks Curtis & Ryano, I did try placing a colon before 'base', it didn't solve the problem when I tried that in class but I will try again to fully make sure.
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: Base class string cannot be accessed due to its protection level!

Posted 15 October 2011 - 12:38 PM

String is a Sealed Class, which means your can't inherit from it.
Was This Post Helpful? 1
  • +
  • -

#6 dougyno1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 06-May 11

Re: Base class string cannot be accessed due to its protection level!

Posted 24 November 2011 - 07:31 AM

ok AdamSpeight2008, do you think it wwould be worth trying a lower case s in what's currently String n, so it refers to the data type ?? thanks for the input btw.
Was This Post Helpful? 0
  • +
  • -

#7 dougyno1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 06-May 11

Re: Base class string cannot be accessed due to its protection level!

Posted 24 November 2011 - 07:37 AM

actually it would be a good idea to change all data type strings with a lower case s, just had a look at my code again and saw it in more than one place ??
Was This Post Helpful? 0
  • +
  • -

#8 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 641
  • Joined: 07-October 08

Re: Base class string cannot be accessed due to its protection level!

Posted 24 November 2011 - 07:38 AM

No, string is still sealed. If you type it, and then right click it and select 'Go To Definition' you will see the the structure of the class, where it is defined as sealed.

And to answer your second question I'm 99% sure there is no difference between string and String, implementation is the same. As a preference I always use String, Int32, Byte etc, but most people seem to use the primitives string, int, byte

This post has been edited by ragingben: 24 November 2011 - 07:40 AM

Was This Post Helpful? 1
  • +
  • -

#9 dougyno1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 06-May 11

Re: Base class string cannot be accessed due to its protection level!

Posted 24 November 2011 - 09:10 AM

ok thanks ragingben, ha learning lots with this post guys.
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Base class string cannot be accessed due to its protection level!

Posted 24 November 2011 - 09:30 AM

Quote

I'm 99% sure there is no difference between string and String, implementation is the same


This is true. string is an alias for String, just like int is an alias for Int32. They both are the same class/struct. It makes no difference which you use, the same IL is generated.
Was This Post Helpful? 1
  • +
  • -

#11 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 308
  • Joined: 30-January 11

Re: Base class string cannot be accessed due to its protection level!

Posted 24 November 2011 - 10:21 AM

Since System.String is the actual class and string is a alias does string work with Refelction?
Was This Post Helpful? 1
  • +
  • -

#12 Feuer  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 57
  • Joined: 15-June 11

Re: Base class string cannot be accessed due to its protection level!

Posted 24 November 2011 - 10:37 AM

I think you have a problem in calling the base class's constructor. You haven't specified Fish()'s visibility, thus making it private and invisible to inheriting classes. This triggers errors when calling base() in child classes.
Was This Post Helpful? 1
  • +
  • -

#13 dougyno1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 06-May 11

Re: Base class string cannot be accessed due to its protection level!

Posted 25 November 2011 - 05:26 AM

Again thanks everone for your input on this topic.
Was This Post Helpful? 0
  • +
  • -

#14 dougyno1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 06-May 11

Re: Base class string cannot be accessed due to its protection level!

Posted 25 November 2011 - 06:21 AM

Just revisited my program and Ryano121 had the correct solution, so thanks a following once more to everyone and in particular Ryano, I learned a bit from this one!!!!!
Was This Post Helpful? 0
  • +
  • -

#15 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Base class string cannot be accessed due to its protection level!

Posted 25 November 2011 - 08:14 AM

Glad you got it working! :)
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2