C# get/set inaccessibility error - c#

I am getting an error as followed:
Inconsistent accessibility: property type 'AudioDevices.Tracks.track.Time' is less accessible than property 'AudioDevices.Tracks.track.length'
I have no clue what it is, or how i can fix it. Anybody that can help me?
This is all the code i have, [template = class library]:
namespace AudioDevices.Tracks
{
public class Track
{
#region STRUCT
private int id;
private string name;
private string artist;
private string albumSource;
private Time length;
private category style;
public enum category{
Ambient, Blues, Country, Disco, Electro, Hardcore, HardRock, HeavyMetal, Hiphop, Jazz, Jumpstyle,
Klassiek, Latin, Other, Pop, Punk, Reggae, Rock, Soul, Trance, Techno
};
#endregion
#region GET/SET
public int Id{
get { return id; }
set { id = value; }
}
public string Name{
get { return name; }
set { name = value; }
}
public string Artist{
get { return artist; }
set { artist = value; }
}
public string AlbumSource{
get { return albumSource; }
set { albumSource = value; }
}
public Time Length{
set { length = value; }
}
public string DisplayTime
{
get { return length.ToString(); }
}
public category Style
{
get { return style; }
set { style = value; }
}
#endregion
#region TIME CONSTRUCTOR
struct Time
{
int seconds;
int minutes;
int hours;
public Time(int seconds)
{
this.seconds = seconds;
this.minutes = 0;
this.hours = 0;
}
public Time(int seconds, int minutes)
{
this.seconds = seconds;
this.minutes = minutes;
this.hours = 0;
}
public Time(int seconds, int minutes, int hours)
{
this.seconds = seconds;
this.minutes = minutes;
this.hours = hours;
}
public override string ToString()
{
return hours + ":" + minutes + ":" + seconds;
}
}
#endregion
#region TRACK CONSTRUCTOR
public Track(){ }
public Track(int id)
{
this.id = id;
}
public Track(int id, string name)
{
this.id = id;
this.name = name;
}
public Track(int id, string name, string artist)
{
this.id = id;
this.name = name;
this.artist = artist;
}
#endregion
#region GetLength
public string GetLength()
{
return length.ToString();
}
public int GetLengthInSeconds(int seconds, int minutes, int hours){
int SecondsToSeconds = seconds;
int MinutesToSeconds = minutes * 60;
int HoursToSeconds = hours * 3600;
int TotalSeconds = HoursToSeconds + MinutesToSeconds + SecondsToSeconds;
return TotalSeconds;
}
#endregion
}
}

