how to set template in c# - c#

how do I set id in Sprite from the template templa
public class templa<T> where T : class , new(){
public T CreateMe(){
T temp = new T();
shortInt si
si.Set(2, 2);
//T needs to have id as shortInt else it won't work
temp.id = si; // temp = si// doesn't work either
return temp;
}
}
public struct shortInt{
public short s;
public int i;
public void Set(short s, int i){
this.s = s;
this.i = i;
}
}
Sprite here has id this is what I'm trying to achieve to change id with template
public class Sprite{
public Sprite(){}
public shortInt id;
set{
id = value;
}
}
I was searching the web and found only do it with { get; set; } I'm clueless here

using where(generic type constraint)
code like this
public class Templat<T> where T :
Sprite, new() //constraint in here
{
public T CreateMe()
{
T temp = new T();
shortInt si = new shortInt();
si.Set(2, 2);
temp.id = si;
return temp;
}
}
public struct shortInt
{
public short s;
public int i;
public void Set(short s, int i)
{
this.s = s;
this.i = i;
}
}
public class Sprite
{
public Sprite() { }
public shortInt id { get; set; }
}

Related

Get the typed object with pattern factory

see my code :
public interface IStructureType
{
int Longueur { get; set; }
int Position { get; set; }
int CompleterCodeBy { get; set; }
}
public abstract class StructureTypeFactory
{
public abstract IStructureType GetStructureType(string type);
}
public class ConcreteStructureTypeFactory : StructureTypeFactory
{
public override IStructureType GetStructureType(string type)
{
switch(type)
{
case "StructureCodeMagasin":
return new StructureCodeMagasin();
case "StructureChrono":
return new StructureChrono();
case "StructureLotSimple":
return new StructureLotSimple();
default:
throw new ApplicationException("");
}
}
}
public class StructureCodeMagasin : IStructureType
{
public int Longueur { get ; set; }
public int Position { get; set; }
public int CompleterCodeBy { get { return 2; } set { CompleterCodeBy = value; } }
public void GetCodeMagasin()
{
//some code
}
}
I try to use Factory pattern, but how I can access to method GetCodeMagasin in this example :
public MainWindow()
{
InitializeComponent();
StructureTypeFactory st = new ConcreteStructureTypeFactory();
var structure = st.GetStructureType("StructureCodeMagasin");
int longueur = structure.CompleterCodeBy;
}
I can access properties but no method, I would like structure variable will typed StructureCodeMagasin.
Thanks for help

Access class variable from another newly created class

I have code like this:
class First
{
public int a { get; set; }
public int b { get; set; }
public int c = 2;
public _second;
public First()
{
_second = new Second(c);
this.a = _second.a;
this.b = _second.b;
}
}
class Second
{
public int a;
public int b;
public Second(int c)
{
if(c == 0)
{
a = 1;
b = 2;
}
else
{
a = -1;
b = -2;
}
}
}
How can I pass a and b from First class into second class and then directly from second class set their values withohut using static as declaration in First class.
I have tried this:
class First
{
public int a;
public int b;
public int c = 2;
public _second;
public First()
{
_second = new Second(a, b, c);
}
}
class Second
{
public Second(int a, int b, int c)
{
if(c == 0)
{
a = 1;
b = 2;
}
else
{
a = -1;
b = -2;
}
}
}
but it is not doing the job.
You can simply add a constructor in the Second class that receives the instance of First and updates the public variables of the instance passed
public class Second
{
public Second(int a, int b, int c)
{
// old constructor if still needed
...
}
public Second(First f)
{
int sign = f.c == 0 ? 1 : -1;
f.a = 1 * sign;
f.b = 2 * sign;
}
}
By the way, I suggest to use properties instead of public fieds.
public class First
{
public int a {get;set;}
public int b {get;set;}
public int c {get;set;} = 2;
public Second _second {get;set;}
public First()
{
_second = new Second(this);
}
}
UPDATE
Looking at your image it is clear what is the source of your problem. You are receiving a Form class instance in your Forma constructor. You need to receive a Main instance like
public Forma(Main form, int modulID)
{
.....
}
The base class Form has no knowledge of methods defined in custom form classes. In alternative you can still receive a Form instance but you need to add something like this
public Forma(Form form, int modulID)
{
Main m = form as Main;
if(m != null)
m.helpWindow = new Help(modulID);
}
.....
}
You can pass the fields as ref meaning that you pass a reference to the field passed as parameter not the value. This means changes to the fields inside the Second constructor will be reflected in the First class
class First
{
public int a;
public int b;
public int c = 2;
public Second _second;
public First()
{
_second = new Second(ref a, ref b, c);
}
}
class Second
{
public Second(ref int a, ref int b, int c)
{
if (c == 0)
{
a = 1;
b = 2;
}
else
{
a = -1;
b = -2;
}
}
}
You can pass by ref to any method not just a constructor. You should read more about this topic, here for example
I accomplished it with Interface.
public interface IClass
{
int a { get; set; }
int b { get; set; }
}
class First : IClass
{
public int a { get; set; }
public int b { get; set; }
public int c = 2;
public _second;
public First()
{
_second = new Second(this, c);
}
}
class Second
{
public Second(IClass ic, int c)
{
if(c == 0)
{
ic.a = 1;
ic.b = 2;
}
else
{
ic.a = -1;
ic.b = -2;
}
}
}

