Forum guidelines
AuthorQuestion
Inconsistent accessibility continue
Posted By Maha on 21 Aug 2013
http://www.c-sharpcorner.com/Forums/Thread/225889/inconsistent-accessibility.aspx

I wish to know whether it is possible to demonstrate behavioural pattern mentioned of FlavorScoop() method by an example.

Your explanation about using access modifier (14th thread of Inconsistent accessibility)
After this (single module) assembly has been compiled, it could (in theory) be added as a reference to another assembly. If code in the latter assembly wanted to call the FlavorScoop method, it would have problems in doing so because it wouldn't have access to the IceCreamCone class.

The compiler is therefore ensuring that these problems won't arise by flagging it as an error now.

using System;
public class Program
{
public static void Main()
{
IceCreamCone vanilla2 = new IceCreamCone("Vanilla", 2);
IceCreamCone chocolate1 = new IceCreamCone("Chocolate", 1);

FlavorScoop(vanilla2);
FlavorScoop(chocolate1);

Console.ReadKey();
}
public static void FlavorScoop(IceCreamCone icc)
{
Console.WriteLine(icc.GetFlavor() + " flavor" + ", " + icc.GetScoops() + " scoop");
}
}
class IceCreamCone
{
string flavor;
int scoop;

public IceCreamCone(string flavor, int scoop)
{
this.flavor = flavor;
this.scoop = scoop;
}
public string GetFlavor()
{
return flavor;
}
public int GetScoops()
{
return scoop;
}
}


AuthorReply
Re: Inconsistent accessibility continue
Posted By Vulpes on 21 Aug 2013  
Well, you can't demonstrate anything as the code stands, because it won't compile.

However, if you fix the problem by making the IceCreamCone class public, then you can create another assembly with this code:

using System;

public class Program2
{
    public static void Main()
    {
        IceCreamCone vanilla2 = new IceCreamCone("Vanilla", 2);
        IceCreamCone chocolate1 = new IceCreamCone("Chocolate", 1);

        Program.FlavorScoop(vanilla2);
        Program.FlavorScoop(chocolate1);

        Console.ReadKey();
   }
}

Add a reference to the first assembly to it so that it will compile.

Finally, when you run it, it should produce the same output as the first assembly did on its own.



Maha
  • 0
  • 0
Re: Inconsistent accessibility continue
Posted By Maha on 21 Aug 2013  
Please fix any problem and write the final program because I couldn't follow you.

Vulpes
  • 0
  • 0
accepted
Re: Inconsistent accessibility continue
Posted By Vulpes on 22 Aug 2013   Accepted Answer
In the program in your opening post, just change:

class IceCreamCone

to:

public class IceCreamCone

The program should now build and run OK.

When you've done that, create a second console application project, delete all the automatically generated code and paste in its place the code in my first post.

Right click the project name in Solution Explorer, select Add Reference and then, under the Browse tab, browse to where the .exe for the first program is stored and OK it.

Now attempt to build and run the second project. If everything is OK, it should display the same output in the console window as the first program did.

Maha
  • 0
  • 0
Re: Inconsistent accessibility continue
Posted By Maha on 22 Aug 2013  
Please tell me where you can find "automatically generated code"