You've got a public property here:
public Time Length{
set { length = value; }
}
... but the type of that property is Time, which is a private type:
struct Time {
...
}
(It's private because it's a nested type; if it were declared as a top-level type it would be internal by default, which would still have the same problem.)
Public member signatures can't refer to private or internal types anywhere in the parameter types or return type. The member simply wouldn't be meaningful to the caller if they were in a different assembly.
So, the fix is to either make Time a public type (and I'd recommend extracting it as a top-level type at the same time) or to make Time a private property.

From MSDN;
The access level for class members and struct members, including
nested classes and structs, is private by default.
So, your Time struct is private by default.
On this part;
public Time Length
{
set { length = value; }
}
You are trying to create public property the type of private struct. You can't do that. For fixing it,
Change your Length property access modifier public to private.
or
Set your Time struct access modifier to public.

This might be because you are using a construct Time.
Try to alter your code like:
public Time Length{
set { length = new Time(value); }
}

Related

Selecting a random object from a list of objects

I'm trying to randomly select one of the customer items from the list. I am not sure what to do to actually print out the info within the list.
I have this as my customer class
namespace PizzaParlor
{
class Customer
{
private string name;
private int flavor;
private int price;
private int quality;
private int speed;
private int accessibility;
private int brand;
private int convenience;
private int variety;
public Customer(string name, int flavor, int price, int quality, int speed, int accessibility, int brand, int convenience, int variety)
{
this.name = name;
this.flavor = flavor;
this.price = price;
this.quality = quality;
this.speed = speed;
this.accessibility = accessibility;
this.brand = brand;
this.convenience = convenience;
this.variety = variety;
}
// Name, Speed, Quality, Variety, Convenience, Accessibility, price, brand, flavor
public string Name
{
get { return name; }
set { name = value; }
}
public int Speed
{
get { return speed; }
set { speed = value; }
}
public int Quality
{
get { return quality; }
set { quality = value; }
}
public int Variety
{
get { return variety; }
set { variety = value; }
}
public int Convenience
{
get { return convenience; }
set { convenience = value; }
}
public int Accessibility
{
get { return accessibility; }
set { accessibility = value; }
}
public int Price
{
get { return price; }
set { price = value; }
}
public int Brand
{
get { return brand; }
set { brand = value; }
}
public int Flavor
{
get { return flavor; }
set { flavor = value; }
}
}
}
and this as my main class that I set up to work with the customer class:
namespace PizzaParlor
{
class Program
{
static void Main(string[] args)
{
var random = new Random();
List<Customer> CustomerList = new List<Customer>();
CustomerList.Add(new Customer("bill", 20,15,10,5,10,20,5,15));
CustomerList.Add(new Customer("kevin", 15, 10, 5, 20, 15, 15, 0, 20));
CustomerList.Add(new Customer("clair", 8,25,2,25,5,15,0,20));
CustomerList.Add(new Customer("jim", 15,20,10,15,0,40,0,0));
CustomerList.Add(new Customer("rachel", 20,15,10,5,10,30,0,10));
CustomerList.Add(new Customer("jeff", 30,20,5,5,10,10,0,20));
CustomerList.Add(new Customer("Mike", 21,23,0,10,14,16,0,16));
CustomerList.Add(new Customer("john", 25,15,10,10,10,5,5,20));
int index = random.Next(CustomerList.Count);
Console.WriteLine(CustomerList[index]);
}
}
}
I know that the random.Next(CustomerList.Count) will randomly a select from the list but I don't know why it is returning this output:
This is because the when you attempt to print an object (e.g. Customer, the default implementation of ToString() is executed. This produces the output that you see.
There are 2 ways of fixing it
Print explicit fields you're interested in
int index = random.Next(CustomerList.Count);
var customer = CustomerList[index];
Console.WriteLine($"customer name = {customer.Name}, flavour = {customer.Flavour}}");
Override the ToString implementation
class Customer
{
//...
// Existing code
// ..
public override string ToString ()
{
return $"customer name = {customer.Name}, flavour = {customer.Flavour}}";
}
}
In your main method
int index = random.Next(CustomerList.Count);
var customer = CustomerList[index];
Console.WriteLine(customer);
You can use Reflection to achieve this.
foreach (var prop in typeof(Customer).GetProperties())
{
Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(CustomerList[index]));
}

Where is the 'value' variable set in properties?

In C# properties, where is the 'value' variable defined? I can see it used in the bodies of setters before it is defined anywhere.
namespace TestBindings
{
public class Dog
{
private decimal age;
private string name;
private const int AgeFactor = 7;
public Dog(decimal age, string name)
{
this.age = age;
this.name = name;
}
public decimal AgeDogYears
{
get { return age / AgeFactor; }
set { age = value * AgeFactor; }
}
public decimal AgeHumanYears
{
get { return age; }
set { age = value; } //here
}
public string Name
{
get { return name; }
set { name = value; } // and here
}
}
}
The 'value' variable is automatically passed in from the use-site and is a pre-defined variable name for the value passed in the set expression.
e.g.
var jack = new Dog(13, "jack");
jack.Name = "Jackson";
Here the value after the = sign is being passed into the setter defined in the class as 'value'.
public string Name
{
get { return name; }
set { name = value; } //here
}
It's roughly equivalent to the Java expression it replaces of having an explicit getter and setter method, just using different syntax in order to unify settings fields and properties.
e.g.
public class Dog {
private double age;
private String name;
private final int AgeFactor = 7;
public Dog(double age, String name) {
this.age = age;
this.name = name;
}
public double getAgeHumanYears() {
return age;
}
public void setAgeHumanYears(double value) {
this.age = value;
}
public double getAgeDogYears() {
return age / AgeFactor;
}
public void setAgeDogYears(double value) {
age = value * AgeFactor;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
With our test changing to.
private Dog jack = new Dog(13, "jack");
jack.setName("Jackson");

Neo4jClient Node/Relationship Class conventions

Is there a standard naming convention for the properties/methods of a node/relationship class when working with Neo4jClient?
I'm following this link Neo4jClient - Retrieving relationship from Cypher query to create my relationship class
However, there are certain properties of my relationship which i can't get any value despite the relationship having it. While debugging my code, i realized certain properties was not retrieved from the relationship when creating the relationship object.
this is my relationship class
public class Creates
{
private string _raw;
private int _sourcePort;
private string _image;
private int _DestinationPort;
private int _eventcode;
private string _name;
private string _src_ip;
private int _src_port;
private string _dvc;
private int _signature_ID;
private string _dest_ip;
private string _computer;
private string _sourceType;
private int _recordID;
private int _processID;
private DateTime _time;
private int _dest_port;
public string Raw { get { return _raw; } set { _raw = value; } }
public int SourcePort { get { return _sourcePort; } set { _sourcePort = value; } }
public string Image { get { return _image; } set { _image = value; } }
public int DestinationPort { get { return _DestinationPort; } set { _DestinationPort = value; } }
public int Eventcode { get { return _eventcode; } set { _eventcode = value; } }
public string Name { get { return _name; } set { _name = value; } }
public string Src_ip { get { return _src_ip; } set { _src_ip = value; } }
public int Src_port { get { return _src_port; } set { _src_port = value; } }
public string DVC { get { return _dvc; } set { _dvc = value; } }
public int Signature_ID { get { return _signature_ID; } set { _signature_ID = value; } }
public string Dest_ip { get { return _dest_ip; } set { _dest_ip = value; } }
public string Computer { get { return _computer; } set { _computer = value; } }
public string SourceType { get { return _sourceType; } set { _sourceType = value; } }
public int RecordID { get { return _recordID; } set { _recordID = value; } }
public int ProcessID { get { return _processID; } set { _processID = value; } }
public DateTime Indextime { get { return _time; } set { _time = value; } }
public int Dest_port { get { return _dest_port; } set { _dest_port = value; } }
}
This is another class
public class ProcessConnectedIP
{
public Neo4jClient.RelationshipInstance<Pivot> bindto { get; set; }
public Neo4jClient.Node<LogEvent> bindip { get; set; }
public Neo4jClient.RelationshipInstance<Pivot> connectto { get; set; }
public Neo4jClient.Node<LogEvent> connectip { get; set; }
}
This is my neo4jclient query to get the relationship object
public IEnumerable<ProcessConnectedIP> GetConnectedIPs(string nodeName)
{
try
{
var result =
this.client.Cypher.Match("(sourceNode:Process{name:{nameParam}})-[b:Bind_IP]->(bind:IP_Address)-[c:Connect_IP]->(connect:IP_Address)")
.WithParam("nameParam", nodeName)
.Where("b.dest_ip = c.dest_ip")
.AndWhere("c.Image=~{imageParam}")
.WithParam("imageParam", $".*" + nodeName + ".*")
.Return((b, bind, c, connect) => new ProcessConnectedIP
{
bindto = b.As<RelationshipInstance<Creates>>(),
bindip = bind.As<Node<LogEvent>>(),
connectto = c.As<RelationshipInstance<Creates>>(),
connectip = connect.As<Node<LogEvent>>()
})
.Results;
return result;
}catch(Exception ex)
{
Console.WriteLine("GetConnectedIPs: Error Msg: " + ex.Message);
return null;
}
}
This is the method to read the results
public void MyMethod(string name)
{
IEnumerable<ProcessConnectedIP> result = clientDAL.GetConnectedIPs(name);
if(result != null)
{
var results = result.ToList();
Console.WriteLine(results.Count());
foreach (ProcessConnectedIP item in results)
{
Console.WriteLine(item.Data.Src_ip);
Console.WriteLine(item.bindto.StartNodeReference.Id);
Console.WriteLine(item.bindto.EndNodeReference.Id);
Console.WriteLine(item.connectto.StartNodeReference.Id);
Console.WriteLine(item.connectto.EndNodeReference.Id);
Node<LogEvent> ans = item.bindip;
LogEvent log = ans.Data;
Console.WriteLine(log.Name);
Node<LogEvent> ans1 = item.connectip;
LogEvent log1 = ans1.Data;
Console.WriteLine(log1.Name);
}
}
}
Somehow, i'm only able to populate the relationship object with src_ip/src_port/dest_ip/dest_port values. the rest are empty.
Is there any possible reason why? I've played with upper/lower cases on the properties names but it does not seem to work.
This is the section of the graph im working with
This is the relationship properties sample:
_raw: Some XML dataSourcePort: 49767Image: C:\Windows\explorer.exeDestinationPort: 443EventCode: 3Name: Bind
IPsrc_ip: 172.10.10.104dvc: COMPUTER-NAMEsrc_port:
49767signature_id: 3dest_ip: 172.10.10.11Computer:
COMPUTRE-NAME_sourcetype:
XmlWinEventLog:Microsoft-Windows-Sysmon/OperationalRecordID:
13405621ProcessId: 7184_time: 2017-08-28T15:15:39+08:00dest_port: 443
I'm not entirely sure how your Creates class is ever populated, in particular those fields - as your Src_port property doesn't match the src_port in the sample you provided (case wise).
I think it's probably best to go back to a super simple version. Neo4jClient will map your properties to the properties in the Relationship as long as they have the same name (and it is case-sensitive).
So start with a new Creates class (and use auto properties - it'll make your life a lot easier!)
public class Creates
{
public string Computer { get; set; }
}
Run your query with that and see if you get a result, then keep on adding properties that match the name and type you expect to get back (int, string etc)
It seems that i have to give neo4j node/relationship property names in lowercase and without special characters at the start of the property name, in order for the above codes to work.
The graph was not created by me at the start thus i had to work on it with what was given. I had to get the developer who created the graph to create the nodes with lowercases in order for the above to work.

Creating two C# class definitions with a driver to test classes, but not sure why so many errors?

I am new to C# and was asked to create two class definitions (customer and order) using partial code and with the suggested class names, methods, contructors and following an example. I am not sure why I am getting so many errors when I build/debug?
After this is finished, I need to create another program that builds onto this one. Our instructor also asked us not to use validation...
Some of my most common errors are:
expected: ; (in a place in my code where I believe there should not be a semi-colon and
Error "Expected class, delegate, enum, interface, or struct.
Here is my code:
public class clsOrder
{
//declare class variables
protected string cstrDescription;
protected int cintQuantity;
protected decimal cdecPrice;
protected decimal cdecExtendedPrice;
//shared variables
static decimal cdecTotalPrice;
static int cintTotalCount;
//declare constructors
public clsOrder();
}
public clsOrde r(string strDescription,
int intQuantity, decimal decPrice)
}
//declare property methods
{
this.Description = string strDescription;
this.Quantity = int intQuantity;
this.Price = decimal decPrice;
//declare read-only properties
public decimal ExtendedPrice
}
public string Description
{
get
{
return strDescription;
}
set
{
strDescription = value;
}
}
public int Quantity
{
get
{
return intQuantity;
}
set
{
intQuantity = value;
}
}
public decimal Price
{
get
{
return decPrice;
}
set
{
decPrice = value;
}
}
get
{
return cdecExtendedPrice;
}
}
//declare Shared (static) ReadOnly Properites
public static decimal TotalPrice
{
get
{
return cdecTotalPrice;
}
}
public static int TotalCount
{
get
{
return cintTotalCount;
}
}
//declare supporting methods
public void calcExtendedPrice()
{
cdecExtendedPrice = cintQuantity * cdecPrice;
}
public void accumulateTotals()
{
cdecTotalPrice += cdecExtendedPrice;
cintTotalCount += 1;
}
public static void resetTotals()
{
cdecTotalPrice = 0;
cintTotalCount = 0;
}
}//end of Class
}//end of namespace
And
public class clsCustomer
{
//declare class variables
private string cstrName;
private string cstrStreet;
private string cstrCity;
private string cstrState;
private string cstrZip;
//declare constructors
public class clsCustomer()
}
public clsCustomer(string strName,
string strStreet, string strCity,
string strState, string strZip)
}
//declare property methods
{
this.Name = cstrName;
this.Street = cstrStreet;
this.City = cstrCity;
this.State = cstrState;
this.Zip = cstrZip;
}
public string Name
{
get
{
return cstrName;
}
set
{
cstrName = value;
}
}
public string Street
{
get
{
return cstrStreet;
}
set
{
cstrStreet = value;
}
}
public string City
{
get
{
return cstrCity;
}
set
{
cstrCity = value;
}
}
public string State
{
get
{
return cstrState;
}
set
{
cstrState = value;
}
}
public string Zip
{
get
{
return cstrZip;
}
set
{
cstringZip = value;
}
}
Any help would be very much appreciated, thank you.

