|
I know partial classes were primarily introduced to facilitate code generation but I use them on those occasions when I have to include a lot of tightly coupled, but loosely feature related items to a class.
Like, for my Context Free Grammar class (always referred to as a CFG in compsci circles) i have
Cfg.cs
Cfg.Validation.cs
Cfg.Computation.cs
Cfg.Symbols.cs
etc.
each one being partial class Cfg
Am I crazy for doing this?
Normally I like to spread features across several different classes but sometimes it just doesn't make sense to do so. This is one of those cases.
I could use regions to accomplish some of the source segregation but I find partial classes make moving from section to section easier in devstudio
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
codewitch honey crisis wrote: Am I crazy for doing this? Not for this
I think whatever organizational scheme that works for you is great; using ReSharper makes moving a selection of whatever into a new partial Class a single menu action + selecting (via CheckBoxes) which items in the selection you want to move + optionally renaming the new partial Class.
Of course, you do end up with multiple files ... the presence of which are known to tempt demons.
cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Oh I'm quite mad.
But yeah, this way of organizing has worked well for me since I started doing it.
The thing about the multiple files is I have a merge utility that integrates with VS that will package source code for me, so I can get it in one file if I want it.
I have a core source library that covers everything from MIDI to webserving to threading. It's just things I've built over the years that are useful and I use this tool to "export" bits of it in manageable chunks.
Maybe I should release that here sometime.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
codewitch honey crisis wrote: ... merge utility that integrates with VS that will package source code for me ... a core source library that covers everything from MIDI to webserving to threading. ... I use this tool to "export" bits of it in manageable chunks.
Maybe I should release that here sometime. Good idea !
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
I've held off on it mostly because it goes against MS coding "best practices" but they seem to think everything should be its own DLL/assembly.
I don't operate that way. I like my smaller apps to be as self-contained and as simply packaged as possible so I jump through hoops to make it the way i like it.
I tested the waters here with the idea and it seemed like the tide was against me on this.
So if i publish i expect wars over it to ensue.
hah. maybe more reason to do it.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
I think Microsoft's built in object templates (especially the new class template) should default to public partial , because that's what is used most.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
That question is not as easy as it looks like.
Normally, a class should care about one (and exactly only one) thing.
But still, there may be some aspects differing from the actual purpose of the class.
I use partial classes typically for the purposes of configuration (checking that the configuration is consistent, and that all mandatory dependencies were injected) and another partial class for test accessors (i.e. special functions for setting up that class with a specific state for test purposes, accessing non-public members as reporters for test purposes etc.).
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
I generally like to keep classes tightly factored like that as well, but sometimes, particularly with non-trivial engine style things, you're going to probably have something that's got a lot of tightly coupled functionality that is nevertheless loosely related and your choices are to break encapsulation, greatly expand the surface area of your interconnected class APIs or encase the functionality all in one class. The latter leads to a much smaller test matrix in cases where it's appropriate.
There's a time and a place for breaking just about every rule of thumb. =)
But that being said, I take your point. In most circumstances we wouldn't disagree, and I'm not sure we would in these cases either.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
codewitch honey crisis wrote: greatly expand the surface area of your interconnected class APIs That's another aspect: the Facade Pattern. Seems appropriate to distribute the "facade" class into some partial classes.
True, I use partial classes also with the Decorator Pattern, distinguishing between the "decorated" methods and those which are called without any changes.
There are some practical uses for partial classes.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
Yeah I could see doing that. I don't do a lot of injection and decoration these days because I'm out of the field and mainly researching parsing and compiling which doesn't really require a lot of that.
Still it makes a lot of sense to me to factor orthogonal functionality that way. It's an interesting approach.
Primarily I use them for augmenting classes created with code generation, which as I understand it is what they were primarily designed for, but I really like them in general as another tool in my factoring toolbelt.
I use one on my ParseContext class as well to make a "tear-off" with a bunch of helper functions that aren't necessary for the core functionality but are provided as a convenience.
Partial classes are neat. =)
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
How to write a method in C# that returns String as data type, no matter what datatype we have on excel spread sheet ?
modified 19-Jul-19 16:22pm.
|
|
|
|
|
I seem to have missed the question in your post. Would you care to clarify that?
|
|
|
|
|
Currently, readCell method in ExcelReader only reads String data. I have to Make it more dynamic so it can accept different datatypes like int, double, String, etc..
|
|
|
|
|
You already said that. Repeating it doesn't help.
I'm going to make some assumptions here. A method can only have one return type, so you're either going to have to make a method for each type you want to support, or write a "generic" version that accepts a type parameter and returns the data as the type specified, IF it can be cast to that type.
The problem with this is that your code has to know, or assume, the correct type for the field you're reading ahead of time.
|
|
|
|
|
Actually, the OP did not repeat the first post which had zero content: it's now clear he wants more than just Type 'String.
I upvote your first response for its salutary emetic effect on the OP
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
I don't know when he did it, but the original question has been modified and bunch of it removed.
|
|
|
|
|
I don't discount your psychic powers
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
It is very rude to post your home work and not even have the decency to ask a question. We'll gladly help but you have to actually ask something. We don't mind read (most of us at least) and we won't do everything for you.
The fact that you did not even post an appropriate title makes me think you don't know what to ask because you don't know what you are doing.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
If you are using OLEDB to read the data you can check the ValueType property on a cell.
|
|
|
|
|
Read a chunk (Excel Range) into a C# DataTable, and look at the Column Types: then you know the Type of any Cell in that Column.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
I do not understand why there is an UnobservedTaskException in that code at all...
A method in a WCF service is defined as
[OperationContract]
[FaultContract(typeof(RemoteException))]
Task SetInfraredFocus(string _clientName, double _focus);
A WPF application calls out to it via:
public void SetFocus(double _focus)
{
try
{
m_RemoteManualPsControl.SetInfraredFocus(UtilsEnvironment.TerminalName, _focus).Wait();
}
catch (Exception ex)
{
Logger.LogException(Name, ex);
}
}
Note the .Wait() and the catch . That in turn calls the WCF client class
public Task SetInfraredFocus(string _clientName, double _focus)
{
Task task = new Task(() => RemoteChannelProvider.Call(_channel => _channel.SetInfraredFocus(_clientName, _focus)));
task.Start();
return task;
}
True, I ought to add a CallAsync function to our WCF client base class, but that's quite convoluted code there...
The server (a singleton service, multi-threaded) has a simple implementation for the sake of demonstration:
public async Task SetInfraredFocus(string _clientName, double _focus)
{
try
{
await Task.Delay(1);
throw new Exception("Oh f***!");
}
catch (Exception ex)
{
Logger.LogException(Name, ex);
RemoteException.ThrowFaultExceptionFromException(ex);
}
}
And then I get that UnobservedTaskException... Its detail clearly shows where it comes from: it is the RemoteException created from the "Oh f***!" exception of the server. But why does the "catch" in the function on the top not catch the exception?
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
Bernhard Hiller wrote:
Task task = new Task(() => RemoteChannelProvider.Call(_channel => _channel.SetInfraredFocus(_clientName, _focus))); That line looks suspicious to me.
The RemoteChannelProvider doesn't seem to be a built-in class. What's the signature of the Call method?
What I suspect is happening is this:
SetInfraredFocus returns a Task ;Call may or may not discard that task;- Even if it doesn't discard the task, the
new Task constructor definitely discards it - none of the overloads accept a Func<Task> ; - The task returned from the client therefore doesn't observe the result of the task returned from the server. It could even complete before the server call has finished.
If the RemoteChannelProvider.Call method returns the Task , then you should be able to fix this by replacing the client code with:
public Task SetInfraredFocus(string _clientName, double _focus)
{
return Task.Run(() => RemoteChannelProvider.Call(_channel => _channel.SetInfraredFocus(_clientName, _focus)));
} Task.Run has overloads which accept a Func<Task> . The task returned from these will not complete until the inner task has completed, and it will propagate any exceptions correctly.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That overload does not do the trick either...
The "RemoteChannelProvider" is not a built-in class. The signature is:
public delegate void CallProxyDelegate<in T>(T _channel);
...
void Call(CallProxyDelegate<T> _codeBlock, [CallerMemberName] string _functionName = ""); Some implementation details:
m_ConfigurationChannelFactory = new ConfigurationDuplexChannelFactory<T>(m_CallbackObject, ConfigurationName, null, config);
...
m_CommunicationChannel = (ICommunicationObject)m_ConfigurationChannelFactory.CreateChannel();
...
_codeBlock((T)m_CommunicationChannel); I do not know how that handles a Task.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
OK, then it's definitely throwing the Task away.
How does the RemoteChannelProvider class cope with server methods which return a value?
If it doesn't, you might still be able to hack around it:
public Task SetInfraredFocus(string _clientName, double _focus)
{
return Task.Run(delegate
{
Task result = null;
RemoteChannelProvider.Call(_channel => result = _channel.SetInfraredFocus(_clientName, _focus));
return result;
});
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Perfect! That hack really works.
(But I still will need some time to find out how it works...)
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|