I'm working on a custom UI for Tracking stocks, although one UI I've made has caused an issue that I can't locate. I keep getting the error: "Inconsistent accessibility: parameter type 'SCM_Addin.Funds.TrackFund[]' is less accessible than method 'SCM_Addin.Forms.frm_FundTracker.frm_FundTracker(SM_Addin.Funds.TrackFund[])'
I've checked the protection in my classes, but I can't find any private variables that would hinder the accessibility in my code. Here is the code:
frm_FundTracker:
namespace SCM_Addin.Forms
{
public partial class frm_FundTracker : Form
{
public frm_FundTracker()
{
InitializeComponent();
}
public frm_FundTracker(String[] fundsToAdd, Double[] ePrices, Double[] cPrices)
{
InitializeComponent();
int index = 0;
foreach (String fund in fundsToAdd)
{
ListViewItem newFundItem = new ListViewItem(fund);
newFundItem.SubItems.Add(ePrices[index].ToString());
newFundItem.SubItems.Add(cPrices[index].ToString());
this.list_Tracker.Items.Add(newFundItem);
index++;
}//END LOADING COLUMNS
}//END FRM_FUNDTRACKER WITH ARRAYS
public frm_FundTracker(TrackFund[] funds)
{
InitializeComponent();
foreach (TrackFund fund in funds)
{
ListViewItem newFundItem = new ListViewItem(fund.symbol);
newFundItem.SubItems.Add(fund.entryPrice.ToString());
newFundItem.SubItems.Add(fund.currentPrice.ToString());
this.list_Tracker.Items.Add(newFundItem);
}
}//END FRM_FUNDTRACKER WITH FUNDS
private void btn_Done_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Close Form?", "Close Form?", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
this.Dispose();
}
}
}
}
Fund Class:
namespace SCM_Addin
{
class Fund
{
public String symbol { get; set; } //Symbol of the fund to be used
private int fundRow { get; set; } //Fund's spot in the Stats Sheet.
private String url1, url2, url3;
private HtmlAgilityPack.HtmlDocument doc;
private DataPuller puller;
private Dictionary<String, String> fundStats;
private SqlConnection conn;
public Fund(String sym)
{
this.symbol = sym;
this.doc = new HtmlAgilityPack.HtmlDocument();
this.puller = new DataPuller();
this.url1 = "http://finance.yahoo.com/q?s=" + this.symbol;
this.url2 = "http://finance.yahoo.com/q/ks?s=" + this.symbol;
this.url3 = "http://www.profitspi.com/stock-quote/" + this.symbol + ".aspx";
this.fundStats = new Dictionary<string, string>();
this.conn = null;
}
TrackFund class (Extends Fund)
namespace SCM_Addin.Funds
{
class TrackFund : Fund
{
public double entryPrice { get; set; }
public double currentPrice { get; set; }
public TrackFund(String sym, double entryP, double curP) : base(sym)
{
this.entryPrice = entryP;
this.currentPrice = curP;
}
}
}
This is my first time really extending a class in C#, so if I'm extending wrong, I guess that could be it.
Default accessibility for a class will be internal, so:
class Fund
//and
class TrackFund : Fund
...should be
public class Fund
//and
public class TrackFund : Fund
Make the TrackFund class public.
public class TrackFund : Fund
{
....
}
Related
Loading / storing the 3 floats (ID=1, Loc1=3, Loc2=100) into the public class MInput works fine. However, I like to access / use the same dataset in the Forms2 class, which unfortunately gives me zero values only. What is wrong with the call in Forms2 for textBox1.text and textBox2.text ? Thanks for your ideas.
namespace WinForms01
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
MInput testDat = new MInput
{
ID = 1F,
Loc1 = 3F,
Loc2 = 100F,
};
{
}
}
namespace WinForms01
{
public class MInput
{
[ColumnName("ID"), LoadColumn(0)]
public float ID { get; set; }
[ColumnName("loc1"), LoadColumn(1)]
public float Loc1 { get; set; }
[ColumnName("loc2"), LoadColumn(2)]
public float Loc2 { get; set; }
[ColumnName("loc3"), LoadColumn(4)]
public float Loc3 { get; set; }
}
}
namespace WinForms01
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MInput testDat = new MInput();
textBox1.Text = Convert.ToString(testDat.ID);
textBox2.Text = Convert.ToString(testDat.Loc1);
}
}
}
If you want Form2 to access an object you create elsewhere you have to pass the object to form2. Some instance of class X doesn't just magically get access to an instance of Y just because both different places do a new Y and call them the same name, for the same reason that you buying an iPhone and your brother buying an iPhone of the same model and specification, doesn't mean you can read his messages
namespace WinForms01
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
MInput testDat = new MInput
{
ID = 1F,
Loc1 = 3F,
Loc2 = 100F,
};
Application.Run(new Form2(testDat));
}
}
}
namespace WinForms01
{
public class MInput
{
[ColumnName("ID"), LoadColumn(0)]
public float ID { get; set; }
[ColumnName("loc1"), LoadColumn(1)]
public float Loc1 { get; set; }
[ColumnName("loc2"), LoadColumn(2)]
public float Loc2 { get; set; }
[ColumnName("loc3"), LoadColumn(4)]
public float Loc3 { get; set; }
}
}
namespace WinForms01
{
public partial class Form2 : Form
{
private MInput _minput;
public Form2(MInput minput)
{
_minput = minput;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = Convert.ToString(_minput.ID);
textBox2.Text = Convert.ToString(_minput.Loc1);
}
}
}
Here is my code below. It gives me Casting exception problem at selIngs.Add(da). tried with the 2nd way. it still give me the same exception. I wonder where I am doing wrong? Once I implement the interface or inherit the base class it should be ok to treat child class as the same. Any idea please?
//1st way
public interface IngredientInterface
{
double Concentration { get; set; }
string DateCreated { get; set; }
string DevCode { get; set; }
}
public class IngredientData : INotifyPropertyChanged, IngredientInterface
{
public string GroupCode
{
get { return groupCode; }
set
{
groupCode = value;
}
}
public double Setpoint { get; set; }
public bool IsHighlighted { get; set; }
public double PPT { get; set; }
}
public class FormulaUploadViewModelData: IngredientData
{
//.....
}
public class FormulaUploadViewModel :INotifyPropertyChanged
{
public FormulaUploadViewModel()
{
selIngs = new List<FormulaUploadViewModelData>();
}
private void IngsUp()
{
List<IngredientData> someIngData = new List<IngredientData>();
foreach (FormulaUploadViewModelData da in someIngData)
{
selIngs.Add(da); //here gives me casting exception
}
}
}
//2nd way
public class FormulaUploadViewModelData: IngredientInterface
{
//.....
}
public class FormulaUploadViewModel :INotifyPropertyChanged
{
public FormulaUploadViewModel()
{
selIngs = new List<FormulaUploadViewModelData>();
}
private void IngsUp()
{
List<IngredientInterface> someIngData = new List<IngredientInterface>();
foreach (FormulaUploadViewModelData da in someIngData)
{
selIngs.Add(da); //here gives me casting exception
}
}
}
All FormulaUploadViewModelData are IngredientInterface. So this will work:
var ingredients = new List<IngredientInterface>();
ingredients.Add(new FormulaUploadViewModelData());
But the opposite does not work because not all IngredientInterface are FormulaUploadViewModelData which is what should follow from allowing:
var formulas = new
List<FormulaUploadViewModelData>();
formulas(someIngredientInterface);
Solution? Make sure the da you are adding is in fact a FormulaUploadViewModelData. There is quite a few ways to do it, to name a couple:
Pattern matching
foreach (var da in someInData)
if (da is FormulaUploadViewModelData formula)
selIngs.Add(formula)
Use Enumerable.OfType<> extension method
foreach (var formula in
someInData.OfType<FormulaUploadViewModelData>())
selIngs.Add(formula)
Etc.
I am trying to implement a high-performance game inventory system. I have This abstract base class to store different type of items in Inventory, for example, Coin, Flashlight, Knife etc..
public abstract class ObtainableItem
{
public string Name { get; private set; }
public ObtainableItem(string name)
{
Name = name;
}
}
For example, I have a DoorKey which opens a door. DoorKey has a property KeyCode which will be used for opening a door.
public class DoorKey : ObtainableItem
{
public int KeyCode { get; private set; }
public DoorKey() : base("key")
{
KeyCode = 1234;
}
}
All ObtainableItem are stored in Inventory
public class Inventory
{
const int slotCount = 2;
ObtainableItem[] slots = new ObtainableItem[slotCount];
public Inventory()
{
slots[0] = new DoorKey();
}
}
Now imagine user drags DoorKey from his Inventory on a Door and triggers Open method
public class Door
{
public void Open(ObtainableItem key)
{
if (key is DoorKey)
{
DoorKey doorKey = (DoorKey)key;
if (doorKey.KeyCode == 1234)
{
// Open door
}
}
else
{
// "can't use this item on a door"
}
}
}
How to avoid cast from ObtainableItem to a DoorKey? I have read that using casting is bad practice and it points at a bad code oop design. Ideally, a Door class should look like this. Is there any pattern I should for my inventory system?
public class Door
{
public void Open(DoorKey key)
{
if (key.KeyCode == 1234)
{
// Open door
}
}
}
There are always exceptions that can be made for ease of implementation and readability. What you describe is common, if not typical.
An alternative would be to have the "control" logic in the class that calls Door.Open. This could be easily achieved with a touch of reflection:
public abstract class ObtainableItem
{
public string Name { get; private set; }
public ObtainableItem(string name)
{
Name = name;
}
}
public abstract class WorldItem
{
}
public interface IActsOn<in TWorldItem>
where TWorldItem : WorldItem
{
void ApplyTo(TWorldItem worldItem);
}
public class World
{
// If profiling shows that this is a performance issue, a cache keyed by tWorldItem, tInvItem
// should fix it. No expiry or invalidation should be needed.
private Action<ObtainableItem, WorldItem> GetApplyTo(Type tWorldItem, Type tInvItem)
{
var tActOn = typeof(IActsOn<>).MakeGenericType(tWorldItem);
if (!tActOn.IsAssignableFrom(tInvItem))
{
return null;
}
var methodInfo = tActOn.GetMethod(nameof(IActsOn<WorldItem>.ApplyTo));
return new Action<ObtainableItem, WorldItem>((invItem, worldItem) =>
{
methodInfo.Invoke(invItem, new object[] { worldItem });
});
}
public bool IsDropTarget(WorldItem worldItem, ObtainableItem item)
=> GetApplyTo(worldItem.GetType(), item.GetType()) != null;
public void ActOn(WorldItem worldItem, ObtainableItem item)
{
var actOn = GetApplyTo(worldItem.GetType(), item.GetType());
if (actOn == null)
{
throw new InvalidOperationException();
}
actOn(item, worldItem);
}
}
While this slightly complicates the implementation of World, it simplifies the implementation of various objects:
class Door : WorldItem
{
public void Unlock(string bitting)
{
if (bitting == "1234")
{
Console.WriteLine("Door Opened");
}
else
{
Console.WriteLine("Door could not unlock");
}
}
}
class DoorKey : ObtainableItem, IActsOn<Door>
{
private readonly string Bitting;
public DoorKey(string bitting)
: base("Key")
{
this.Bitting = bitting;
}
public void ApplyTo(Door worldItem)
{
worldItem.Unlock(this.Bitting);
}
}
class RubberChicken : ObtainableItem
{
public RubberChicken()
: base("Rubber chicken")
{
}
}
Example usage:
class Program
{
static void Main(string[] args)
{
var key1 = new DoorKey("1234");
var key2 = new DoorKey("4321");
var rubberChicken = new RubberChicken();
var door = new Door();
var world = new World();
Debug.Assert(!world.IsDropTarget(door, rubberChicken));
Debug.Assert(world.IsDropTarget(door, key1));
world.ActOn(door, key2);
world.ActOn(door, key1);
Console.ReadLine();
}
}
When I was working on nest classes and ran into issues. That's when somebody told me I need to use interface. Along the way on implementing the interface, there I learned that interface doesn't allow constructor or passing of the argument parameters. So, I'm thinking of abstract but I'm uncertain on implementing them and keeping it simple.
In the source code below. Environment Setting is populated in Console project and is passed on to the Database class in DLL project.
Question #1 - Is it possible to abstract Environment Setting for simplier script?
Question #2 - Using the nested class or not, How do I protected the DealerRepository class (Make it invisible to Console project? (I'm guessing abstract).
Question #3 - Does DealerRepository class need to be abstracted or what?
What I want here is to protect/hide the DealerRepository class from Console project and not having to pass on EnvironmentSetting class/model repeatly through nest classes.
Thanks...
//EnvironmentSetting.cs
namespace zTestcase1.Model {
public class EnvironmentSetting : IDisposable
{
public EnvironmentSetting() { } //Constructor...
public void Dispose() { } /Dispose...
//Member variables...
private string _emulation = "";
private string _application = "";
private string _database = "";
//Get/Set properties...
public string Emulation { get {return _emulation;} set {_emulation = value;} }
public string Application { get {return _application;} set {_application = value;} }
public string Database { get {return _database;} set {_database = value;} }
}
}
//DealerRepository.cs
namespace zTestcase1.Data.Dealer
{
public class DealerRepository : IDisposable
{
public DealerRepository(EnvironmentSetting parmEnvironmentSetting) {
_environmentSettingA = parmEnvironmentSetting;
} //Constructor...
public void Dispose() { } //Dispose...
//Member variables...
private EnvironmentSetting _environmentSettingA = "";
//Get/Set properties...
//N/A...
//Functions...
public string EnvironmentResponse()
{
return "Emulation - " + _environmentSettingA.Emulation + ", Application - " + _environmentSettingA.Application + ", Database - " + _environmentSettingA.Database + "";
}
public string DealerProifle_Lookup(string parmName)
{
return "(DealerProfile-Lookup) - " + parmName;
}
public string DealerProfile_Save(string parmName)
{
return "(DealerProfile-Save) - " + parmName;
}
}
}
namespace zTestcase1.Library
{
/*public class Database : IDisposable
{
//Class...
public class DataDealer : IDisposable
{
//Constructor...
public DataDealer(EnvironmentSetting parmEnvironmentSetting)
{
_environmentSettingA = parmEnvironmentSetting;
}
//Dispose...
public void Dispose() { }
//Member variables...
private EnvironmentSetting _environmentSettingA = null;
private DealerRepository _dataDealer = null;
//Get/Set properties...
public DealerRepository Dealer { get { if (_dataDealer == null) { _dataDealer = new DealerRepository(_environmentSettingA); } return _dataDealer; } }
//Functions...
//N/A...
}*/
//Constructor...
public Database(EnvironmentSetting parmEnvironmentSetting)
{
_environmentSettingB = parmEnvironmentSetting;
}
public void Dispose() { } //Dispose...
//Member variables...
private EnvironmentSetting _environmentSettingB = null;
//private DataDealer _dataDealerB = null;
private DealerRepository _dataDealerC = null;
//Get/Set properties...
//public DataDealer Dealer { get { if (_dataDealerB == null) { _dataDealerB = new DataDealer(_environmentSettingB); } return _dataDealerB; } }
public DealerRepository Dealer { get { if (_dataDealerC == null) { _dataDealerC = new DealerRepository(_environmentSettingB); } return _dataDealerC; } }
//Functions...
//N/A...
}
}
namespace zTestcase1.Sample1.Console
{
class Program
{
static void Main(string[] args)
{
EnvironmentSetting environmentSettting = new EnvironmentSetting();
environmentSettting.Application = "DOS Console";
environmentSettting.Database = "Not yet hooked up";
environmentSettting.Emulation = "Development";
Database _libraryDatabase = new Database(environmentSettting);
//var test1 = _libraryDatabase.Dealer.Dealer.DealerProifle_Lookup("Best Dealer Cars Buy");
var test2 = _libraryDatabase.Dealer.DealerProifle_Lookup("Best Dealer Cars Buy");
System.Console.WriteLine(_libraryDatabase.Dealer.EnvironmentResponse() + " [=] " + test2);
System.Console.ReadLine(); //To pause the console...
}
}
}
Answer #1: Instead of ...
//Member variables...
private string _emulation = "";
private string _application = "";
private string _database = "";
//Get/Set properties...
public string Emulation { get {return _emulation;} set {_emulation = value;} }
public string Application { get {return _application;} set {_application = value;} }
public string Database { get {return _database;} set {_database = value;} }
Your properties can be expressed this way instead (it is equivalent, except for the initialization to "", which can be done if needed in a constructor instead):
public string Emulation { get; set; }
public string Application { get; set; }
public string Database { get; set; }
Answer 2/3: Abstractness and inner classes are not used to limit the visibility of classes to outside callers. Use internal instead: this will give other classes within the same library access, while prohibiting access from outside the current library. Though, in this case, it looks as though you are working with the DealerRepository class from the Console application through the _libraryDatabase.Dealer property. I'm not exactly sure what you're trying to achieve here.
If I've understood you correctly, you need dependency injection.
This will require DI-container. E.g., with MEF the code will look like this:
1) assembly "A.dll" (contracts):
public interface IEnvironmentSetting
{
string Emulation { get; }
string Application { get; }
string Database { get; }
}
public interface IDealerRepository
{
string EnvironmentResponse();
string DealerProifle_Lookup(string parmName);
string DealerProfile_Save(string parmName)
}
public interface IDatabase
{
IDealerRepository Dealer { get; }
}
2) Assembly "B.dll" (implementations):
[Export(typeof(IDatabase))]
public class Database : IDatabase, IDisposable
{
[Import]
public IDealerRepository Dealer { get; set; }
// other code here...
}
[Export(typeof(IDealerRepository))]
public class DealerRepository : IDealerRepository, IDisposable
{
[Import]
private IEnvironmentSetting EnvironmentSetting { get; set; }
// other code here...
}
[Export(typeof(IEnvironmentSetting))]
public class EnvironmentSetting : IEnvironmentSetting, IDisposable
{
// other code here...
}
3) Assembly "C.exe" (console application). You need to refer assembly "A.dll" only from "C.exe":
static void Main(string[] args)
{
using (var catalog = // configure composable part catalog here, read MEF docs to find best way for you)
using (var container = new CompositionContainer(catalog))
{
var database = container.GetExportedValue<IDatabase>();
// you're working with IDatabase and IDealerRepository here,
// there's no direct access to the particular implementation classes
database.Dealer.DealerProifle_Lookup("Best Dealer Cars Buy");
// ...
}
}
Using this model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
#region Abstracts definitions
abstract class AnAbstract
{
public string Name { get { return this.GetType().Name; } }
public bool IsNumeric { get { return this is ANumericAbstract; } }
public /*abstract*/ string Description = default(string);
}
abstract class ANumericAbstract : AnAbstract
{
public /*abstract*/ double Min = double.MinValue;
public /*abstract*/ double Max = double.MaxValue;
}
abstract class ANonNumericAbstract : AnAbstract
{
public List<Object> objects = new List<Object>();
}
#endregion Abstracts definitions
#region Concrete definitions
class NumericImpl : ANumericAbstract
{
new public const string Description = "A numeric implementation";
new public const double Min = 0;
new public const double Max = 1000;
public NumericImpl()
{
}
}
abstract class AnotherImpl : ANonNumericAbstract
{
public AnotherImpl()
{
objects.Add("one");
objects.Add("two");
objects.Add("three");
}
}
class SideA : AnotherImpl
{
new public const string Description = "Disc side A";
}
class SideB : AnotherImpl
{
new public const string Description = "Disc side B";
}
#endregion Concrete definitions
partial class Parameter
{
public string Name { get; set; }
public string Description { get; set; }
public bool IsNumeric { get; private set; }
public double Min { get; private set; }
public double Max { get; private set; }
public List<Object> Values { get; private set; }
private Parameter()
{
Values = new List<Object>();
}
}
}
With this, I pretend to define a hierarchy of classes for which I can have some abstract properties (Name,Description,IsNumeric) and at the end of the hierarchy there should be some classes which mandatorily define those properties; in the case of ANumericAbstract they should have additional specific properties, e.g. Min and Max.
Now here's the problem.
I'm attemtping to be able to create instances of Parameter which take a generic AnAbstract and read from it some values to fill in the Parameter properties, à la
Parameter<ANumericAbstract> ParamNum = new Parameter<NumericImpl>();
where a Parameter constructor would take in the passed type and "fill in the blanks". In other words, I'm trying something like:
using System;
namespace ConsoleApplication1 {
partial class Parameter
{
public static Parameter NewParameter<T>() where T : AnAbstract
{
Parameter Parameter = new Parameter();
// THESE DON'T WORK:
this.Name = T.Name;
this.Description = T.Description;
this.IsNumeric = T.IsNumeric;
if (this.IsNumeric)
{
this.Min = (T as ANumericAbstract).Min;
this.Max = (T as ANumericAbstract).Max;
}
else
{
foreach(Object val in (T as ANonNumericAbstract).Values)
{
this.Values.Add(val);
}
}
return Parameter;
}
}
class Program
{
private AnAbstract Number = new NumericImpl();
static void Main(string[] args)
{
}
// THESE DON'T WORK:
private static Parameter<ANumericAbstract> ParameterNum =
Parameter.NewParameter<NumericImpl>();
private static Parameter<ANonNumericAbstract> ParameterA =
Parameter.NewParameter<SideA>();
private static Parameter<ANonNumericAbstract> ParameterB =
Parameter.NewParameter<SideB>();
}
}
Obviously the syntax is invalid, but I'm not sure if I'm going in the right direction. Is there some Generics syntax that I'm not using properly? Should I just be done with it and use Getters and Setters à la Java? :-) At this point, just doing the
Parameter par = new Parameter { Name = NumericImpl.Name, /* ... */ };
might seem more sensible...
Firstly you should not use New keyword on your properties.
Consider virtual keyword:
abstract class AnAbstract
{
public virtual string Name { get { return this.GetType().Name; } }
public virtual string Description { get { return String.Empty; } }
}
abstract class ANumericAbstract : AnAbstract
{
public virtual double Min = double.MinValue;
}
class NumericImpl : ANumericAbstract
{
public override string Description { get { return "A numeric implementation"; } }
public override double Min { get { return 0; } }
}
1) You can place an instance of you type in Parameter constructor and have Parameter instance.
partial class Parameter
{
public Parameter(AnAbstract inputObject)
{
this.Name = inputObject.Name;
// etc
}
}
private static Parameter ParameterNum = new Parameter(new NumericImpl());
2) The second way is to use reflection to create an instance of object with initial parameters.
partial class Parameter<T> where T : AnAbstract
{
public static Parameter<T> NewParameter<T>() where T : AnAbstract
{
Parameter<T> parameter = new Parameter<T>();
AnAbstract instance = (AnAbstract)Activator.CreateInstance(typeof(T));
parameter.Name = instance.Name;
// etc
return parameter;
}
}
private static Parameter<NumericImpl> ParameterNum =
Parameter<NumericImpl>.NewParameter();
3) Make Parameter class static and create in via static constructor.
static partial class Parameter<T> where T : AnAbstract
{
public static string Name { get; set; }
//etc
}
static partial class Parameter<T> where T : AnAbstract
{
static Parameter ()
{
AnAbstract instance = (AnAbstract)Activator.CreateInstance(typeof(T));
Parameter<T>.Name = instance.Name;
//etc
}
}
In the last example you can use this class like this:
String someName = Parameter<NumericImpl>.Name;