Re: Inconsistent accessibility continue
Posted By Vulpes on 22 Aug 2013  
If you create a new Console Application project in VS 2010 (it's probably the same in VS 2012) a file called program.cs is automatically generated containing the following code:

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

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

All this should be deleted and replaced with the code in my first post.

Maha
  • 0
  • 0
Re: Inconsistent accessibility continue
Posted By Maha on 22 Aug 2013  
Now I got the idea. Thank you very much for your explanation.
Maha
  • 0
  • 0
Re: Inconsistent accessibility continue
Posted By Maha on 22 Aug 2013  
Even though I deleted the Program folder (my 1st post) from the system and restart the computer to make sure all traces were eliminated. That means reference to the Program2 was deleted. But Program2 is still function without it's reference. What is the reason behind this?


Re: Inconsistent accessibility continue
Posted By Vulpes on 22 Aug 2013  
What's probably happened is that Visual Studio has copied program.exe (or whatever you called it) to the same folder as program2.exe.

If you click on program.exe under References in Solution Explorer and then look at the Properties Box, if 'Copy Local' is set to True, then that's what's happened.

Maha
  • 0
  • 0
Re: Inconsistent accessibility continue
Posted By Maha on 23 Aug 2013  
If Solution Explorer has disappeared from IDE how to bring back?

Re: Inconsistent accessibility continue
Posted By Vulpes on 23 Aug 2013  
Under the View menu, click on Solution Explorer.
Maha
  • 0
  • 0
Re: Inconsistent accessibility continue
Posted By Maha on 23 Aug 2013  
And how to dock properties Window vertically under Solution Explorer Window.

Re: Inconsistent accessibility continue
Posted By Vulpes on 23 Aug 2013  
Just 'drag' it into position.
Maha
  • 0
  • 0
Re: Inconsistent accessibility continue
Posted By Maha on 23 Aug 2013  
file.zip Dogging is not coming I don't know why. I can remember before I successfully did with older version. If I drag, either properties window is overlapping Solution Explorer or coming side by side or properties window is appearing horizontally.

Secondly I attach two files. I couldn't see 'Copy Local' in the Properties Box (this question is associated with thread 9).



Re: Inconsistent accessibility continue
Posted By Vulpes on 23 Aug 2013  
It's not the program.cs file whose properties we're interested in here.

It's the program.exe file (i.e. after it has been built and added as a reference to program2.exe) where I think you'll find that the Properties Box shows Copy Local to be true.

However, another way to tell is to simply open the folder containing program2.exe and see whether there's a copy of program.exe in there.

Maha
  • 0
  • 0
Re: Inconsistent accessibility continue
Posted By Maha on 23 Aug 2013  
Please tell me where you can find folder containing program2.exe.
Re: Inconsistent accessibility continue
Posted By Vulpes on 24 Aug 2013  
The easiest way is to use the search box from the Windows Start button.

Just look for program.exe and program2.exe and see if they're in the same folder.

Maha
  • 0
  • 0
Re: Inconsistent accessibility continue
Posted By Maha on 24 Aug 2013  
I wish to know when I search whether I have to use class name or file name. Because Program and Prgram2 are class name. You can see this in the above thread.

Re: Inconsistent accessibility continue
Posted By Vulpes on 24 Aug 2013  
Well, it appears from those 2 images that program.cs and program2.cs are going to generate executables called program.exe and program2.exe, respectively.

So it's the executables that you should be searching for.

Maha
  • 0
  • 0
Re: Inconsistent accessibility continue
Posted By Maha on 25 Aug 2013  
search programexe.zip I attach what I got in program.exe search. There is a backup folder I wish to know whether this is the one I am looking for.

In program2.exe search I couldn't find any relevant folder.


Re: Inconsistent accessibility continue
Posted By Vulpes on 25 Aug 2013  
I hadn't notice it before but looking again at the images in your penultimate post, it appears that the solutions have the following names:

Going to be added as a reference to  (for program.cs)

Another assembly (for program2.cs).

By default the .exes will be given the same names as the solutions, so you should be searching for:

   Going to be added as reference to.exe

   Another assembly.exe

to see whether they're in the same folder.

Maha
  • 0
  • 0
Re: Inconsistent accessibility continue
Posted By Maha on 26 Aug 2013  
file.zip I attach the result in "Going to be added as reference to.exe" and "Another assembly.exe" search. I can see a relevant file in "Going to be added as reference to.exe" search result. I wish to confirm whether it is the file which I am looking for.

Re: Inconsistent accessibility continue
Posted By Vulpes on 26 Aug 2013  
Yes, that's the one.

If you right click on the file names and choose Properties, General tab, you can check that the two files are in the same location.

Maha
  • 0
  • 0
Re: Inconsistent accessibility continue
Posted By Maha on 26 Aug 2013  
Problem is even though I deleted "Going to be added as reference to.exe" from the system Another assembly program is still executing.

Re: Inconsistent accessibility continue
Posted By Vulpes on 26 Aug 2013  
Do you mean that you've just deleted the copy of "Going to be added as reference to.exe" that you've just found in (hopefully) the same folder as "Another assembly.exe", but the latter is still working OK?
Maha
  • 0
  • 0
Re: Inconsistent accessibility continue
Posted By Maha on 26 Aug 2013  
In "Going to be added as reference to.exe" search there are two files found one is "Going to be added as reference to.exe" and other one is "Another assembly" file.

I deleted one file which is "Going to be added as reference to.exe" and "Another assembly" file remains intact. But Another assembly program (source code of Another assembly) is still executing.


Re: Inconsistent accessibility continue
Posted By Vulpes on 27 Aug 2013  
Well, I don't understand that at all :-/

The second program must have access to a copy of the first program to execute - it won't know anything about the classes in the first program otherwise.

Using Windows Explorer or the command line, try navigating to this folder:

C:\Users\user\Documents\Visual Studio 2012\Projects\Another assembly\Another assembly\bin

There should be two folders in there: debug and release. The executable, 'Another assembly.exe', should be under the 'release' folder if you've compiled it in release mode.

Otherwise you should find an executable called 'Another asembly.vshost.exe' under the 'debug' folder.

Just check these folders to see if there's a copy of either:

'Going to be added as reference to.exe' 

or:

'Going to be added as reference to.vshost.exe' 

within them.


Maha
  • 0
  • 0
Re: Inconsistent accessibility continue
Posted By Maha on 28 Aug 2013  
debug.zip I attach Debug folder. Yes I can see two "Going to be added as reference to" files without .exe extension. I assume this is the file I am looking for.

Re: Inconsistent accessibility continue
Posted By Vulpes on 28 Aug 2013  
I think it must be.

Possibly, the .exe is missing because the name is too long to fit in the box.

Maha
  • 0
  • 0
Re: Inconsistent accessibility continue
Posted By Maha on 28 Aug 2013  
reference manager.zip Thank you for your help.

In the Reference Manager Window there is a pop-up window with two texts in it, one is saying "Clear Recent References" other one is saying "Remove from Recent References" it is very difficult to distinguish differences between these two. Please explain the differences. Problem is in attached file.


Re: Inconsistent accessibility continue
Posted By Vulpes on 28 Aug 2013  
I think the Reference Manager window must be a new addition in VS 2012 as I don't recall seeing it in previous versions.

My guess is that 'Remove from Recent References' removes those references that are ticked but 'Clear Recent References' removes them all, ticked or not.

Maha
  • 0
  • 0
Re: Inconsistent accessibility continue
Posted By Maha on 28 Aug 2013  
Thank you for your explanation.

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.