|
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[^]
|
|
|
|
|
|
Google for some C# example with NHibernate doing stuff with ISession and OpenSession; there's not much examples, but any that works would do
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I posted on this a few months ago, and I need to revisit it...
I have a typical n-tier app - DAL, BL, WebAPI, Controllers, UI, etc.
I've seen many discussions on how to let the UI know that an exception happened in the back end. All have both good & bad points.
If you put a TRY/CATCH in the DAL, log the error, then what..??? Throw again and catch it again in a UI side TRY/CATCH?
What about situations where it's NOT an exception, but the UI needs to know something, like a duplicate record problem, or a record in use and can't be deleted problem? Would you throw for these also?
What I WAS doing was passing back a custom response object from each DAL method. My Response class:
[DataContract]
[Serializable]
public class _ResponseBase
{
[DataMember]
public Result Result { get; set; }
[DataMember]
public string Message { get; set; }
private Exception _Exception;
[DataMember]
public Exception Exception
{
get { return _Exception; }
set
{
if (_Exception != value)
{
_Exception = value;
Result = Result.Failure;
Message = _Exception.Message;
}
}
}
public _ResponseBase()
{
Result = Result.Success;
Message = string.Empty;
Exception = null;
}
}
This class allows you to pass back Result.Success with the data from the DAL, or Result.Failure with a message such "Duplicate record", or Result.Failure with an Exception.
Then in the UI's base View Model I have a method called HandleResponse that is called like:
var response = Engine.APIProxy.GetLookups(AppConstants.TRAINING_CATEGORIES);
if (response.Result == Result.Success)
{
Categories = new ObservableCollection<LookupEntity>(response.Data);
}
else
{
HandleResponse(response);
}
and the HandleResponse method:
protected void HandleResponse(_ResponseBase response)
{
if (response.Exception == null)
{
if (!string.IsNullOrEmpty(response.Message))
{
MessageBox.Show(response.Message, "Response", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
throw new ArgumentException("The response did not contain an exception or a message");
}
}
else
{
logException(response.Exception);
throw response.Exception;
}
}
There is a WPF UI, a WPF Tablet App, and a Web App that all go through the Web API, so having a consistent response object returned is very helpful, although it does mean a bit more UI code.
So far this works well, but I'd like to get your opinion. Do you have a better way?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
I prefer the general "don't catch it if you're not going to do anything with it" theory. Let it bubble up unmolested until something can actually handle it.
Just above that, is the "catch it, add relevent information to the Data collection, and rethrow it" theory.
As regards database operations, I like to add the SQL statement, parameters, and maybe connection information.
You can also add a timestamp and other information -- too much is better than too little.
Such information should not be presented to the user of course.
In some cases, it makes sense to catch the exception and wrap it in a more descriptive (perhaps custom) exception.
As you probably know, a database problem will just produce a general exception -- which is fairly useless.
When appropriate, you can determine more detail about the exception and throw a more appropriate derived exception -- e.g. DuplicateDataException -- which can help the eventual handler.
I'm of the opinion that such spelunking should be done at the lowest level so the higher levels don't need to know the details of the particular connection in use.
Retain the caught exception as the InnerException, in case the handler does want to know.
Similarly, if logging of the exception is to be performed, it should be done at the highest level that makes sense, not the lowest level. Logging is an application responsibility, not a framework responsibility.
Finally, when catching-and-rethrowing, use throw , not throw ex .
You'll never get very far if all you do is follow instructions.
|
|
|
|
|
I agree with everything you wrote.
But you didn't answer my design question. Anything wrong with the concept of returning responses?
If it's not broken, fix it until it is
|
|
|
|