Asp.Net C# Object Related Ques - c#

I am new to Asp .net C#. i have question about objects and inheritance.
if i have parent class (Base-Table) that have 2 child classes (Credit-Card-Table , Bank-Account-Table) i have fun. in another class that take an object from the base-table class.
my problem is i want to know if the Base-table is Credit-card or Bank-account ?!
class BaseTable
{
string date;
public string Date
{
get { return date; }
set { date = value; }
}
string description;
public string Description
{
get { return description; }
set { description = value; }
}
}
class CreditCardTable:BaseTable
{
string Amount;
public string amount
{
get { return Amount; }
set { Amount = value; }
}
string Type;
public string type
{
get { return Type; }
set { Type = value; }
}
}
class BankAccountTable:BaseTable
{
string Refr;
public string Ref
{
get { return Refr; }
set { Refr = value; }
}
string debit;
public string Debit
{
get { return debit; }
set { debit = value; }
}
string credit;
public string Credit
{
get { return credit; }
set { credit = value; }
}
}

3 options:
use is, as or GetType() to explicitly check the type of an instance you have been given, to test it against some known types
if(obj is CreditCardTable) {...} else ...
add a virtual or abstract method to the base-type, and use that instead of ever having to worry about which it is (since it will automatically invoke the most derived override)
obj.SomeMethod();
add a discriminator - perhaps a virtual enum property to the BaseTable which all derived types return a different value from, and switch on that discriminator:
switch(obj.Type) { ... }

Related

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.

Inconsistent accesibility: property type is less accesible than property [duplicate]

Please can someone help with the following error:
Inconsistent accessibility: property type 'Test.Delivery' is less accessible than property 'Test.Form1.thelivery'
private Delivery thedelivery;
public Delivery thedelivery
{
get { return thedelivery; }
set { thedelivery = value; }
}
I'm not able to run the program due to the error message of inconsistency.
Here is my delivery class:
namespace Test
{
class Delivery
{
private string name;
private string address;
private DateTime arrivalTime;
public string Name
{
get { return name; }
set { name = value; }
}
public string Address
{
get { return address; }
set { address = value; }
}
public DateTime ArrivlaTime
{
get { return arrivalTime; }
set { arrivalTime = value; }
}
public string ToString()
{
{ return name + address + arrivalTime.ToString(); }
}
}
}
make your class public access modifier,
just add public keyword infront of your class name
namespace Test
{
public class Delivery
{
private string name;
private string address;
private DateTime arrivalTime;
public string Name
{
get { return name; }
set { name = value; }
}
public string Address
{
get { return address; }
set { address = value; }
}
public DateTime ArrivlaTime
{
get { return arrivalTime; }
set { arrivalTime = value; }
}
public string ToString()
{
{ return name + address + arrivalTime.ToString(); }
}
}
}
Your class Delivery has no access modifier, which means it defaults to internal. If you then try to expose a property of that type as public, it won't work. Your type (class) needs to have the same, or higher access as your property.
More about access modifiers: http://msdn.microsoft.com/en-us/library/ms173121.aspx
Your Delivery class is internal (the default visibility for classes), however the property (and presumably the containing class) are public, so the property is more accessible than the Delivery class. You need to either make Delivery public, or restrict the visibility of the thelivery property.

Populating combobox with two different objects

I want to populate a combobox with two different objects using an interface. This is what I currently got. This works but now I would like to have a display member and value member for each object, how would I do so?
In Controller.cs
public List<IMusic> Populate()
{
List<IMusic> newList = new List<IMusic>();
foreach(Track t in tr.GetAllTracks()){
newList.Add(t);
}
foreach (Artist a in ar.GetAllArtists())
{
newList.Add(a);
}
return newList;
}
IMusic.cs
interface IMusic
{
}
The combobox with DataSource:
cBMainScreen_Search.DataSource = controller.Populate();
GetAllTracks() :
public List<Track> GetAllTracks()
{
return db.Track.ToList();
}
GetAllArtists() :
public List<Artist> GetAllArtists()
{
return db.Artist.ToList();
}
Just setup some properties in your interface:
interface IMusic
{
string Display { get; set; }
string Value { get; set; }
}
Then in your Track class (which should implement IMusic):
public string Display
{
get
{
return this.TrackName;
}
set
{
this.TrackName= value;
}
}
public string Value
{
get
{
return this.TrackID;
}
set
{
this.TrackID= value;
}
}
And in your Artist class (also implements IMusic):
public string Display
{
get
{
return this.ArtistName;
}
set
{
this.ArtistName= value;
}
}
public string Value
{
get
{
return this.AritstID;
}
set
{
this.AritstID= value;
}
}

