|
Hi All,
I am having the error message "Inheritance.TeamManagement.salary is inaccessible due to its protection level" when I build my program. I have done some research into this problem and some of the articles I was reading say that the problem could be caused by the constructor having been declared as private instead of public. I did not declare my constructor as private so I really don't know what to do now. The following is a snippet of code from my program. Please take a look and help point out what I am doing wrong.
using System;
using System.Collections.Generic;
using System.Text;
namespace Inheritance
{
public class TeamManagement
{
private string name;
private double salary;
public TeamManagement(string nameVal, double salaryVal)
{
name = nameVal;
salary = salaryVal;
}
public string ToString()
{
return "TeamManagement[name=" + name + ",salary=" + salary + "]";
}
}
}
namespace Inheritance
{
public class Executive:TeamManagement
{
private string role;
public Executive(string nameVal, double salary, string roleVal):base( nameVal, salaryVal)
{
role = roleVal;
}
public
string ToString()
{
return "Executive[name=" + name + ",salary=" + salary + "]";
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Inheritance
{
public class Owner:Executive
{
public Owner(string nameVal, double salary, string roleVal):base( nameVal, salaryVal,roleVal)
{
}
public
string ToString()
{
return "Owner[name=" + name + ",salary=" + salary + "]";
}
}
}modified on Friday, February 12, 2010 11:06 PM
|
|
|
|
|
ASPnoob wrote: private double salary;
private fields can only be accessed in the class where they are defined.Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
Help humanity, join the CodeProject grid computing team here
|
|
|
|
|
Hi thanks for replying,
However your reply is helping as that is not the cause of my problem. I have stated that according to what I have read relating to this problem, the cause of the error message I am receiving is due to declaring a constructor as private. That is not how I declared my constructors, all of them is public as you can see.
|
|
|
|
|
ASPnoob wrote: not the cause of my problem
No that is not the cause of your problem. Your error stated: "TeamManagement.salary is inaccessible due to its protection level" which means that the field "salary" is inaccessible due to its access level. Has nothing to do with your constructor declaration. That is just a cause.Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
Help humanity, join the CodeProject grid computing team here
|
|
|
|
|
The fields name and salary are private which is as it should be. If you want these to be accessible to inheriting classes then you have a couple of choices.
1 Change the fields access modifiers - this is not recommended however! You could use (from least bad to worst)
protected - they will only be visible to TeamManagement and classes derrived from it.
internal - they will be visible to all classes in the assembly.
public - they will be visible to everyone.
2. Expose them via a property. I've rewritten your code below that way, and made a couple of other changes to make it more usable.
namespace Inheritance
{
public enum TeamManagementType
{
TeamManagement = 0,
Executive = 1,
Owner = 2,
}
public class TeamManagement
{
private string name;
private double salary;
private TeamManagementType teamManagementType;
public TeamManagement(string name, double salary)
: this(name, salary, TeamManagementType.TeamManagement)
{ }
internal TeamManagement(string name, double salary, TeamManagementType teamManagementType)
{
this.name = name;
this.salary = salary;
this.teamManagementType = teamManagementType;
}
public string Name
{
get { return name; }
}
public double Salary
{
get { return salary; }
}
public override string ToString()
{
return string.Format("{0}[{1}, {2}]", teamManagementType, name, salary);
}
}
public class Executive : TeamManagement
{
private string role;
public Executive(string name, double salary, string role)
: this(name, salary, role, TeamManagementType.Executive)
{ }
internal Executive(string name, double salary, string role, TeamManagementType teamManagementType)
: base(name, salary, teamManagementType)
{
this.role = role;
}
public string Role
{
get { return role; }
}
}
public class Owner : Executive
{
public Owner(string name, double salary, string role)
: base(name, salary, role, TeamManagementType.Owner)
{ }
}
} Notice by using the changes I have amde, the base ToString method hasn't needed to be rewritten in the derived classes so the properties haven't actually been needed as yet, but I've included them as no doubt they will be later.
|
|
|
|
|
I'm not going to read unformatted code, you should use PRE tags when publishing some code.
My suggestion is you rely on line nmbers to discover which line is not acceptable. Exceptions, compiler errors, and IDE editors all are capable of providing line number information.
My guess is you have a private field that needs to be protected so a derived class can access it.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
Hi
Anyone know how to call a .aspx web-form from Microsoft CRM 4?
When I resolve a Case in MS CRM 4 I want to run a .aspx web-form. Anyone knows if this can be done in a workflow?
Regards,
Henrik
|
|
|
|
|
It's said that information stored in ttf files are strokes in bezier format.
How to extract such information? i.e., what's the details in a ttf file?
Thanks!
|
|
|
|
|
Reading the documentation[^] usually helps..
Be careful though, they use "long" for "int32", which these days is really an odd thing to do IMO
Some information about what to do with the information in the ttf file is here: digitizing letterform designs[^]
|
|
|
|
|
Hi
I am creating a windows service which is using timers. I know I shouldn't use something like DispatcherTimer or Forms.Timer. The problem with other timers(like System.Timers.Timer or System.Threading.Timer) is that its events are raised in background threads. I tried to use the System.Threading.Timer and than use Dispatcher.CurrentDispatcher to marshal the event to the thread that created the timer but this did not work ok. I am asking the following:
Can I use Dispatcher.CurrentDispatcher and Dispatcher.Invoke in windows service app. ?
If not, can somebody direct me in the right way to solve my problem?
Any advice will be appreciated,
Uros
|
|
|
|
|
I use System.Timers.Timer for my Windows Services and I have no trouble. What problem are you having?
|
|
|
|
|
The problem I am having is that the thread that created the timer and started it, is no longer available when the timer's interval elapses. When I created an instance of the CustomTimer - which is just a wrapper around System.Threading.Timer - I save the Dispatcher.CurrentDispatcher in the private field of the CustomTimer. Than I start the timer end wait for its event to get raised(still inside the CustomTimer) and then use the saved Dispatcher to marshal to the thread that created the timer. But in some casses the Thread property of the stored Dispatcher has the state of stoped or WaitSleepJoin. But I am not sure what is the reason for that - maybe one should not use dispatcher in the windows service? I can put the code here if I was not clear.
Thanks for your time,
Uros
|
|
|
|
|
But what's the problem? Why do you care? What are you trying to accomplish? Why would your code care what thread is being used? The only time I've cared about the thread is in WinForms applications, but a Windows Service has no GUI so I don't see what the problem is.
|
|
|
|
|
I am just trying to avoid locking all the resources all over my app., which would be necessary if there would be multiple threads accesing them.
Uros
|
|
|
|
|
But even with a System.Timers.Timer you still really only have one thread active at a time... unless your threads run longer than the timer interval and you wind up with overlapping threads -- but that doesn't really seem like a desireable state so I avoid it.
If you do desire overlapping threads, then I can see where you could have trouble and I have no answers for you. Good luck.
|
|
|
|
|
Have a look at the AsyncOperation and AsyncOperationManager classes. The former has a Post and PostOperationCompleted methods that will call a method on the required thread via the SendOrPostCallback delegate, you could raise your event in that method.
|
|
|
|
|
Thanks for your suggestions. I will try to do some more researh and see if it helps.
Uros
|
|
|
|
|
I was working on the tips offered above, and come across something I do not understand. If I use a System.Threading.Timer in windows service app, and inside it's event handler check the current thread with Thread.ManagedThreadId I get the same value if I check the current thread in the OnStart() method. If I do the same in a Windows forms or WPF app. I get different values. Does anybody knows what is going on?
Thanks in advance,
Uros
|
|
|
|
|
Msdn says, the timer handler method does not execute on the thread that created the timer; it executes on a ThreadPool thread supplied by the system. And the threads in the managed thread pool are always background threads. Does this explain why you are not receiving a callback on the UI thread when tried from WinForm or WF applications?
|
|
|
|
|
Hi
I know that timer tick event should execute on the thread from the thread pool. That is expected behavior and that does not bother me. The problem is that this is only true when used in desktop(WPF or Forms) app. If the timer is used in windows service app. it executes on the thread that created the timer instance (at least Thread.CurrentThread.ManagedThreadId implies so and that is what I do not understand.
Thank you for your answer,
Uros
|
|
|
|
|
Hello, currently I am learning to create custom controls. And now here are a few question. A book sugested to use Cache and to use WeakReference class.
When I think of holding properties, I do not know if I should chose a class or a struct. For example: Button. I want to cache to remember all appearance properties. Is it better to use Class or a struct.
Does a class and struct compare by reference?
Is it faster if I call gdi directly instead GDI+?
Is there a better drawing calls instead of GDI+ for a windows form controls?
|
|
|
|
|
Saksida Bojan wrote: When I think of holding properties, I do not know if I should chose a class or a struct. For example: Button. I want to cache to remember all appearance properties. Is it better to use Class or a struct.
Well, it depends. First on how many appearance properties a button has. Second how many buttons and controls
you have. With struct(value) you get a faster access to the properties but you could run in a
StackOverflowException given that the stack memory is relatively small. So it's your decision, cause you know how many properties to hold and how many controls you'll create.
Saksida Bojan wrote: Is it faster if I call gdi directly instead GDI+?
may be faster is some cases but it is more cumbersome.
Saksida Bojan wrote: Is there a better drawing calls instead of GDI+ for a windows form controls?
Not that I know of. Unless you're willing to dive into DX and treat your client area a a DX surface.
|
|
|
|
|
I would choose for a class, because a struct only holds the data of the button and you will have actions too. Besides a class allocates memory for you. Realize that a window quikly has dozens of controls on it, and if for example you want to create a listview you may design the cells as buttons.
A struct would be faster i guess but on the other hand classes are faster as you would expect. Your language has a huge support for classes while with structures you have to do everything yourself.
GDI is faster then GDI+ and is to my opinion not bad at all. GDI+ has some features you can't mimic in GDI (anti-aliassing for example).
|
|
|
|
|
Saksida Bojan wrote: should chose a class or a struct
This is a long running debate and the general consensus seems to be...
Structs should be used for things that hold a specific value that cannot be changed. If it needs to be changed then a new instance is required (immutable). For example, Size . If either the width or height changes then a new instance is assigned to the variable as it's now a different size!
An object such as a Form would always be a class as changing a property would not create a new form.
A class that holds other class/struct instances is normally the best way to go and far easier to deal with.
There are other issues such as recommended size of structs etc. If you do seriously consider using a struct I would do some serious research first and learn about the stack/heap etc...
Saksida Bojan wrote: Does a class and struct compare by reference
Do you mean pass by reference? Structs(being values) are passed by value and classes are passed by reference, although you can use references with structs by using ref / out .
|
|
|
|
|
Hmm, it seems with this i woud prefer to use class
DaveyM69 wrote: Do you mean pass by reference? Structs(being values) are passed by value and classes are passed by reference, although you can use references with structs by using ref / out.
I meant like this:
struct test
{
public Int32 x;
public Int32 y;
}
test t1 = new test();
test t2 = new test();
t1.x = 30;
t2.x = 30;
t1.y = 45;
t2.y = 45;
if (t1 == t2)
DaveyM69 wrote: There are other issues such as recommended size of structs etc. If you do seriously consider using a struct I would do some serious research first and learn about the stack/heap etc...
Do you know where can i find good reading material for this?modified on Saturday, February 13, 2010 3:26 AM
|
|
|
|