|
That example doesn't really depend on it being a struct, it's about the implicit conversion operator.
Here's a minimal implementation of a class to implicitly converts to and from string:
class CustomString
{
public static implicit operator CustomString(string s)
{
return new CustomString();
}
public static implicit operator string(CustomString s)
{
return "";
}
}
Do that for both subclasses.
By the way, as far as I can tell you should be able to use is[^] instead of the GetType() == typeof() "not-quite-anti-pattern-but-close".
|
|
|
|
|
Thanks, this seems to be just what I need, though I imagine that I need to save the actual string value and have the constructor set that value and the operator string(CustomString s) return the string instead of an empty string?
class CustomString
{
private string _value;
private CustomString(string s)
{
_value = s;
}
public static implicit operator CustomString(string s)
{
return new CustomString(s);
}
public static implicit operator string(CustomString s)
{
return s._value;
}
}
Would I also be able to implement INotifyPropertyChanged with this class? I don't see how as there's no public property. Would I just use "_value" even though it's private?
|
|
|
|
|
You could just give it a public property though, right?
|
|
|
|
|
There wouldn't be a whole lot of point in implementing the interface if he doesn't!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
So the idea would be to include a public property, but still have the implicit operator act on that property? Would that then allow someone to either use the implicit operator or the property directly?
|
|
|
|
|
Yes, is it giving you any trouble?
|
|
|
|
|
No, I just didn't even think about this, or even know that it was possible. I'm reading more about `implicit`, which seems very useful. It doesn't seem to be touched on in most 'beginner' reading, though.
|
|
|
|
|
I'm not quite sure what you're trying to achieve here. I think it is that you want a data object that has a 'number' (a string identifier) and also a type. Using GetType or is/as functions is generally a sign that you haven't quite got the solution you want; switching based on type usually implies you need more polymorphic behaviour.
I think the better solution is
enum ItemType { Serial, Lot };
struct ObjectIdentifier {
string Identity { get; set; }
ItemType Type { get; set; }
}
List<ObjectIdentifier> mySLNList = ...;
foreach(ObjectIdentifier oi in mySLNList)
switch(oi.Type)
...
|
|
|
|
|
What would a good book to start learning C#? I'm not looking for theory and such, but more of practical use of code. There are SO MANY and don't know which/how to choose.
Thanks
|
|
|
|
|
|
|
According to the review[^], it should be fine.
|
|
|
|
|
Pretty much any of them, provided they don;t have multiple exclamation marks, "in xxx days", or "for dummies" in the title. Wrox do good ones, and so do Adison-Wesley.
This is a good one: Pro C# 5.0 and the .Net 4.5 Framework[^] - I started with the .NET 2.0 version and it's pretty easy to follow.
If you want "practical use of code" then you need to concentrate on the .NET framework rather than the language: if you have a good grounding in other non-Net languages, then you can pick up pretty much the whole of C#-the-language in an afternoon. The .NET framework on the other hand is months of learning - and you can't use C# effectively without it.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Thanks. But that book says it's intended NOT for beginners...
|
|
|
|
|
Then read the book, you won't be a beginner anymore so you can read the book.
|
|
|
|
|
Depends what you are a beginner in!
If this is your first computing language, then no, it isn't suitable - but then you'd be silly to ignore the theory because you need that to make the "practical use of code" understandable in a wider context.
Computing isn't like cooking: you don't just follow a recipe, you have to understand what the ingredients do, and why you want to use rice flour instead of strong white bread flour, for example. If you don't understand that, then you will use the wrong ingredient and produce something that is only technically food...or code that is only technically an application!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
|
Hello everyone
I'm trying to solve this problem but i can't get it. Maybe someone can help me solve this problem please...
When I compile the error says:
Quote: Error 'System.Collections.Generic.Dictionary<string,nhibernate.isessionfactory>' does not contain a definition for 'get' and no extension method 'get' accepting a first argument of type 'System.Collections.Generic.Dictionary<string,nhibernate.isessionfactory>' could be found (are you missing a using directive or an assembly reference?)
protected static ISessionFactory getSessionFactory(string configFile)
{
if (null == configFile)
{
if (sessionFactory == null)
{
throw new Exception("The session factory has not been initialized (or an error occurred during initialization)");
}
else
{
return sessionFactory;
}
}
else
{
if (sessionFactoryMap == null)
{
throw new Exception("The session factory for '" + configFile + "' has not been initialized (or an error occurred during initialization)");
}
else
{
ISessionFactory sf = (ISessionFactory) sessionFactoryMap.get(configFile);
if (null == sf)
{
throw new Exception("The session factory for '" + configFile + "' has not been initialized (or an error occured during initialization)");
}
else
{
return sf;
}
}
}
}
Thanks in advance
|
|
|
|
|
It means there's no method name "get" on the ISessionFactory. Is the code from Java?
Try (ISessionFactory)sessionFactoryMap[configFile];
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thanks for your help a lot, and actually it is from java code
|
|
|
|
|
You're welcome
|
|
|
|
|
Can I please ask you for another help if you don't mind
|
|
|
|
|
In this part of code im having error on get() and set() method...
<blockquote class="FQ"><div class="FQA">Quote:</div>protected static ISession getSession(string configFile, bool createNew)
{
if (createNew)
{
return getSessionFactory(configFile).OpenSession();
}
else
{
if (null == configFile)
{
if (null == sessions)
{
sessions = new ThreadLocal<ISession>();
}
ISession session = sessions.get();
if (null == session || !session.IsOpen)
{
session = getSessionFactory(null).OpenSession();
session.set(session);
}
return session;
}
else
{
if (null == mappedSessions)
{
mappedSessions = new ThreadLocal<IDictionary>();
}
Dictionary<string, ISession> map = mappedSessions.get();
if (null == map)
{
map = new Dictionary<string, ISession>(1);
mappedSessions.set(map);
}
ISession session = map[configFile];
if (null == session || !session.IsOpen)
{
session = getSessionFactory(configFile).OpenSession();
map.Add(configFile, session);
}
return session;
}
}
}</blockquote>
|
|
|
|
|
According to MSDN, the ThreadLocal[^] class has a "Value" property that looks promising. ISessions session = sessions.Value?
Properties in C# do not need to explicitly call a getter/setter.
What kind of session is being created? Google is betting on NHibernate
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|