|
Yeah, that sounds pretty close to a struct, even though it can contains methods and do several things similar to classes.
To know for sure, you could probably answer yourself better than I (since like I mentioned before I don't know Delphi). If records are allocated on the stack, then they are definitely closer to structs.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I'm not familiar with Delphi, but it seems more like you have a class or struct here. The big difference between the two is that structs are allocated on the stack while classes are allocated on the heap. Classes are reference types so passing instances of classes as parameters passes a reference, where passing a struct passes a copy of the object (unless using the out or ref keywords).
In this case, you might be better of with a struct like:
public struct MyRecord
{
public string Name;
public int Age;
public TMyProc Proc;
} The only thing I'm not sure about is that last member. You say it's a "type" but it almost seems like a function pointer? In that case, you'll want to look up information on delegates in the .NET Framework SDK. If it's just any type, declare it as an object , which is the base class for ALL classes, structs, etc., in .NET.
An enum is more like a grouping of constants that provides type-safe access to such constants in .NET. For example, you could declare a bunch of constants like so:
public const int One = 1;
public const int Two = 2;
public const int Three = 3;
public void SomeMethod(int value)
{
if (value != 1 && value != 2 && value != 3)
throw new ArgumentException("Invalid value.", "value");
} For SomeMethod , it can take any int so unless you want to hard-code and document the possible values, this isn't very type-safe. After all, any int could be supplied. If you only want this to accept certain values, then you should declare it like the following:
public enum MyEnum
{
One = 1,
Two,
Three
}
public void SomeMethod(MyEnum value)
{
if (!Enum.IsDefined(value.GetType(), value))
throw new ArgumentException("Not defined.", "value");
} You still have to validate the param to be sure, but as you can see that's much easier and more flexible that checking it against hard-coded values. It also gives the caller some idea of what he or she can actually pass.
If you're new to the .NET Framework and the C# language (which is just one of many languages that target the Common Language Runtime, or CLR), you should definitely read the .NET Framework SDK to get an overview and review what's available in the base class library. It should've been installed with VS.NET and is also available online at http://msdn.microsoft.com/netframework[^].
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Yes, I think struct is what I need. Thanks.
|
|
|
|
|
how to use CreateProcess function (like in c++) in c#? I found EnvironmentVariable method to access windows environment var but it only has read only property, I cannot set new environment var.
I use CreateProcess to execute wincgi file like php.exe to run PHP file with it's querystring and post data manually.
Thx anyway!!!
laurent
|
|
|
|
|
See the documentation for the System.Diagnostics.Process class in the .NET Framework SDK. If you want to set additional environment variables used by the process, you can initialize a ProcessStartInfo and provide extra string key/value pairs in the EnvironmentVariables property. After assigning a few more properties of ProcessStartInfo , just pass it to a call to Process.Start . See the documentaton for more information and examples.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
1. Process.Start()
2. You can use DllImport and use win32 functions in C# , for more informatiom about it check MSDN and search this site.
Mazy
"Art is life, life is life, but to lead life artistically is the art of life." - Peter Altenberg
|
|
|
|
|
Hi Folks
Hope you all are having a good knowldze on C#.
Here i want to know one thing , At runtime how runtime will decide what to use I mean either virtual one or overrided one.
two more Question
What is the difference between Abstract class and interface
We know some basic differences.
when to use structures and when to use classes
Praveen Chintamani.
|
|
|
|
|
praveenc80 wrote:
At runtime how runtime will decide what to use I mean either virtual one or overrided one.
That is polymorphism, the compiler will take care of that for you. Whatever your object "is a" instance of will be called.
praveenc80 wrote:
What is the difference between Abstract class and interface
Abstract class can't be instantiated. Interface is good to use if you want to enforce a method on certain objects, but those objects might not have a base->child relationship. For example, look at IDisposable, Microsoft classes that implemenet this interface support the Dispose() method to cleanup resources.
praveenc80 wrote:
when to use structures and when to use classes
Your call really. Structures are stored on the stack and classes are on the heap. Strcutures are not as powerful as classes, but are more quickly accessed from memory. I could imagine using structs for something not so object oriented.
Hope this gives you a starting point.
R.Bischoff
.NET, Kommst du mit?
|
|
|
|
|
Soliant wrote:
Abstract class can't be instantiated.
Interfaces are also nice because a class can only extend one other class. If you use abstract classes and need to inherit additional functionality (say, from Control ), you won't be able to because you're already extending one abstract base class. This is where interfaces come in handy because you an implement more than one interface.
Soliant wrote:
I could imagine using structs for something not so object oriented.
Structures should really only be used for small amounts of short-lived data, such as a Size or a Point (both used to position and size a control, but then aren't needed again things change).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I have a scrollbar with properties set below:
min: 0
max: 100
LargeChange: 10
SmallChange: 1
Now at run-time, I try to scroll the scrollbar to its max position using mouse, but it is impossible! I can only scroll it up to:
Max-LargeChange+1
here it is 91
The strange thing is that I can programatically set the scrollbar's value to 100.
What the hell should I do with this damn control?
Don't forget, that's Persian Gulf not Arabian gulf!
|
|
|
|
|
Hi,
I have a simple application. I want to draw two concentric circles and a bunch of random dots between the circles that are refreshed periodically. I could code this app in either GDI+ or DirectDraw. My question is will any one of these give me higher efficiency (faster processing)? As I understand GDI+ does all its graphics processing in memory i.e. is s/w based does not take advantage of hardware acceleration.
I am completely new to direct draw but it appears in my app I would be first drawing onto the back buffer surface and then while rendering flip the front buffer with the back buffer. The Surface object which ddraw provides is a memory object so how is ddraw taking any advantage of graphics hardware?
Also could anyone explani to me the SetDisplayMode method of ddraw?
sid
|
|
|
|
|
direct draw is faster, that's what it's for. The question is, do you need to be that fast ?
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
To (hopefully) clarify further: The technology formerly known as DirectDraw ended with DirectX 7. DirectX 8 pulled the DirectDraw functionality into the Direct3D interface, and in DirectX9 a managed wrapper for this interface was introduced. So if you want to use C# you should use Managed DirectX9. When you create the D3D surface you specify whether you want it created in system memory or on the graphics card. Obviously if you want acceleration you create it "directly" (get it?) on the card. What specifically about SetDisplayMode don't you understand?
Michael Blome [MS]
|
|
|
|
|
HI,
I am using DX9 and I think DDraw does not end with DX7, its there in DX9 also. ANyway I created two projects - one in GDI+ another in DDraw9 and I find ddraw is much much slower than GDI+ - obviously it seems I am making a mistake somewhere but where? Please see the DisplayFrame method in my DDraw code.
Regarding SetDisplayMode:
is the fourth parameter the graphics card refresh rate you want to set and its specified in Hz?
I have no idea what the fifth parameter means.
This is my GDI+ code (I would be happy to send you the entire project for GDI+ and Ddraw if you want) - the OnPaint method is of interest:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace enigma1
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics dc = e.Graphics;
Double area = Math.PI*(m_RadiusOuter*m_RadiusOuter
- m_RadiusInner*m_RadiusInner);
Int32 n = (Int32) ((m_DotDensity / (128*128)) * area);
this.WindowState = FormWindowState.Maximized;
this.BackColor = Color.White;
Int32 centery = this.Bottom / 2;
Int32 centerx = this.Right / 2;
dc.DrawEllipse(m_PenInner, centerx-m_RadiusInner,
centery-m_RadiusInner, 2*m_RadiusInner, 2*m_RadiusInner);
dc.DrawEllipse(m_PenOuter, centerx-m_RadiusOuter,
centery-m_RadiusOuter, 2*m_RadiusOuter, 2*m_RadiusOuter);
// draw n random dots
for(Int32 ctr = 0; ctr < n; ctr++)
{
Double theta = (Double) m_Rnd.Next()/Int32.MaxValue * 2*Math.PI;
Double r = m_Rnd.Next(m_RadiusInner+1, m_RadiusOuter-1);
Double cx = centerx + r*Math.Cos(theta);
Double cy = centery - r*Math.Sin(theta);
dc.DrawEllipse(m_DotPen, (float) cx - m_DotSize,
(float) cy - m_DotSize,
(float) 2*m_DotSize, (float) 2*m_DotSize);
dc.FillEllipse(m_DotBrush, (float) cx-m_DotSize,
(float) cy - m_DotSize,
(float) 2*m_DotSize, (float) 2*m_DotSize);
}
// pause for m_t1 ticks
System.Threading.Thread.Sleep(100);
// for(Int32 ctr = 0; ctr < 0xafffff; ctr++);
// blank the screen for m_t2 ticks
this.BackColor = Color.White;
System.Threading.Thread.Sleep(100);
// for(Int32 ctr = 0; ctr < 0xafffff; ctr++);
this.Invalidate(); // redraw again
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(692, 466);
this.Name = "Form1";
this.Text = "Form1";
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Form1 f1 = new Form1();
f1.GetInputs();
// following displays the screen resolution
// MessageBox.Show(Screen.PrimaryScreen.Bounds.Right.ToString() + " x " + Screen.PrimaryScreen.Bounds.Bottom.ToString());
//
Application.Run(f1);
}
public void GetInputs()
{
m_DotSize = 2;
m_t1 = 10;
m_t2 = 10;
m_PenInner = new Pen(Color.Black);
m_PenOuter = new Pen(Color.Blue);
m_RadiusInner = 64;
m_RadiusOuter = 128;
m_DotDensity = 20;
m_Rnd = new Random();
m_DotPen = new Pen(Color.Black);
m_DotBrush = new SolidBrush(Color.Black);
}
private Int32 m_DotSize;
private Int32 m_t1;
private Int32 m_t2;
private Pen m_PenInner;
private Pen m_PenOuter;
private Int32 m_RadiusInner;
private Int32 m_RadiusOuter;
private Double m_DotDensity;
private Random m_Rnd;
private Pen m_DotPen;
private Brush m_DotBrush;
///
/// Processes key up events.
///
private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
// If the user presses the Escape key, the app needs to exit.
if (Keys.Escape == e.KeyCode)
Close();
}
}
}
THIS IS MY DDRAW CODE (the DisplayFrame method is of interest):
//-----------------------------------------------------------------------------
// File: AnimatePalette.cs
//
// Desc: AnimatePalette demonstrates DirectDraw palette animation when in full-screen
// on a palettized surface.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectDraw;
namespace AnimatePalette
{
public class AnimatePalette : System.Windows.Forms.Form
{
// Declare the class scope variables here.
public void GetInputs()
{
m_DotSize = 2;
m_t1 = 10;
m_t2 = 10;
m_PenInner = new Pen(Color.Black);
m_PenOuter = new Pen(Color.Blue);
m_RadiusInner = 64;
m_RadiusOuter = 128;
m_DotDensity = 20;
m_Rnd = new Random();
m_DotPen = new Pen(Color.Black);
m_DotBrush = new SolidBrush(Color.Black);
}
private Int32 m_DotSize;
private Int32 m_t1;
private Int32 m_t2;
private Pen m_PenInner;
private Pen m_PenOuter;
private Int32 m_RadiusInner;
private Int32 m_RadiusOuter;
private Double m_DotDensity;
private Random m_Rnd;
private Pen m_DotPen;
private Brush m_DotBrush;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
const int indexBlack = 144; // Index of the palette entry that contains black.
const int indexWhite = 0; // Index of the palette entry that contains white.
const int screenWidth = 640; // Screen width.
const int screenHeight = 480; // Screen height.
Device displayDevice = null; // The Direct Draw Device.
Surface front = null; // The front surface.
Surface back = null; // The back surface.
Palette pal = null; // The palette the front surface will use.
int tickLast = 0; // Holds the value of the last call to GetTick.
bool restoreNeeded = false;// Flag to determine the app needs to restore the surfaces.
PaletteEntry[] pe = new PaletteEntry[256]; // Palette entry array.
///
/// Entry point for the application.
///
public static void Main()
{
AnimatePalette app = new AnimatePalette();
Application.Exit();
}
///
/// Constructor for the main class.
///
public AnimatePalette()
{
//
// Required for Windows Form Designer support.
//
InitializeComponent();
// Make sure to use the arrow cursor.
Cursor = Cursors.Arrow;
// Create a new DrawDevice.
InitializeDirectDraw();
this.GetInputs();
// Keep looping until app quits.
while (Created)
{
ProcessNextFrame(); // Process and draw the next frame.
Application.DoEvents(); // Make sure the app has time to process messages.
}
}
///
/// Restore all the surfaces, and redraw the sprite surfaces.
///
void RestoreSurfaces()
{
/*
SurfaceDescription description = new SurfaceDescription();
*/
displayDevice.RestoreAllSurfaces();
// We need to release and re-load, and set the palette again to
// redraw the bitmap on the surface. Otherwise, GDI will not
// draw the bitmap on the surface with the correct palette.
/*
pal.Dispose();
pal = null;
front.Palette = pal;
*/
// Release and re-load the sprite bitmap.
/*
surfaceLogo.Dispose();
surfaceLogo = new Surface(bitmapFileName, description, displayDevice);
ColorKey ck = new ColorKey();
surfaceLogo.SetColorKey(ColorKeyFlags.SourceDraw, ck);
*/
return;
}
///
/// Move the sprites, blt them to the back buffer, then
/// flips the back buffer to the primary buffer
///
private void ProcessNextFrame()
{
// Figure how much time has passed since the last time.
int CurrTick = Environment.TickCount;
int TickDiff = CurrTick - tickLast;
// Don't update if no time has passed.
if (0 == TickDiff)
return;
tickLast = CurrTick;
//Draw the sprites and text to the screen.
DisplayFrame();
}
private void DisplayFrame()
{
if (null == front)
return;
if (false == displayDevice.TestCooperativeLevel())
{
restoreNeeded = true;
return;
}
if (true == restoreNeeded)
{
restoreNeeded = false;
// The surfaces were lost so restore them .
RestoreSurfaces();
}
// Fill the back buffer with black.
// back.ColorFill(indexBlack);
back.ColorFill(System.Drawing.Color.White);
Double area = Math.PI*(m_RadiusOuter*m_RadiusOuter
- m_RadiusInner*m_RadiusInner);
Int32 n = (Int32) ((m_DotDensity / (128*128)) * area);
Int32 centery = this.Bottom / 2;
Int32 centerx = this.Right / 2;
back.ForeColor = Color.Black;
back.FillStyle = 1;
back.DrawCircle(centerx, centery, m_RadiusInner);
back.DrawCircle(centerx, centery, m_RadiusOuter);
back.FillStyle = 0;
// draw n random dots
for(Int32 ctr = 0; ctr < n; ctr++)
{
Double theta = (Double) m_Rnd.Next()/Int32.MaxValue * 2*Math.PI;
Double r = m_Rnd.Next(m_RadiusInner+1, m_RadiusOuter-1);
Double cx = centerx + r*Math.Cos(theta);
Double cy = centery - r*Math.Sin(theta);
back.DrawCircle((Int32) cx, (Int32) cy, (Int32) m_DotSize);
/*
dc.FillEllipse(m_DotBrush, (float) cx-m_DotSize,
(float) cy - m_DotSize,
(float) 2*m_DotSize, (float) 2*m_DotSize);
*/
}
// We are in fullscreen mode, so perform a flip.
front.Flip(back, FlipFlags.DoNotWait);
// pause for m_t1 ticks
System.Threading.Thread.Sleep(20);
// for(Int32 ctr = 0; ctr < 0xafffff; ctr++);
// blank the screen for m_t2 ticks
back.ColorFill(Color.White);
// We are in fullscreen mode, so perform a flip.
front.Flip(back, FlipFlags.DoNotWait);
System.Threading.Thread.Sleep(20);
}
///
/// Initializes DirectDraw and all the surfaces to be used.
///
private void InitializeDirectDraw()
{
SurfaceDescription description = new SurfaceDescription(); // Describes a surface.
displayDevice = new Device(); // Create a new DirectDrawDevice.
displayDevice.SetCooperativeLevel(this, CooperativeLevelFlags.FullscreenExclusive); // Set the cooperative level.
try
{
displayDevice.SetDisplayMode(screenWidth, screenHeight, 8, 0, false); // Set the display mode width and height, and 8 bit color depth.
}
catch(UnsupportedException)
{
MessageBox.Show("Device doesn't support required mode. Sample will now exit.", "UnsupportedException caught");
Close();
return;
}
description.SurfaceCaps.PrimarySurface = description.SurfaceCaps.Flip = description.SurfaceCaps.Complex = true;
description.BackBufferCount = 1; // Create 1 backbuffer.
front = new Surface(description, displayDevice); // Create the surface using the description above.
SurfaceCaps caps = new SurfaceCaps();
caps.BackBuffer = true; // Caps of the surface.
back = front.GetAttachedSurface(caps); // Get the attached surface that matches the caps, which will be the backbuffer.
front.ColorFill(Color.White);
/*
pal = new Palette(displayDevice, bitmapFileName); // Create a new palette, using the bitmap file.
pe = pal.GetEntries(0, 256); // Store the palette entries.
front.Palette = pal; // The front surface will use this palette.
*/
}
///
/// Clean up any resources being used.
///
protected override void Dispose(bool disposing)
{
if (disposing)
{
displayDevice.RestoreDisplayMode(); // Restore the display mode to what it was.
displayDevice.SetCooperativeLevel(this, CooperativeLevelFlags.Normal); // Set the cooperative level back to windowed.
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
//
// AnimatePalette
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(208, 69);
this.ForeColor = System.Drawing.Color.Black;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.KeyPreview = true;
this.Name = "AnimatePalette";
this.Text = "AnimatePalette";
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.AnimatePalette_KeyUp);
}
#endregion
///
/// Processes key up events.
///
private void AnimatePalette_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
// If the user presses the Escape key, the app needs to exit.
if (Keys.Escape == e.KeyCode)
Close();
}
}
}
|
|
|
|
|
Hi everyone,
Probably a real newbie question. How can I prepend to a file in C#. I have a file and I want to attach a header to it in front. Is there an easy API available to do something like this?
Thanks,
Sincerely,
Pankaj
Without struggle, there is no progress
|
|
|
|
|
As with everything in the .net framework, there is an API. In fact, there are several. And as you might imagine, there's more than one way to solve the problem. Start out with the MSDN library here and the exact algorithm is up to you. There are several useful examples.
|
|
|
|
|
how can i send an image from a pictureBox using a NetworkStream. I need the code, starting from declaring stream then socket and so on.
thnks....
|
|
|
|
|
Get a reference to the Image from PictureBox.Image and call Image.Save(Stream, ImageFormat) using an initialized NetworkStream as the first parameter and whatever ImageFormat you want to save it in. See the documentation in the .NET Framework SDK for more information on the mentioned classes. Reading the SDK is an important part of development. The information above should be more than enough.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
The information above should be more than enough.
Amen. Reading some questions I often think, "Did this person even try to search through MSDN or SDK documentation before asking everyone else?"
|
|
|
|
|
Hi,
I'm developing a Activex with .net C++, i try to add a method that has both external and internal name into this activex, so that I can call this method ourside of this activex. but I can notfind a way to do this job as we do this with classwizard in VC.
So pls help me, if you know how to solve this problem?
thx.
Ray
|
|
|
|
|
First of all, this is the C# forum, not the C++ forum. These are two completely different languages.
If you want to call the method both externally and internally, you should declare the method as public (which any COM method must be anyway). If you're using Managed C++, declaring a public method is not merely enough. You should define a class interface (.NET can automatically generate one for you, but this is not recommended for many reasons), attribute your interface and class with different GuidAttribute s, and several more attributes. You can call this public method internally with no problems.
For more information about exposing .NET Controls as COM components, see http://msdn.microsoft.com/library/en-us/cpguide/html/cpconexposingnetframeworkcomponentstocom.asp[^]. There's also a pretty good article here on CodeProject, but it's in C# (though if you truly understand the .NET Framework and not just a language, translating this should be no problem): http://www.codeproject.com/dotnet/nettocom.asp[^].
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi there
Is it possible to play flash swfs in c# forms?
and also interact with them?
does anyone know how to do this?
Regards
VisionTec
|
|
|
|
|
You can use flash player control using COM Interoperability.
Just customize your toolbox and add that control to current controls from COM tab(flash player should be already installed). And for interacting with swf file you can use FSCommand . For more information refer to FSCommand documentation.
Don't forget, that's Persian Gulf not Arabian gulf!
|
|
|
|
|
here is the solution:
1.right click on toolbox at general tab
2.select add new items
3.one popup window appears to add references
4.select COM tab
5.navigate to shock wave object select that
6.click on ok
7.u will see the flash object on General tab
|
|
|
|
|
Hi there
i an a newbie to c#
how can i change the date to british date format?(dd-mm-yy)
in my applications?
VisionTec
|
|
|
|
|