|
I'm not sure what is going on here, but I hope there's someone who knows why this happens. Basically, I have a base class and an inherited class, something like this:
public abstract class Base {}
public class Inherited : Base {}
I also have a class that populates inherited objects called ObjectBuilder. It is a singleton and it has a method called PopulateObject. Here is the code to do what I want:
Inherited i = new Inherited();
ObjectBuilder ob = ObjectBuilder.GetInstance();
ob.PopulateObject( ref i );
The problem is that the signature for PopulateObject takes a ref to Base. If I understand the is-a relationship properly, doing so should work and in fact, when I remove the 'ref' keyword from the declaration as well as implementation, it builds fine. It just won't work properly because it is at that point passing a copy (isn't it?). What is going on here? Why will it build without error when I pass a copy, but fails when I pass by reference?
Any help would be greatly appreciated.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
Which error message is your compiler giving?
lazy isn't my middle name.. its my first.. people just keep calling me Mel cause that's what they put on my drivers license. - Mel Feik
|
|
|
|
|
See below...
The best overloaded method match for 'DataNamespace.ObjectBuilder.PopulateObject( ref DataNamespace.Base)' has some invalid arguments
'Argument '1': cannot convert from 'ref DataNamespace.Inherited' to 'ref DataNamespace.Base'
Thanks.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
You are infact passing by reference by default unless your base class is infact of a value-type!
"There are no stupid question's, just stupid people."
|
|
|
|
|
I went ahead and tried to run my app without the ref parameter and it worked--which led me to the same conclusion. I looked back at my Programming C# book (Oreilly) and sure enough, the example where he is using this is with value types. Oy!! People who think that C# and Java are the same are smoking crack. I like C# better because you can pass a value type by reference if you want to(in java you have to use the Integer, Double, etc. classes to do so). Thanks for your help.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
There was an obscure white paper I ran across once on the MSDN site which discussed the use of ByVal vs. Ref in application of VB.NET vs. C#.NET.
It basically stated that C#, by default, passes parameters as ref. So if you specify MyMethod(ref someParameter) what you are doing is passing a pointer to the pointer for the parameter. This could explain the illegal reference compile error if you are using the parameter as a parameter pointing to a data area within the caller, rather than a pointer pointing back to a pointer which points to the data area within the caller. gives me a headache just describing it.
---------------------------------------------
Once I thought I was wrong but I was happy to discover that was a mistake.
Condor
|
|
|
|
|
I know what you mean about a headache. It's not so bad once you get used to it, I suppose, but I'm used to C++ and trying to make the transition to C#. Probably a few more headaches along the way. I guess the rule of thumb with C# is if you think it's complicated, you're probably just making it so. Thanks for your help. I appreciate it.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
Could be
It's one of the reasons why C++.NET could be considered superior (despite the rather hap-hazard managed extensions syntax) -- it forces you to box and unbox objects yourself, and think about whether they're value or reference types, and access them accordingly.
--
Paul
"I need the secure packaging of Jockeys. My boys need a house!"
- Kramer, in "The Chinese Woman" episode of Seinfeld
MS Messenger: paul@oobaloo.co.uk
Sonork: 100.22446
|
|
|
|
|
I have a float number like 32134(its size of file in bite).I want to divide it by 1000.So I do this:
f=12342;
f2=f/1000
I want to get 12.342 but it gives me 12. How can I get 12.342?
Mazy
"If I go crazy then will you still
Call me Superman
If I’m alive and well, will you be
There holding my hand
I’ll keep you by my side with
My superhuman might
Kryptonite"Kryptonite-3 Doors Down
|
|
|
|
|
Hi.
its the same as in C++
1000.0 is a double
1000.0f is a float
1000f is a float
1000 is an int
Therefore
f=12342;<br />
f2=f/1000.0f
Hope this helps
Pete
|
|
|
|
|
Mazy, This gives me 12.342, how are you doing it? Was your f2 defined as an int?
float f = 12342;
float f2;
f2 = f/1000;
Console.WriteLine(f2);
Nick Parker
May your glass be ever full.
May the roof over your head be always strong.
And may you be in heaven half an hour before the devil knows you’re dead. - Irish Blessing
|
|
|
|
|
Thank you both.I made some mistakes.Its OK now.
Mazy
"If I go crazy then will you still
Call me Superman
If I’m alive and well, will you be
There holding my hand
I’ll keep you by my side with
My superhuman might
Kryptonite"Kryptonite-3 Doors Down
|
|
|
|
|
Hi
I am facing a problem when darging and droping a user control. How can i Drag and Drop a user control physically. that is when draging and droping the control must go from one place to the other