How to use Collection in loop

Small doubt, In Update method I had int count, let say count == 4, then i am using for loop to get id, version and set.
In this case Id,version and set values getting only the last value, but how to get the all the values
I tried but i feel its wrong and not working,
Created a seperate list for id, version and set,
eg: _details.imageList.Add(logoHeader.LogoID);
public void Updates(AUnit _aUnit, int Id)
{
ImageDetails _details = new ImageDetails(_aUnit, Id);
int count = (int) _aUnit.ReadBits(8);
for (int i = 0; i < (int) count; i++)
{
_details.ID = (int) _aUnit.ReadBits(8);
_details.Version = (int) _aUnit.ReadBits(8);
_details.set = (int) _aUnit.ReadBits(24);
}
_details.Rset = _aUnit.Buffer.Skip(10).Take(_details.set).ToArray();
//MemoryStream ms = new MemoryStream(_details.PortrateImages.First());
//Image image = Image.FromStream(ms);
//Bitmap bmp = new Bitmap(image);
_details.UpdateTime = DateTime.Now.ToString("h:mm:ss tt");
newData.Add(_details);
}
public class ImageDetails
{
public ImageDetails(AUnit _au, int carouselId)
{
carId = carouselId;
_AUnit = _au;
_updateTime = "";
}
private string _updateTime;
public int ID { get; set; }
public int Version { get; set; }
public int set { get; set; }
public int carId { get; set; }
public byte[] Rset { get; set; }
public AUnit _AUnit { get; set; }
public byte[] bytes { get; set; }
public List<byte[]> dataArray = new List<byte[]>();
public string UpdateTime
{
get { return _updateTime; }
set { _updateTime = value; }
}
public List<byte[]> PImages
{
get
{
List<byte[]> Plogos = new List<byte[]>();
if (carId == 2)
{
Plogos.Add(Rset);
}
return Plogos;
}
}
public List<byte[]> LImages
{
get
{
List<byte[]> Llogos = new List<byte[]>();
if (carId == 1)
{
Llogos.Add(Rset);
}
return Llogos;
}
}
}
You could make detail an object(class) and make a list of details.
like,
public class Detail
{
public int Id{get;set;}
public int Version{get;set;}
public int set{get;set;}
}
and make a list of detail like,
var Details = new List<Detail>();
and you could access it using the foreach loop. like,
foreach(var detail in Details){
Console.log(detail.Id)
.....
}
hope this helps.

Implementing tree structure on c#

I have an object myBook.
Can I implement a better structure for that kind of data?
public class myRow{
public int ID = 0;
public int number = 0;
public String param1 = null;
public decimal param2 = null;
public string parm3 = "";
public int param4 = null;
}
public class mySubChapter{
public int ID = 0;
public string title = "";
public List<myRow> rows;
internal bool sort(){...} //sort rows by ID
}
public class myChapter{
public int ID = 0;
public string title = "";
public List<mySubChapter> subChapters;
internal bool sort(){...} //sort subChapters by ID
}
public class myBook{
public int ID = 0;
public string title = ""
public List<myChapter> chapters;
internal bool sort(){...} //sort chapters by ID
}
If you really want to model your book structure in a tree, you could use a generic tree implementation like the one presented here. Then, you could form a tree using code like this
DTreeNode<string> root = new DTreeNode<string>();
DTreeNode<string> temp;
temp = root.Nodes.Add("Hello");
temp.Nodes.Add("olleH");
temp = root.Nodes.Add("World");
temp.Nodes.AddRange(new string[]
{ "dWorl", "ldWor", "rldWo", "orldW" } );
In my opinion, I'll merge subchapter and chapper class into one myChaper class and add new property is chapterLevel in it. Because I think subChapter is a chapter too with just difference level(children of chapter may be). Sorry for my English.
public class myRow{
public int ID = 0;
public int number = 0;
public String param1 = null;
public decimal param2 = null;
public string parm3 = "";
public int param4 = null;
}
public class myChapter{
public int ID = 0;
public string title = "";
public int chapterLevel = 0;
internal bool sort(){...} //sort chapters by ID and level
}
public class myBook{
public int ID = 0;
public string title = ""
public List<myChapter> chapters;
internal bool sort(){...} //sort chapters by ID
}
Another tree implementation:
public interface INode
{
int Id { get; set; }
INode Parent { get; }
ReadOnlyCollection<INode> Children { get; }
void SetParent(INode node);
void AddChild(INode node);
}
public class Node : INode
{
private INode _parent;
private IList<INode> _children;
public Node()
{
_children = new List<INode>();
}
public int Id { get; set; }
public INode Parent
{
get { return _parent; }
}
public ReadOnlyCollection<INode> Children
{
get
{
return new ReadOnlyCollection<INode>
(_children.OrderBy(c => c.Id).ToList());
}
}
public virtual void AddNode(INode node)
{
_children.Add(node);
node.SetParent(this);
}
public virtual void SetParent(INode node)
{
_parent = node;
}
}
The classes, Row, Chapter, Book can derive from the Node class, e.g.
public class Book : Node
{
public override void SetParent(INode node)
{
throw new InvalidOperationException();
}
public string Title { get; set; }
}

