|
I have done this, and it works particularly well when dealing with database fields. One thing that I have found particularly useful with this pattern is to include implicit type conversions to and from string, so that passing the objects to standard framework functions becomes a breeze. It also makes initialisation simple and intuitive
Email myAddress = "mick@midimick.com";
System.Net.Mail.MailAddress mailAddress = new System.Net.Mail.MailAddress(myAddress);
|
|
|
|
|
Very interesting post, Sascha. I was hoping Marc Clifton would post in this thread, since he's done some interesting work in this area.
What do you think about using T4 to generate semantic types ?
cheers, Bill
«In art as in science there is no delight without the detail ... Let me repeat that unless these are thoroughly understood and remembered, all “general ideas” (so easily acquired, so profitably resold) must necessarily remain but worn passports allowing their bearers short cuts from one area of ignorance to another.» Vladimir Nabokov, commentary on translation of “Eugene Onegin.”
|
|
|
|
|
Bill, I have to admit: I've never "actively" used T4 What I need for code generation I do with some own generators, without VS-integration.
I guess for my outlined purpose here it would make sense - but it's almost as quickly done by copying an existing class file and renaming the file and the class.
BillWoodruff wrote: I was hoping Marc Clifton would post in this thread I sure would welcome him to do so - but I don't think he's a regular in these forums, is he?
cheers, Sascha
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
 Hi Sascha, I'm in the process of studying T4 in depth (steep learning curve ... for me). But, I can see the power available by using it. fyi: here's an experimental design-time .tt file that will produce a new .cs file with a series of "nested" Classes built from iterating a List of Strings. Each Class except the first in the List will inherit from the previous Class in the List, and each Class will define a Property to hold a List of the Class that inherits from it ... except for the last Class in the List which has no sub-Class. An interesting experiment, but don't me ask me what it might be good for