|
|
|
|
|
Is there a way to count the number of subscriptions to a given event? We all know about the += operator to add a new subscription to an event and the -= to remove one. So, can you count the number of subscriptions, for example, a click event of a menu item:
this.mnuFileOpen.Click += new System.EventHandler(this.mnuFileOpen_Click1);
this.mnuFileOpen.Click += new System.EventHandler(this.mnuFileOpen_Click2);
this.mnuFileOpen.Click += new System.EventHandler(this.mnuFileOpen_Click3);
I would like something like this (I know this is not valid):
int nClickCount = this.mnuFileOpen.Click.Count;
or
int nClickCount = CountSubscriptions(this.mnuFileOpen.Click);
nClickCount would be 3.
Thanks in advance
Andy
|
|
|
|
|
In fact, you are using a multicast delegate instead of a single cast delegate (System.MulticastDelegate instead of System.Delegate).
The System.MulticastDelegate API provides what you need.
Namely, GetInvocationList(); returns the Delegate[] array.
How low can you go ? (MS rant)
|
|
|
|
|
OK. So, from the example I gave, what would I need to do exactly to get the count? I'm a bit unsure.
Thanks again
Andy
|
|
|
|
|
More info on delegate type is needed to provide full source code.
I guess this code will be helpful :
class App {
delegate String GetStatus();
static void Main() {
GetStatus getStatus = null;
getStatus += new GetStatus(new Light().SwitchPosition);
getStatus += new GetStatus(new Fan().Speed);
getStatus += new GetStatus(new Speaker().Volume);
Console.WriteLine(GetComponentStatusReport(getStatus));
}
static String GetComponentStatusReport(GetStatus status) {
if (status == null) return null;
StringBuilder report = new StringBuilder();
Delegate[] arrayOfDelegates = status.GetInvocationList();
foreach (GetStatus getStatus in arrayOfDelegates) {
try {
report.Append(getStatus() + "\r\n\r\n");
}
catch (Exception e) {
Object component = getStatus.Target;
report.Append("Failed to get status from " +
((component == null) ? "" : component.GetType() + ".") +
getStatus.Method.Name +
"\r\n Error: " + e.Message + "\r\n\r\n");
}
}
return report.ToString();
}
}
How low can you go ? (MS rant)
|
|
|
|
|
I have a simple c# component which actually read the Excel Spreedsheet and store the contain into an array.
I'm referencing the Microsoft Excel 9.0 Object Library in my C# componenet.
It work OK when i using the C# sample program to call the component but it not able to read the excel file when i using the ASP.NET.
The error message "Access is denied" is show when i using the ASP.NET to call the component.
I have try to open and read the same excel file only using ASP.NET by using SELECT * FROM [SHEET1] and able to read the Spreedsheet contains.
It open and read the file successfully. When i bind the dataSet from the Excel spreadsheet to the DataGrid, it show all the contains in the DataGrid.
But it not show the complete DataSet contain when i try to print the dataset contain in xml format using
Response.write(ds.getxml()).
Have anyone of you have experience this problem before? please advice. Thanks.
Regard
LSL
|
|
|
|
|
mee wrote:
The error message "Access is denied" is show when i using the ASP.NET to call the component.
ASP.NET runs in its own user ID ("ASPNET"), so that user must be given permission to access your file.
Paul
I think there're pieces of me you've never seen - Tori Amos, Tear in Your Hand
|
|
|
|
|
Hi Paul ,
Thanks for your reply, i have already given permission to ASPNET user , IIS user [IUSR_Machinename] and everyone with Full control on the excel file.
The error on browser actually highligt the following line (this line actualy program in the C# component).
ExcelObj = new Excel.Application();
Is it possible because ASPNET user do not have access to any of the excel library file . Anyway i also try to set the EXCEL9.OLB permission to the ASPNET user. It still giving the same error.
Regards
LSL
|
|
|
|
|
I have a C# application that calls functions on a MFC COM dll. I can easily debug my C# code, but I can't seem to be able to step into the MFC code. Did anybody try this?
Thanks
|
|
|
|
|
Have you tried to add a DebugBreak() statement in your MFC code. Actually when the .NET virtual machine leaves his context to execute unsafe code, I believe it is up to you to manage to catch up.
How low can you go ? (MS rant)
|
|
|
|
|
True, it does start VC6, but it throws me in disassembly. I can't actually step into the MFC code.
|
|
|
|
|
Are you using the debug version of the COM DLL?
Paul
I think there're pieces of me you've never seen - Tori Amos, Tear in Your Hand
|
|
|
|
|
Anonymous wrote:
True, it does start VC6, but it throws me in disassembly.
That's what DebugBreak() usually does, even in a simple WIN32 only environment. Use the debugger callstack.
How low can you go ? (MS rant)
|
|
|
|