System.StackOverflowException was unhandled

Please help, I'm confused I do not know why the error logs
System.StackOverflowException was unhandled.
I keep getting an error on set LekID.
How would I that fix?
Here is the code:
public Lager(long lekID, string lek, string proizvojdac, int kolicina, double cena)
{
LekID = lekID;
Lek = lek;
Proizvodjac = proizvojdac;
Kolicina = kolicina;
Cena = cena;
}
public long LekID
{
get { return LekID; }
set { LekID = value; }
}
public string Lek
{
get { return Lek; }
set { Lek = value; }
}
public string Proizvodjac
{
get { return Proizvodjac; }
set { Proizvodjac = value; }
}
public int Kolicina
{
get { return Kolicina; }
set { Kolicina = value; }
}
public double Cena
{
get { return Cena; }
set { Cena = value; }
}
public long LekID
{
get { return LekID; }
set { LekID = value; }
}
This (and the other properties) cause a StackOverflowException, since you are assigning value to LekID over and over again.
You should add a field to the property and store the value there:
private long _lekID;
public long LekID
{
get { return _lekID; }
set { _lekID = value; }
}
You should give different names to your private variables and to your properties. Otherwise, your property is calling itself when you access it.
Example:
long _lekID;
public long LekID
{
get { return _lekID; }
set { _lekID = value; }
}
Or simply:
public long LekID { get; set; }
The properties are calling themself. Try changing your properties like this:
public string Lek
{
get;
set;
}
You're calling the Lek property recursively in both the setter and the getter
Either introduce a backing field:
private string lek;
public string Lek
{
get { return this.lek; }
set { this.lek = value; }
}
or use an Automatic Property:
public string Lek
{
get; set;
}
Marking this community wiki as it is only an aside, but none of this would have happened if you'd been sufficiently lazy (that is often a virtue in programming, not a vice):
public long LekID {get;set;}
public string Lek {get;set;}
public string Proizvodjac {get;set;}
public int Kolicina {get;set;}
public double Cena {get;set;}
less typing; no errors; and you've correctly exposed the API as properties so you can add validation / side-effects later if you need, and it'll work with binding APIs (which don't usually love fields).
Try using code snippet like prop/ propfull,
the snippets will create the properties code automaticly

Categories

Resources