The design-time .tt file:
<#@ template debug="true" hostSpecific="true"#>
<#@ output extension=".cs"#>
<#@ Assembly Name="System.Core"#>
<#@ Assembly Name="System.Windows.Forms"#>
<#@ import namespace="System"#>
<#@ import namespace="System.IO"#>
<#@ import namespace="System.Diagnostics"#>
<#@ import namespace="System.Linq"#>
<#@ import namespace="System.Collections"#>
<#@ import namespace="System.Collections.Generic"#>
<#
List<string> categories = new List<string>{"Life","Domain", "Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species"};
#>
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestNameSpace
{
public class Life
{
public string Name { set; get; }
public List<Domain> Domains{ set; get; }
public Life(string name = "Life", params string[] args)
{
Name = name;
Domains = new List<Domain>();
foreach(string str in args) Domains.Add(new Domain(str));
}
<#
for (int i = 1; i < categories.Count; i++)
{
#>public class <#=categories[i]#>: <#=categories[i - 1]#>
{
<#if(i < categories.Count - 1)
{
#>
public List< <#=categories[i + 1]#>> <#=categories[i + 1]#>s = new List< <#=categories[i + 1]#>>();
<#}#>
public <#=categories[i]#>(string name = "", params string[] args)
{
Name = name;
<#if(i < categories.Count - 1)
{
#>
foreach(string str in args)
{
<#=categories[i +1]#>s.Add(new <#=categories[i + 1]#>(str));
}
<#}#>
}
}
<#}
#>
}
} When processed by the compiler's TextTemplatingTool, this generates the following "live" class structure:
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestNameSpace
{
public class Life
{
public Life(string name = "Life", params string[] args)
{
Name = name;
Domains = new List<Domain>();
foreach (string str in args) Domains.Add(new Domain(str));
}
public string Name { set; get; }
public List<Domain> Domains { set; get; }
public class Domain : Life
{
public List<Kingdom> Kingdoms = new List<Kingdom>();
public Domain(string name = "", params string[] args)
{
Name = name;
foreach (string str in args)
{
Kingdoms.Add(new Kingdom(str));
}
}
}
public class Kingdom : Domain
{
public List<Phylum> Phylums = new List<Phylum>();
public Kingdom(string name = "", params string[] args)
{
Name = name;
foreach (string str in args)
{
Phylums.Add(new Phylum(str));
}
}
}
public class Phylum : Kingdom
{
public List<Class> Classs = new List<Class>();
public Phylum(string name = "", params string[] args)
{
Name = name;
foreach (string str in args)
{
Classs.Add(new Class(str));
}
}
}
public class Class : Phylum
{
public List<Order> Orders = new List<Order>();
public Class(string name = "", params string[] args)
{
Name = name;
foreach (string str in args)
{
Orders.Add(new Order(str));
}
}
}
public class Order : Class
{
public List<Family> Familys = new List<Family>();
public Order(string name = "", params string[] args)
{
Name = name;
foreach (string str in args)
{
Familys.Add(new Family(str));
}
}
}
public class Family : Order
{
public List<Genus> Genuss = new List<Genus>();
public Family(string name = "", params string[] args)
{
Name = name;
foreach (string str in args)
{
Genuss.Add(new Genus(str));
}
}
}
public class Genus : Family
{
public List<Species> Speciess = new List<Species>();
public Genus(string name = "", params string[] args)
{
Name = name;
foreach (string str in args)
{
Speciess.Add(new Species(str));
}
}
}
public class Species : Genus
{
public Species(string name = "", params string[] args)
{
Name = name;
}
}
}
}
«In art as in science there is no delight without the detail ... Let me repeat that unless these are thoroughly understood and remembered, all “general ideas” (so easily acquired, so profitably resold) must necessarily remain but worn passports allowing their bearers short cuts from one area of ignorance to another.» Vladimir Nabokov, commentary on translation of “Eugene Onegin.”
modified 15-Feb-16 14:43pm.
|
|
|
|
|
Thank you, Bill, that's a nice example. I've bookmarked it in case I'll ever feel the need to learn T4 (or in case I change profession to biologist )
Do you learn it because you need it for something?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Hi,
I am using HP DEKJET INK ADVATAGE4645 PRINTER TO scan documents
by using my program.
after scanning a page if any barcode is there i need to decode that barcode using Decoding Methods.
I am using One third party tool ImageaMan.
after scanning some documents i am unable to decode a barcode on page.
If I press the option RestoreToDefault option On Scaneer again it is working fine.
so how to RestoreToDefault settings of a scaner using .net code...?
sunanda
ph:07760675056
email:sunanda1989@gmail.com,sunanda.lankipalli@cgi.com
|
|
|
|
|
Never post your email address in any forum, unless you really like spam! If anyone replies to you, you will receive an email to let you know.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
avoid blinking screen+window form application c#
|
|
|
|
|
Okay, I will. How?
As it stands, your question is far too vague to get anything remotely resembling a full answer to solve your issue. You need to fully define your problem, what you have tried and what the relevant code section looks like.
This space for rent
|
|
|
|
|
As Pete says, that isn't a question we can answer as it stands - we have no idea what you are talking about other than 1) it's a C# WinForms app, and 2) the screen is probably "blinking".
The trouble is that we have no idea what you app does, or how it does it, and we can't see your screen, access your HDD, or read your mind!
So explain what you are doing, what problem you are having, and try to give us relevant code fragments to work with.
At the moment, we can't even begin to help you!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
|
|
Yes ... because it can cause fits in some people.
|
|
|
|
|
i am looking for code example which teach me how abstract class can be use to version class /component. please discuss with code example or redirect me to right article. thanks
tbhattacharjee
|
|
|
|
|
|
i searched google but unfortunately found no good one write up with code example for implement class /component Versioning using abstract class in c#.
tbhattacharjee
|
|
|
|
|
|
Hi All.
I would like to ask how to read variable with period of time or if this variable has change.
|
|
|
|
|
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
And at the moment I have no idea what you are trying to work out!
Try expanding on what you have told us - because at the moment we can't help you.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hi.
I'm sorry from my negligence.
I create Windows Forms application. In this application I would like to read data form PLC (Real Variable).
Now when I run this program I read value but only one time.
In my PLC I see that my variable has change but in C# application not.
In conclusion I would like to read my variable continuously or if it change.
My Code:
private void Form1_Load(object sender, EventArgs e)
{
SIS7.Net.Channel myChannel = new SIS7.Net.Channel();
SIS7.Device myDevice = new SIS7.Device("10.228.216.156,0,1", SIS7.Model.S7_1200);
SIS7.Group myGroup = new SIS7.Group(false, 500);
SIS7.Item myItem = new SIS7.Item("DB4.DBD68", 1, SIS7.DataType.Int32);
SIS7.Item myItem1 = new SIS7.Item("DB4.DBD100", 1, SIS7.DataType.Single);
myChannel.Devices.Add(myDevice);
myDevice.Groups.Add(myGroup);
myGroup.Items.Add(myItem);
myGroup.Items.Add(myItem1);
System.Single myFloat32;
try
{
myItem.Read();
myItem1.Read();
textBoxItemRead.Text = myItem.Values[0].ToString();
textBox1.Text = myItem1.Values[0].ToString();
}
catch (Exception ex)
{
}
myChannel.Dispose();
}
Regards
Pawel
|
|
|
|
|
Well...that code reads from the PLC only once (when the form loads) and then closes and Disposes your channel immediately.
So when the form is displayed, the PLC isn't communicating with the application at all - and couldn't if it wanted to because the connection isn't there any more.
Probably, you want to move that into a different thread, and make it loop to read the values, and then update the textboxes.
I'd suggest that you look at a BackgroundWorker[^] as that allows you to use the Progress to pass the messages back to the main thread for display (you can't change UI items from any thread except the main thread) via the BackgroundWorker.ProgressChanged Event (System.ComponentModel)[^] - the ProgressChangedEventArgs contains a UserState property you can transfer it in.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I need to transfer files to/from a server. I've done FTP in C# before but never used SCP.
From what I can find in Google, there isn't a lot available about using SCP in C#.
- Can anyone shed some light on SCP?
- Any reason not to use FTP assuming I encrypt the files before transfer?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
SCP[^] is an earlier and more primitive incarnation of the SFTP[^] protocol.
(Not to be confused with FTPS[^], which is just standard FTP over SSL.)
SCP, SFTP or FTPS would likely be a better choice than rolling your own encryption code. They've been written by experts and battle-tested by thousands of users over several decades.
The BCL has built-in support for FTPS via the FtpWebRequest.EnableSsl property[^]. For SFTP or SCP, you'll need a third-party library:
- SSH.NET[^] provides SCP and SFTP support.
- WinSCP[^] also has a .NET / COM wrapper.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Excellent info.. Thank you!
If it's not broken, fix it until it is
|
|
|
|
|
This kind of answer is what makes me scan every answer in this forum. Always nice to learn something new
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|