Should a class have string fields for values from SQL JOIN from dictionary tables

I have a class (code below) that I use to save to and read from db. Everything works fine but when it comes to finally print some object information taken from dictionary tables I really don't know where to put them. (Active Record).
Code of class:
class Object
{
public int id;
public int size;
public int color;
public int author;
public Object(int id, int size, int color, int author)
{
this.id = id;
this.size = size;
this.color = color;
this.author = author;
}
// add, update, delete methods
}
So for above class the SQL:
select id, size, color, author from object;
Should I add string fields into this class to look like this:
class Object
{
public int id;
public int size;
public int color;
public int author;
// String fields for dictionary
public string sizeString;
public string colorString;
public string authorString;
//
// Nr 1
public Object(int id, int size, int color, int author)
{
this.id = id;
this.size = size;
this.color = color;
this.author = author;
}
// Nr 2
public Object(int id, string size, string color, string author)
{
this.id = id;
this.size = sizeString;
this.color = colorString;
this.author = authorString;
}
// add, update, delete methods
}
SQL:
select o.id, s.size, c.color, a.name
from object o
join sizes s on o.size = s.id
join colors c on o.color = c.id
join authors a on o.author = a.id
If this approach is correct then should my new constructor (Nr 2) look like above I mean should I left the int fields empty or always get all data from db:
public Object(int id, int size, int color, int author,
string sizeString, string colorString,
string authorString)
{
this.id = id;
this.size = size;
this.color = color;
this.author = author;
this.sizeString = sizeString;
this.colorString = colorString;
this.authorString = authorString;
}
SQL:
select o.id, o.size, o.color, o.author,
s.size as sizeS, c.color as colorS, a.name as authorS
from object o
join sizes s on o.size = s.id
join colors c on o.color = c.id
join authors a on o.author = a.id
If whole idea of adding addtional string fields is bad please steer me in a right direction. Thanks for help.
You can put in items which resolve the ids to their corresponding values such as below:
using System;
using System.Collections.Generic;
namespace StackOverflow
{
class Program
{
static void Main(string[] args)
{
IColorResolver colors = new ColorResolver();
IObject demoObject1 = new Object(1, 1, 1, 1, colors);
IObject demoObject2 = new Object(2, 3, 3, 3, colors);
Console.WriteLine("demoObject1: {0}", demoObject1.Color.Name);
Console.WriteLine("demoObject2: {0}", demoObject2.Color.Name);
Console.ReadKey();
}
}
public interface IObject
{
int Id { get; }
ISize Size { get; set; }
IColor Color { get; set; }
IAuthor Author { get; set; }
}
public class Object: IObject
{
bool isDirty = false;
readonly int id;
int size;
int color;
int author;
IColorResolver colors;
public int Id { get { return this.id; } }
public ISize Size { get; set; } //this would implement code like Color's
public IAuthor Author { get; set; }//this would implement code like Color's
public IColor Color
{
get { return colors.GetColor(color); }
set
{
if (!this.color.Equals(value.Id))
{
this.color = value.Id;
this.isDirty = true;
}
}
}
public Object(int id, int size, int color, int author, IColorResolver colorResolver)
{
this.id = id;
this.size = size;
this.color = color;
this.author = author;
this.colors = colorResolver;
}
// add, update, delete methods
}
public interface ILookupValue
{
int Id { get; }
string Name { get; /*set;*/ } //no set since this is a lookup so we don't want to amend it
}
public interface IColor: ILookupValue
{
IColorResolver GetResolver();
}
public interface IAuthor : ILookupValue { /* ... */ }
public interface ISize : ILookupValue { /* ... */ }
public class Color : IColor
{
int id;
string name;
IColorResolver colors;
public int Id { get { return this.id; } }
public string Name { get { return this.name; } }
public Color(int id, string name, IColorResolver colors)
{
this.id = id;
this.name = name;
this.colors = colors;
this.colors.AddColor(this);
}
public IColorResolver GetResolver() { return this.colors; }
}
public interface IColorResolver
{
IColor GetColor(int id);
void AddColor(IColor color);
}
public class ColorResolver : IColorResolver
{
IDictionary<int, IColor> colors = new Dictionary<int, IColor>();
public ColorResolver()
{
/*
in reality you'd probably pass a data layer object through
the constructor to fetch these values from your database
*/
new Color(1, "Red", this);
new Color(2, "Green", this);
new Color(3, "Blue", this);
}
public void AddColor(IColor color)
{
this.colors.Add(color.Id, color);
}
public IColor GetColor(int id)
{
IColor result;
this.colors.TryGetValue(id, out result); //you could throw an exception here if not found
return result;
}
}
}
The reason for the huge amount of interfaces in the above code is this makes testing simpler; i.e. I can create Mock objects for any of my objects and pass them in instead of my real objects.

Categories

Resources