Inconsistent accessibility error: field less accessible than field - c#

I've tried to solve this problem that I have but I really don't know what else to do. I get this error:
Inconsistent accessibility: field type 'ChatClient.Configurator.IPChangeHandler' is less accessible than field 'ChatClient.Configurator.IPChange'
and this is part of the code:
namespace ChatClient
{
public partial class Configurator : Form
{
public delegate void IPChangeHandler(object sender, IPAddressInfoEventArgs e);
public event IPChangeHandler IPChange;
// ...
}
}
making delegate and class public did not worked out.
Thank you!

Check accessible level of IPAddressInfoEventArgs class.
It must be public since the event IPChange is public too.

Please Go to Solution Part ..& Open the IPAddressInfoEventArgs.cs file ....
Update in that File....
using System;
using System.Collections.Generic;
using System.Text;
namespace ChatClient
{
public class IPAddressInfoEventArgs : EventArgs
{
private string _ipAddress;
public IPAddressInfoEventArgs(string ipAddress)
{
this._ipAddress = ipAddress;
}
public string IPAddress
{
get { return this._ipAddress; }
}
}
}

Related

C# class can't see static method of another class

I've run into some accessibility problem with one of my Unity projects. I created a class that does NOT inherit from MonoBehaviour. It has a method public static void LoadScene(string sceneName).
When I try to call this method from another class, I get a syntax error. This is my first script with the static method:
public class GameLoader
{
public static void LoadScene(string sceneName)
{
SceneManager.LoadSceneAsync(sceneName);
}
}
And here is my other script:
public class GameHandler : MonoBehaviour
{
private void Start()
{
GameLoader.LoadScene("MyScene"); //Syntax error
}
}
Normally, I would have some idea about what might the problem be, but in this case, the GameHandler recognizes GameLoader as class, but after the dot (GameLoader.), it does not find any property or function at all. And I get a syntax error when I try to write anything after the dot.
I experimented a little and it doesn't seem like I would cross another class with the name GameLoader and the neccessary namespace was added as well.
I'm pretty lost here, I hope someone can help me out.
Original codeGameLoader:
using UnityEngine.SceneManagement;
using UnityEngine;
namespace MyGame
{
namespace System
{
public class GameLoader
{
public static void LoadScene()
{
}
}
}
}
Original UIHandler:
using UnityEngine;
using System;
namespace MyGame
{
namespace System
{
namespace UI
{
public class UIHandlerMenu : MonoBehaviour
{
GameLoader.LoadScene();
}
}
}
}
Error message:
Severity Code Description Project File Line Suppression State
Error IDE1007 The name 'GameLoader.LoadScene' does not exist in the current context.
And the same error for just LoadScene itself.
Edit:
After your edit it is now obvious where the problem lies.
Try moving your call GameLoader.LoadScene("bla"); into a method.
If you want this method getting called when instantiating your handler you can move it to a constructor.
Example:
public UiHandlerMenu() {
GameLoader.LoadScene("bla");
}

Why can't i use a method from another class

I am putting together an application but I'm getting a strange issue where i can't use any methods from a class i've created with a couple of methods, the methods don't do anything at the moment because I'm just getting the shell of the program in place. I am trying to call from the Form1 class below, specifically from a button click checking a specific operation from radio buttons.
If btnDeviceControlAccept_Click is clicked it checks which of the radio buttons and goes to a method in the DeviceControlMethods class such as Add, Change or Delete VLAN. When i use the object (dc, DeviceControlMethods dc = new DeviceControlMethods();)I created in the Form1 i'm unable to use the methods even if the class is public or if i set the methods to static and use DeviceControlMethods.AddVlan etc.
I'm sure I'm just doing something daft because I've not doing C# in quite a while.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MFT___Configurator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void btnDeviceControlAccept_Click(object sender, EventArgs e)
{
DeviceControlMethods dc = new DeviceControlMethods();
if (rbAddDevice.Checked == true)
{
dc.CreateVlan() // the method is not found
resutlBox.Clear();
}
else if (rbChange.Checked == true)
{
resutlBox.Clear();
}
else if (rbDelete.Checked == true)
{
resutlBox.Clear();
}
else
{
resutlBox.Clear();
resutlBox.Text = "Select a valid operation; Add, Change or Delete.";
}
}
Class with the methods i want to call;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MFT___Configurator
{
public class DeviceControlMethods
{
static DeviceControlMethods()
{
string CreateVlan()
{
Console.WriteLine("ggg");
return "";
}
string ChangeVlan()
{
return "";
}
void DeleteVlan()
{
}
}
}
}
I see only private methods, you need to make them public explicitly, not only the class. See the docs about access modifiers
public
The type or member can be accessed by any other code in the same assembly or another assembly that references it.
private
The type or member can be accessed only by code in the same class or struct.
protected
The type or member can be accessed only by code in the same class, or in a class that is derived from that class.
internal
The type or member can be accessed by any code in the same assembly, but not from another assembly.
protected internal
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly.
private protected
The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is derived from that class.
Edit
And, as other comments state as well, methods defined in the static constructor won't be accessible either.
You have scoping issues with your class. Read through this article to learn more about scoping in C#. https://msdn.microsoft.com/en-us/library/ms973875.aspx
But to solve your issue, change your class to be as follows:
public class DeviceControlMethods
{
public string CreateVlan()
{
Console.WriteLine("ggg");
return "";
}
public string ChangeVlan()
{
return "";
}
public void DeleteVlan()
{
}
}