Preferred way to set default values of nullable properties?

I'm in a dilemma. The (reduced) task is to redesign the following data holder class
class Stuff
{
public String SomeInfo { get; set; }
}
to accommodate the demand that null mustn't be returned. I can think of two ways to achieve that and after deep consideration of 15 minutes, I simply can't decide which one is to be preferred.
Approach by constructor.
class Stuff
{
public String SomeInfo { get; set; }
public Stuff() { SomeInfo = String.Empty; }
}
Approach by property.
class Stuff
{
private String _SomeInfo;
public String SomeInfo
{
get { return _SomeInfo ?? String.Empty; }
set { _SomeInfo = value; }
}
}
Note that the creation of the Stuff instances might be done using the constructor as well as initialization, if that's of any significance. As far as I'm informed, there won't be any other restrictions (but you know how the customers' specifications not always reflect the reality).
You can only ensure that null is never returned when you use the property:
class Stuff
{
private String _SomeInfo;
public String SomeInfo
{
get { return _SomeInfo ?? String.Empty; }
set { _SomeInfo = value; }
}
}
The same approach is used by text-controls(e.g. in ASP.NET) where the Text property never returns null but String.Empty.
For example(ILSpy):
// System.Web.UI.WebControls.TextBox
public virtual string Text
{
get
{
string text = (string)this.ViewState["Text"];
if (text != null)
{
return text;
}
return string.Empty;
}
set
{
this.ViewState["Text"] = value;
}
}
Just to add another answer to this, you can also set a default value to a string object in a single statement;
class Stuff
{
private String Something {get; set;} = string.Empty;
}
You can also implement the logic in the setter rather than in the getter, that way your back field always has a valid value
class Stuff
{
private String _SomeInfo = string.Empty;
public String SomeInfo
{
get { return _SomeInfo; }
set
{
if (value != null)
{
_SomeInfo = value;
}
}
}
}

Derived Class to use property of Base class?

I have a class, with a public property "appController", as follows:
public class FAST
{
#region Props
public AppController.AppControllerClass appController = new AppController.AppControllerClass();
#endregion
#region Contructors
public FAST(AppController.AppControllerClass appcontroller)
{
this.appController = appcontroller;
}
#endregion
}
I have another few class, in which I would like to use the appController of FAST, the above class.They look like:
public class Forecast
{
#region Properties
private int _forecastnumber;
public int ForecastNumber
{
get { return _forecastnumber; }
set { _forecastnumber = value; }
}
private DateTime _startdate;
public DateTime StartDate
{
get { return _startdate; }
set { _startdate = value; }
}
private DateTime _enddate;
public DateTime EndDate
{
get { return _enddate; }
set { _enddate = value; }
}
private DateTime _deadline;
public DateTime Deadline
{
get { return _deadline; }
set { _deadline = value; }
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _type;
public string Type
{
get { return _type; }
set { _type = value; }
}
private string _description;
public string Description
{
get { return _description; }
set { _description = value; }
}
private string _status;
public string Status
{
get { return _status; }
set { _status = value; }
}
#endregion
#region Constructors
public Forecast()
{
}
#endregion
#region Methods
public static void InsertForecast(Forecast forecast)
{
try
{
this.appController.Execute(appController.nDC.FASTData.InsertForecast(forecast.StartDate, forecast.EndDate, forecast.Deadline, forecast.Type, forecast.Name, forecast.Description, forecast.Status));
}
catch (Exception ex)
{
this.appController.LogError(ex);
}
}
#endregion
}
I want to be able to declare the FAST class once, passing in the AppController, then use my other classes freely, and they will use the appcontroller of the FAST class.
Can this be done at all? (inheritance?)
Thanks for any help.
It sounds like you simply want a static class for your FAST class. If you define the AppController variable as static, it will be accessible from anywhere.
I would say no to inheritance. Inheritance suggests an "is" relationship, e.g. "Forecast is a specialized version of the app controller." Aggregation, a specialized form of object composition, suggests a "has" relationship, e.g. "Forecast has an app controller."
http://en.wikipedia.org/wiki/Object_composition#Aggregation
You could add a setter method to set your FAST object as a property of Forecast:
public FAST appController { get; set; }
And then
var f = new FAST(new AppController.AppControllerClass());
var forecast = new Forecast();
var forecast2 = new Forecast();
forecast.appController = f;
forecast2.appController = f;

Categories

Resources