|
|
|
Hi George,
I seldom dislike a question, but I dislike this one.
If you are not sure whether a method/property will be public/private when not specifying
it explicitly, then the next person looking at your code could well have the same doubts.
The only good remedy then is to make it explicit all the time.
That's what I do, I add private/protected/public or whatever is appropriate to every
single member in every single class.
If you ask me, having a default was a mistake by the C# language designers.
|
|
|
|
|
Luc Pattyn wrote: having a default was a mistake by the C# language designers
I agree. And that's what I do too.
|
|
|
|
|
|
Thanks Luc,
I totally agree with you, but sometimes we need to read some code by others, for example, the samples in MSDN.
I have written some samples and here are the learning results. Could you review whether I am correct.
http://msdn.microsoft.com/en-us/library/75e8y5dd.aspx
1.
"You cannot use accessor modifiers on an interface or an explicit interface member implementation."
Means we can not put modifier on interface declaration of accessor, and also can not use modifier on the class implementation of the accessor (which is declared in interface).
2.
"When you use an accessor to implement an interface, the accessor may not have an access modifier. However, if you implement the interface using one accessor, such as get, the other accessor can have an access modifier, as in the following example:"
It means if interface only declare one accessor, either get/set, then in the class implementation, for the interface declared accessor, either get/set, we can not use any modifier, but for the other accessor, which is not declared in interface, either set/get, we can add modifier.
Both understandings are correct?
regards,
George
|
|
|
|
|
Hi George,
I don't know, I can't answer your question without either reading the documentation,
or trying it in Visual Studio; you can do those things equally well yourself.
Anyway, if I don't know today it tells me it probably is not important, i.e. I tend to put
an access modifier and when it is not allowed the compiler will tell me. So you can
call me pragmatic, I want to know a lot but I don't feel a need to know it all,
ceetainly not when the tools are available and adequate to correct me in time.
Regards,
|
|
|
|
|
Thanks all the same, Luc!
regards,
George
|
|
|
|
|
I have read all the mail merge threads and articles I could find, and I'm a little frustrated at the moment... I really hope someone could help me here!
I found a very simple approach to merging, which I've implemented, and the code is show below:
Object oMissing = System.Reflection.Missing.Value;<br />
<br />
Object oTrue = true;<br />
Object oFalse = false;<br />
DataRow row;<br />
<br />
Word.Application oWord = new Word.Application();<br />
Word.Document oWordDoc = new Word.Document();<br />
<br />
openFileDialog.ShowDialog();<br />
openFileDialog.Filter = "Word Template File (*.dot)|*.dot|Word File (*.doc)|*.doc|All files (*.*)|*.*";<br />
<br />
oWord.Visible = true;<br />
<br />
Object oTemplatePath = openFileDialog.FileName;<br />
<br />
oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);<br />
<br />
bs_results.MoveFirst();<br />
row = ((DataRowView)bs_results.Current).Row;<br />
<br />
foreach (Word.Field myMergeField in oWordDoc.Fields)<br />
{<br />
String fieldtext = myMergeField.Code.Text;<br />
<br />
if (fieldtext.StartsWith(" NEXT"))<br />
{<br />
if (bs_results.Position == bs_results.Count-1) break;<br />
bs_results.MoveNext();<br />
row = ((DataRowView)bs_results.Current).Row;<br />
myMergeField.Code.Text = String.Empty;<br />
}<br />
<br />
if (fieldtext.Contains("firstname"))<br />
{<br />
myMergeField.Select();<br />
oWord.Selection.TypeText(row["firstName"].ToString());<br />
}<br />
if (fieldtext.Contains("lastname"))<br />
{<br />
myMergeField.Select();<br />
oWord.Selection.TypeText(row["lastName"].ToString());<br />
}<br />
if (fieldtext.Contains("cellphone"))<br />
{<br />
myMergeField.Select();<br />
oWord.Selection.TypeText(row["phoneCell"].ToString());<br />
}<br />
}
This is simple, and to the point, and works well. It has limitations though. It loops merge fields, so there are only so many labels created for example as are propagated in the .dot template file, as apposed to rows in the datatable.
My QUESTION:
How do I either propagate labels based on the row count, or do it completely different??
I basically have a results datatable with all the data I need, and would like to mail merge that data to labels in a Word template.
Any help would be appeciated! PLEASE!
|
|
|
|
|
Hello everyone,
I am migrating from C++ to C#. The following compile error makes me confused. Suppose in interface there is a method called Abc which returns object, and in the implementation class, there is also a method called Abc, but the return type is List<int>, I think List<int> is already a type (derived type) of object, so no need to explicitly implement Interface.Abc again, but here is a compile error.
D:\Visual Studio 2008\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs(14,11): error CS0738: 'MyList.Foo' does not implement interface member 'MyList.IFoo.Abc()'. 'MyList.Foo.Abc()' cannot implement 'MyList.IFoo.Abc()' because it does not have the matching return type of 'object'.
Could anyone show me what is the rule I break here please?
public class MyList
{
interface IFoo
{
object Abc();
}
class Foo : IFoo
{
public Foo()
{
}
public List<int> Abc()
{
return new List<int>;
}
}
static void Main()
{
Foo f = new Foo();
return;
}
}
thanks in advance,
George
|
|
|
|
|
The Abc method of the interface and the implementing class must be of the same return type.
Your interface is object and the implementation is List<int>
|
|
|
|
|
Must be same, derived type is not ok?
regards,
George
|
|
|
|
|
Your problem is the implementation of Abc();
<br />
public List<int> Abc()<br />
{<br />
return new List<int>;<br />
}<br />
</int></int>
Change the return type from "List<int>" to "object" and that will fix it.
When implementing an interface, you must copy the interface declaration directly.
Inversly, you could change
<br />
interface IFoo<br />
{<br />
object Abc();<br />
}<br />
TO
interface IFoo
{
List<int>Abc();
}
Hogan
|
|
|
|
|
Thanks Hogan,
1.
I want to confirm with you must implement the same exact type? Derived type is not allowed as well?
2.
The same rule for parameter, must be exact the same in implementation class and interface declaration?
regards,
George
|
|
|
|
|
1. Yep, the exact same type.
2. Same again, the exact same type.
Hogan
|
|
|
|
|
Thanks Hogan,
Question answered.
regards,
George
|
|
|
|
|
You need to do what is called an explicit implementation of the interface. Here is the syntax using your example:
public interface IFoo
{
object Abc();
}
public class Foo : IFoo
{
public List<int> Abc()
{
return new List<int>();
}
object IFoo.Abc()
{
return this.Abc();
}
}
|
|
|
|
|
Thanks tgrt!
regards,
George
|
|
|
|
|
|
Let's say I have a resource broker, a 'lookup resource' event and a main program that will invoke the lookup resource event. All of these reside in decoupled modules, there may be only one resource broker and of course only one lookup event definition.
I created the following classes to implement the situation above:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Program::Main");
EventBroker.Current.FireLookupResource(null, EventArgs.Empty);
Console.ReadLine();
}
}
public class EventBroker
{
private static readonly EventBroker _current = new EventBroker();
public static EventBroker Current { get { return _current; } }
public event EventHandler LookupResource;
private EventBroker() { }
public void FireLookupResource(object sender, EventArgs args)
{
Console.WriteLine("EventBroker::FireLookupResource");
if (LookupResource != null)
LookupResource(sender, args);
}
}
public class ResourceBroker
{
private static readonly ResourceBroker _current = new ResourceBroker();
public static ResourceBroker Current { get { return _current; } }
private ResourceBroker()
{
Console.Write("ResourceBroker::ResourceBroker");
EventBroker.Current.LookupResource += EventBroker_LookupResource;
}
private static void EventBroker_LookupResource(object sender, EventArgs e)
{
Console.Write("ResourceBroker::EventBroker_LookupResource");
}
}
However the output is not what I expected: the LookupResource event never gets called. That's understandable, since the ResourceBroker never gets referred in the executing code, it will not get instantiated. But what should I do to get the 'correct' output?
Creating a NoOp method in the ResourceBroker, and calling it in the Main function, works but it seems to me like an ugly hack. Am I missing something in the language keywords/features?
Thanks in advance
Luca
The Price of Freedom is Eternal Vigilance. -- Wing Commander IV
En Það Besta Sem Guð Hefur Skapað, Er Nýr Dagur.
(But the best thing God has created, is a New Day.)
-- Sigur Ròs - Viðrar vel til loftárása
|
|
|
|
|
Hmmmm... This is a bit of a tricky situation. Usually when I find myself in this situation, it means it's time to do a redesign. As a suggestion, you may want to make a BrokerManager that does all of the initialization for you, or pick an appropriate Broker class and have it take an instance of the other as a constructor argument. You can limit the instances that way, and you may be able to do away with the singleton pattern. Hope that helps somewhat.
Scott P
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
|
|
|
|
|
Hi ,
i have try to open an PDF file in windows application using C# I was getting an error that type should not be null following is the code . Please Help me Urgent.
Acrobat.CAcroApp app = (Acrobat.CAcroApp)Activator.CreateInstance(Type.GetTypeFromProgID("AcroExch.App"));
app.Show();
Acrobat.CAcroAVDoc doc = (Acrobat.CAcroAVDoc)Activator.CreateInstance(Type.GetTypeFromProgID("AcroExch.AVDoc"));
doc.Open(FileName, "Computed Mileage");
Error Message is
Value cannot be null.
Parameter name: type
Thank u ,
Maruthi.
|
|
|
|
|
My guess is that Type.GetTypeFromProgID is returning a null. Try seperating this out a bit and setting a couple of checks / breakpoints to try and find out more about what's going on:
Type acroapp = Type.GetTypeFromProgID("AcroExch.AcroExch");
Type acrodoc = Type.GetTypeFromProgID("AcroExch.AVDoc");
if ((acroapp == null) || (acrodoc == null))
throw new Exception("Acrobat not installed on this system.");
Acrobat.CAcroApp app = (Acrobat.CAcroApp)Activator.CreateInstance(acroapp);
Acrobat.CAcroAVDoc doc = (Acrobat.CAcroAVDoc)Activator.CreateInstance(acrodoc);
if ((app == null) || (doc == null))
throw new Exception("Error initialising Acrobat");
app.Show();
doc.Open(FileName, "Computing Mileage"); At least laying it out like this will tell you what's going wrong (or give you more of a hint).
If you see the first exception then check your TypeIDs for Acrobat, if you see the second then check the acrobat documentation is my only suggestion.
|
|
|
|
|
I have Adobe Reader 8.0 in my system But it was still showing null
even we want to install acrobat professional in my system
|
|
|
|
|
Where in the code I provided does it fail?
|
|
|
|
|