Error when trying to keep code inside of Main method

I am trying to teach myself this so I am sure this is obvious.
I am trying to create 2 classes that I can call instances of in Program/Main. One class is a string to double tryparse method and the other will just hold variables that will be used for many things.
My problem is I can only set Main up without error if the Main Method is only holding my new instance of class statements so I am exiting code immediately.
I will post the code to the Main. Newbie code and question, any help is much appreciated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SetupMath
{
class Program
{
public static void Main(string[] args)
//Bringing in classes to inherit methods from
{
StringToDouble IntakeParse = new StringToDouble();
SetUpVar GuitAction = new SetUpVar();
}
// new instance of the StringToDouble class
// getting variable value "action" from string to double tryparse
public class StringToDouble
{
public string action { get; set; }
//converting "action" variable to "what"
public string What
{
get { return this.action; }
set { this.action = value; }
}
}
public class SetUpVar // new instance of the SetVar class
{
public string GuitAction { get; set; }
public string What { get; set; }
//Do something code
public void Work()
{
Console.WriteLine("Please enter a number", GuitAction);
What = Console.ReadLine();
Console.WriteLine("You entered: " + What);
}
}
}
}
You are writing StringToDouble and SetUpVar classes inside the Program class scope that's why they are visible only there. If you want your classes to be visible inside the whole namespace you should write them outside of Program class

Namespaces Scope

I have a class named AuditLog inside Domain.AuditLog namespace. I want to use AuditLog class inside another class with namespace ApplicationServices.AuditLog. like:
using Domain.AuditLog;
namespace ApplicationServices.AuditLog
{
public interface IAuditLogService
{
List<AuditLog> GetAuditLogs();
}
}
It says 'ApplicationServices.AuditLog' is a 'namespace' but is used like a 'type'. I know I can solve this using like:
namespace ApplicationServices.AuditLog
{
using Domain.AuditLog;
public interface IAuditLogService
{
List<AuditLog> GetAuditLogs();
}
}
Is there another way of referencing Domain.AuditLog ?
Maybe this could help you:
using AL = Domain.AuditLog.AuditLog;
namespace ApplicationServices.AuditLog
{
public interface IAuditLogService
{
List<AL> GetAuditLogs();
}
}

How to deal with imported class and use them in Parent Class

How do i rename an imported class so i can access it.Below is the code of the Class with a different name space which was imported and the Class of the one i am working on respectively.Should i just change the name space?
namespace FaultTreeSelectionAsistant
{
public partial class Astra : Form
{
public Astra()
{
InitializeComponent();
}
}
}
namespace THE_HELP
{
public partial class MainPanel : Form
{
public MainPanel()
{
InitializeComponent();
}
private void MainPanel_Load(object sender, EventArgs e)
{
}
}
}
You can give an alias to a namespace or a class like this :
using FTSA = FaultTreeSelectionAsistant;
using FtsaAstra = FaultTreeSelectionAsistant.Astra;
Use . (dot) to delimit namespaces in a declaration :
In MainPanel, you can declare:
FaultTreeSelectionAsistant.Astra astra;
To avoid expliciting the namespace name, you can import it with a using declarative:
using FaultTreeSelectionAsistant;
Please consult MSDN for a complet description of what are namespaces and how to use them.
Simplest way is to use intellisense:
Just write the class name for example Astra, mouse hover and see the option:

Categories

Resources