|
What do you mean "public accessors for private/protected variables.", John? Default are public to property, set and get?
regards,
George
|
|
|
|
|
If you don't want to provide set method, just cut it off from property field, no need to put private accessor in front.
|
|
|
|
|
Thanks adamzhang,
Good point.
regards,
George
|
|
|
|
|
True if you're using a backing field, but if you're using automatic properties en 3.0/3.5, you'll need a private set for it to work.
class WithAutomaticProperties {
public String NoBackingField { get; private set; }
}
class WithBackingField {
private string backing;
public String HasBackingField { get { return backing; } }
}
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
|
|
|
|
|
|
George_George wrote: What means "use an accessor to implement an interface"? Does it mean implement an accessor in a class which (the accessor) is declared in an interface?
Exactly.
|
|
|
|
|
Thanks Rob,
Another confusion from this document is "You cannot use accessor modifiers on an interface or an explicit interface member implementation".
http://msdn.microsoft.com/en-us/library/75e8y5dd(VS.80).aspx[^]
"cannot use accessor modifiers on an interface" means can not use accessor modifiers like public/private on interface accessor declaration or on some class's accessor which implements the interface's accessor?
regards,
George
|
|
|
|
|
"
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.
"
Everything specified by an interface is public and you can't change that. (That's the meaning of "interface".)
When an interface specifies a property, it must specify at least one accessor. If the interface specifies only one, you can't modify the accessibiity of that accessor, but you may implement the other accessor and choose to make it non-public.
|
|
|
|
|
Great PIEBALDconsult!
But why it is "may not have an access modifier"? It should be must not, agree?
regards,
George
|
|
|
|
|
George_George wrote: It should be must not,
Indeed.
|
|
|
|
|
Thanks PIEBALDconsult,
But when we implement a property's accessor in a class, and not using explicit interface implementation, we can use access modifiers. Right?
Here is my code, correct?
interface IFoo
{
int abc
{
get;
}
}
class Foo : IFoo
{
private int _abc;
public int abc
{
get
{
return _abc;
}
private set
{
_abc = value;
}
}
}
regards,
George
|
|
|
|
|
|
Sorry PIEBALDconsult,
I made a mistake. I think when we implement an accessor defined in an interface, whether or not using explicit implementation in the class, we can not add any modifiers.
But if in the interface, only one accessor is defined for a property, we can add modifier to the other accessor of the property (as shown in my above sample).
Any comments? Agree or?
regards,
George
|
|
|
|
|
Ummm, yeah, the "explicit" doesn't matter.
|
|
|
|
|
Thanks for your confirmation, PIEBALDconsult!
regards,
George
|
|
|
|
|
|
|
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
|
|
|
|
|