|
Everything declared in an Interface, when you implement it in a class, must have the access modifier 'public. This "default" implementation syntax can be called implicit implementation.
The exception, which does confuse people, is the special case where you have a class that implements more than one Interface and identical names are used in each Interface, and you need to make it clear, for a duplicated name implementation, which Interface your implementation refers to.
So, when explicit implementation is used: no modifiers are allowed; and, the Interface name is followed by a dot, and the declaration.
The examples here should make this clear: [^].
If you are one of those curious persons who dares ask why you can't have access modifiers in Interfaces, and/or why you can't use other modifiers than 'public in your implementation of Interfaces; you may wish to read this thread: [^].
Suppose your class implements one Interface, and you make every implementation explicit:
public interface IStuff
{
void SPB(string a, DateTime d, string c);
void P(decimal a, string r);
void EPB();
string Name { set; get; }
}
public class Test : IStuff
{
void IStuff.SPB(string a, DateTime d, string c)
{
throw new NotImplementedException();
}
void IStuff.P(decimal a, string r)
{
throw new NotImplementedException();
}
void IStuff.EPB()
{
throw new NotImplementedException();
}
private string _name;
string IStuff.Name
{
get { return _name; }
set { _name = value; }
}
} When you create an instance of this class, none of its methods, and its one field, can be accessed from the instance: you'll have to cast the instance to the Interface:
var test = new Test();
(test as IStuff).name = "wtf ?";
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
modified 18-Jan-17 2:48am.
|
|
|
|
|
Hi there everyone, new here and would like to get some feedback on my code.
I run a store procedure that I want to dump on a excel template.
It currently works, but just takes way too long. On sql management studio the query runs good, but just writing to the template is really slow.
Can anyone suggest a more efficient way to achieve the same result?
thanks in advance
here is part of my code:
sdate = StartDate.Value.ToString();
edate = EndDate.Value.ToString();
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet aSheet;
try
{
//Start Excel and get Application object.
oXL = new Excel.Application();
oXL.Visible = true;
//open the excel template
oWB = oXL.Workbooks.Open("C:\\TEMP\\template.xlsm");
//oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
//Call to service
//aSheet = (Excel._Worksheet)oWB.Worksheets.get_Item(1);
aSheet = (Excel._Worksheet)oWB.ActiveSheet;
//backgroundWorker1.ReportProgress(i++);
writedata_from_proc(aSheet, "dbo.CODE_RED_2017");
//backgroundWorker1.ReportProgress(i++);
//Make sure Excel is visible and give the user control
//of Microsoft Excel's lifetime.
//backgroundWorker1.ReportProgress(i++);
MessageBox.Show("Data extraction complete");
oXL.Visible = true;
oXL.UserControl = true;
//SaveExcel(oWB);
//clean up the COM objects to remove them from the memory
Marshal.FinalReleaseComObject(aSheet);
Marshal.FinalReleaseComObject(oWB);
Marshal.FinalReleaseComObject(oXL);
}
catch (Exception theException)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, theException.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, theException.Source);
MessageBox.Show(errorMessage, "Error");
}
}
|
|
|
|
|
It's slow because you're using Office Interop to create the Excel workbook. Office Interop is notoriously slow and there's no way you can speed this up.
To get it to go faster, you have to drop Excel and use the OpenXML SDK to create an Excel workbook file directly. It's much faster but the learning curve to use the SDK is kind of steep.
|
|
|
|
|
You can also export the data to a tab or comma separated text file - Excel is happy to use them
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
If it's straight data, yeah, that works. If he's formatting it, not so much.
|
|
|
|
|
It doesn't matter how much you format it, the bean counters will always change it so that it looks totally different, because they are all "Excel experts"!
In the sample code in the question it is just straight data - no formatting or formulae.
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
|
Yeah, by careful who you reply to. I got a notification that you replied but the OP didn't.
I know about ClosedXML.
|
|
|
|
|
|
You may want to try EPPlus-Create advanced Excel spreadsheets on the server - Home[^]
Works quite well.
What do you get when you cross a joke with a rhetorical question?
The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism.
Do questions with multiple question marks annoy you???
|
|
|
|
|
Hi all
I am beginner with C# programming. I have a problem with Teleric split button. I want to have the index number (or value) of menu item in split button. please help me.thanks.
|
|
|
|
|
Member 12938797 wrote: I am beginner with C# programming
Then you get familiar with reading the documentation that came with the control suite. If you still can't figure it out, ask the support people. You paid for the controls you also pay for support and they are the most familiar with their library than random strangers on the internet. You can contact them here Support and Learning | Telerik[^]
Speed of sound - 1100 ft/sec
Speed of light - 186,000 mi/sec
Speed of stupid - instantaneous.
|
|
|
|
|
the the "defaultitem" property is used for this case. I need a code to learn how to use this property.
|
|
|
|
|
Member 12938797 wrote: I need a code to learn how to use this property
Then do what I already told you to do and read the documentation. If you still can't figure it out, go to the Telerik website and ask them your question. We aren't a "write code for me" website.
Speed of sound - 1100 ft/sec
Speed of light - 186,000 mi/sec
Speed of stupid - instantaneous.
|
|
|
|
|
Agreed, Telerik documentation has with detailed examples. They also have a demo app of all other their tools for the type of project that you are developing for - ie: Wpf, Winforms, Asp, Mvc, etc... This app has a tab where you can view the source for the demo.
|
|
|
|
|
I am using Digital Persona One Touch SDK to create finger print based authenticating application in C#. There is a sample application (and code) given with it, when I installed it I could run that. It creates Template and Verifies correctly. But actually I want to save the finger print template data into MySql database and then retrieve that from database and compare while verifying.
So is there any way or technique to store the template data in mysql database and then retrieve it.
Kindly reply whoever knows any thing I am in a great need of it.
Thanks and Best regards
|
|
|
|
|
The Blob conquers all.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Also, do not repost questions on multiple forums. You were provided an answer on your previous post, Saving fingerprint template to SQL database. You should use that answer and build on top of that.
If you do not know how to do that, read some online tutorials first.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Hello,
I'm creating a custom exception that I want to display addition information. I have this so far, and it works, but it feels wrong. I was wonder if there was a better way.
public class AccessIOPortException : Exception
{
public AccessIOPortException(string message, int port)
: base("Error while accessing IO port " + port.ToString() + Environment.NewLine + message) { }
}
and then to call it
<pre lang="c#">
catch (Exception X)
{
Utilities.LogEvent(new AccessIOPortException(X.Message, port));
}
Any input would be greatly appreciated. Thanks!
|
|
|
|
|
If you are using C#6, try this instead:
public class AccessIOPortException : Exception
{
public AccessIOPortException(string message, int port) : base($"Error accessing port {port}{Environment.NewLine}{message}")
{
Port = port;
}
public int Port { get; }
}
This space for rent
|
|
|
|
|
What's "wrong" with this is that you are not "throwing" your new exception; you're simply creating an instance of the exception and passing it around.
If you throw your new exception, it will become the "inner" exception of any later exception handling, if any; otherwise, it is "the" exception.
Exception.InnerException Property (System)
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
For any real exception, you'll need to make it serializable.
You should also have a constructor which accepts the inner exception, if there is one.
[Serializable]
public class AccessIOPortException : Exception
{
public AccessIOPortException(string message, int port, Exception innerException) : base(FormatMessage(message, port), innerException)
{
Port = port;
}
public AccessIOPortException(string message, int port) : base(FormatMessage(message, port))
{
Port = port;
}
protected AccessIOPortException(SerializationInfo info, StreamingContext context) : base(info, context)
{
Port = info.GetInt32(nameof(Port));
}
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue(nameof(Port), Port);
}
private static string FormatMessage(string message, int port)
{
return $"Error while accessing IO port {port}\n{message}";
}
public int Port { get; }
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I would find an article explaining this code very valuable, and I think many others would also benefit.
thanks, Bill
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
Hi I am new to c# and learning a lot lately. My question relates to classes. Say I create a class then fill it with my parameters and a constructor, do I need a method for functionality in the class or do classes not always need functionality. Just a general question.
|
|
|
|
|
Are you asking this more from a language-lawyer point of view (having no methods is of course allowed by the language) or more from a design point of view, eg how useful is a class with no methods?
|
|
|
|