|
Sounds like a good idea to me.
Would it make sense to use a struct rather than a class ? That way, you'd have less memory overhead and GC churn.
I'd be inclined to use a read-only property instead of a field, but only because it "feels" cleaner.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: Would it make sense to use a struct rather than a class ? That way, you'd have less memory overhead and GC churn. The types for which I would use these semantic name types belong to a type hierarchy and are polymorphically used in several places; so with structs I would probably have to have several new method overloads which I could prevent with classes (and sub-classes) instead of structs. Also I'm guessing that the performance degradation will be insignificant because although those name lookups happen at a lot of places it's anything but the "main business". I'll probably profile both ways
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Opinion on how to do it or opinion if it is worthwhile and valid?
|
|
|
|
|
RugbyLeague wrote: Opinion on how to do it or opinion if it is worthwhile and valid? The latter. As I haven't seen this done anywhere yet I thought maybe I'm missing something which is why it's not being done.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
If you think it will bring something to your code - compile time checks etc. then go for it.
|
|
|
|
|
There are several articles here on CP about using Semantic Types in .NET. You might want to take a look at some of these[^] (I have read the first three articles before and they are all really enjoyable).
This space for rent
|
|
|
|
|
I've read those (more or less thoroughly) - what made me ask my question anyway is the fact that those articles deal with semantic types for "real world entities" (ZipCode, EmailAddress, etc.) including conversions or validity checking or formatting - while I would just need a surrogate for something like
typedef string NameOfSomeType
for application-internal use only.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Addressing the requirement of looking up "object type names":
class Program {
private static List<Type> _types = new List<Type>();
static void Main( string[] args ) {
_types.Add( typeof( DateTime ) );
_types.Add( typeof( int ) );
_types.Add( typeof( bool ) );
double d = 1.0D;
Register( d );
int index = _types.IndexOf( d.GetType() );
Console.Write( index );
Console.ReadKey();
}
static void Register( object o ) {
_types.Add( o.GetType() );
}
}
|
|
|
|
|
Thank you - but you misunderstood: It's not about looking up types by their type or type-name but about looking up objects by a given name (like "bob"). And because there are several different types of those objects it would seem to make sense to have their names wrapped in dedicated semantic types instead of just passing strings around in order to avoid passing a name of an object of type A where the name of an object of type B is expected.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I see no restriction on "names" then; 2 objects could have the same name.
Sounds like you need an object "IDentity" property.
|
|
|
|
|
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
|
|
|
|
|