|
Base
-------------------------
| public int iNo; |
| |
-------------------------
Class1 : Base Class2 : Base
---------------------------- --------------------------
| Class2 c2; | | private int iMyNo; |
| void Class1() | | public void SetMyNo()|
| { iNo = 10; | | { iMyNo = iNo; |
| c2 = new Class2(); | | } |
| c2.SetMyNo(); | --------------------------
| } |
----------------------------
This is my srtucture, I begin with Class1, which then init Base, and then sets iNo = 10. Next I create a new instance of Class2, which in turn again init Base. Now when I call c2.SetMyNo, it sets is to nothing, since there are two instances of Base. So how do I inherit Class2 from the same instance of Base, the super class of Class1?
There's someone in my head but it's not me - Pink Floyd
gunigugu
|
|
|
|
|
Your question is not very clear but if I understand it you have to two classes Class1 and Class2 both inheriting from Base.
Then you create two objects, c1 from Class1 and c2 from Class2. Both objects will have its own copy of the iNo, inhereted from Base. They are not the same iNo int variable just because they are defined in the same class, instead they refer to seperate variables in memory. You don't want to use inheritance in this case, you want to use aggregation
class Base
{
public int iNo;
}
class Class1
{
public Base myBase;
}
class Class2
{
public Base myBase;
}
class Test
{
[STAThread]
static void Main()
{
Base b = new Base();
b.iNo = 10;
Class1 c1 = new Class1();
c1.myBase = b;
Class2 c2 = new Class2();
c2.myBase = b;
Console.WriteLine("c1.myBase.iNo = " + c1.myBase.iNo);
Console.WriteLine("c2.myBase.iNo = " + c2.myBase.iNo);
c2.myBase.iNo = 5;
Console.WriteLine("c1.myBase.iNo = " + c1.myBase.iNo);
Console.WriteLine("c2.myBase.iNo = " + c2.myBase.iNo);
}
}
This will result in the following outpu
c1.myBase.iNo = 10
c2.myBase.iNo = 10
c1.myBase.iNo = 5
c2.myBase.iNo = 5
/Patric
My C# blog: C# Coach
|
|
|
|
|
Thank you very much, however I think I'm going to use static variables.
I'm quite new to programming and didn't know exactly how to work with static variables, so it didn't occurr to me.
Regards
There is someone in my head but it's not me - Pink Floyd
|
|
|
|
|
in this program there is a function to create a new desktop, and it includes the source, but it is in C++ and VB. is there a wway to create a new desktop in C#? If so, how? thanks
http://www.codeproject.com/win32/AntonioWinLock.asp
|
|
|
|
|
|
I have form1 and form2 (Modal form). when I click button on form1 it will show form2(Modal Form)then input name(TextBox), when finished, click ok button on form2, I want value in name(TextBox) back to form1. I don't know why it's alway null; thank you
|
|
|
|
|
This is a standard way to obtain information from dialogs (modal forms):
<br />
<br />
void ButtonClick(object oj, EventArgs ea) {<br />
<br />
val = "";<br />
<br />
if (form2.ShowDialog() == DialogResult.OK) {<br />
<br />
val = form2.Val;
}<br />
}<br />
<br />
Be careful to call Form.ShowDialog() ! If you call Form.Show(), it will be unmodal and your code passes before user types anything.
|
|
|
|
|
How do I instantiate a Color in C# using a red-green-blue value? In Java, I am used to:
Color c = new Color(255,128,0);
What is the equivalent in C#?
Thanks!
Mark
|
|
|
|
|
|
In other words,
Color c = Color.FromARGB(255, 255, 255);
|
|
|
|
|
Hi,
I'm trying to make a panel scrollable when it's smaller than a bitmap that's drawn on it, but the scrollbars don't show up... When I place a control, such as a button, out of the panel boundaries and set the AutoScroll property of the panel to true, I do see scroll bars, but not when the bitmap is larger than the panel. Hope I made it clear. Any ideas?
Yigal
|
|
|
|
|
whenever you want to update scrollbars, call:
myPanel.OnResize(EventArgs.Empty);
|
|
|
|
|
Thanks, but I don't think that would help. The panel simply ignores the bitmap when calculating the scroll margins.
Yigal
|
|
|
|
|
I have app working with MDIs - there are also panels with bitmaps and AutoScroll on, but it works fine.
My panel contains PictureBox and this PictureBox has fixed size (same as bitmap) and location (0, 0).
Maybe you just setting Panel.BackgroundImage property (??). This will not work. Panel 's scroll bars depends on size of controls it contains.
|
|
|
|
|
I've created some control that browses directories using TreeView on the left side and shows image thumbnails on the right side.
This control contains thumbnail management. It works this way:
Create hash string from image file address: C:\hello.png -> e8f838d8283kn4.jpg
Does e8f838d8283kn4.jpg exist?
Yes: load this file
No: 1) shrink source image and save as e8f838d8283kn4.jpg
2) dispose source image
But when I browse gallery of 100 3.0 mpx images, my app creates about half of them and then system tells me, there is not enough virtual memory. Next message comes from mscorlib.dll (Out of memory exception) and program falls down.
But thumbnails are small enough. If there are 100 128x128 thumbs, it takes up to:
128x128x3x100 = +- 4.6 MB
Every 3 mpx source image is disposed (using Image.Dispose() call) as well as thumbnails, when directory changes. So why is there lack of memory (my PC has about 240 MB RAM, this may not be a problem).
GC must clear all resources since system needs memory occupied by disposed images, doesn't?
|
|
|
|
|
I'd have to question how your making the thumbnail images and making absolutely sure of what your doing. It sounds like your not aactually creating the thumbnail, but making a copy of the original image and just showing it scaled down in a picturebox.
Without seeing the relevant code, it's impossible to tell you what's going on.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
No, I'm not so silly.
This is the pseudo-code (loading and saving thumb) of my Thumbnail class:
<br />
Image source = Image.FromFile(sourcePath);<br />
Image img = new Bitmap(128, 128);<br />
<br />
Graphics grfx = Graphics.FromImage(img);<br />
grfx.DrawImage(img, ...)<br />
<br />
grfx.Dispose();<br />
source.Dispose();<br />
<br />
this.image = img;<br />
The thumbnail creation process runs on background and Thumbnail instance draws its image in overriden OnPaint method (not in PictureBox).
|
|
|
|
|
oh, sorry - this is on the one line:
<br />
grfx.DrawImage(source, new Rectangle(..., 128, 128), new Rectangle(... source.Size), GraphicsUnit.Pixel);<br />
I draw the large image onto small image. Then I dispose Graphics object and source image.
Thumbnail control disposes the small 128x128 image in the Dispose() method.
|
|
|
|
|
Have you tried calling (in order)
GC.Collect(); // collect unused memory
GC.WaitForPendingFinalizers(); // wait for any pending objects
GC.Collect(); // free any pending objects
Often it's not recommended, but sometimes the GC needs a little kick in the rear to start cleaning things up. This is especially true in pre-.NET 2.0 frameworks, where the GC has no knowledge of how "heavy" a particular unmanaged resource is.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Homosexuality in Christianity
Judah Himango
|
|
|
|
|
It seems to work fine!
Eh - Image object in unmanaged?
|
|
|
|
|
Well, in essence an Image is an 'unmanaged' resource, because the CLR and the GC do not manage the raw resources (in this case, a bunch of bytes representing a file) held by the object.
FYI, in .NET 2.0, you can call GC.AddMemoryPressure(long numberOfBytes) to let the GC know that you're holding on to some big resource like an image.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Homosexuality in Christianity
Judah Himango
|
|
|
|
|
Thx again. Important thing to remember.
|
|
|
|
|
You probably want to profile your application while you are at it to see if you have any other memory issues.
How To: Use CLR Profiler[^]
|
|
|
|
|
This is a very fundamental question about the use of enumerations in C#. I want to write out the value of an enumeration like System.Drawing.FontStyle to a file like this:
Font f...<br />
StreamWriter sw...<br />
<br />
:<br />
<br />
sw.WriteLine(f.FontStyle);
But then I want to read it in without having to use an if statement to decode it. This is what I want to avoid doing:
String myFontStyleReadIn...<br />
<br />
if (myFontStyleReadIn == "Bold") f = new Font("Arial",8,FontStyle.Bold)<br />
else<br />
if (myFontStyleReadIn == "Italic") f = new Font("Arial",8,FontStyle.Italic)
...
How do I write out the value of the enumeration, then read in the value and instantiate a new font using it?
Thanks!
Mark
|
|
|
|
|
Don't save enumeration members as strings. Save them as integers:
int index = (int)f.FontStyle;
Whenewer you want a string, use:
string style = ((FontStyle)index).ToString();
|
|
|
|