|
Is it possible to create an instance of an object that has been passed through as a method parameter with a ref keyword ?
public class BasicClass{...}
public class NewClass: BasicClass{...}
...
Something(ref NewClass)
...
public BasicClass Something(ref BasicClass aclass)
{
return new aclass();
}
Regards, Desmond
|
|
|
|
|
You should look at making a class factory. In particular you should look at Reflection and the Activator.CreateInstance() method.
- Nick Parker My Blog
|
|
|
|
|
Something like
return (BasicClass)Activator.CreateInstance(aclass.GetType()); should work.
Charlie
if(!curlies){ return; }
|
|
|
|
|
Could BasicClass implement ICloneable so that in method Something, you return aclass.Clone() ?
|
|
|
|
|
I'm not going to answer your question since the three people above did, but I did want to comment on your use of the ref keyword: it isn't necessary! An instance of a class is already a reference. There is no need to declare the parameter as a ref parameter and this can actually lead to problems with regard to marshaling.
See Value vs Reference Types in C#[^] (for all of .NET, actually) for more information.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|
For example, when marshaling to a native function via P/Invoke you actually end up passing a pointer to a pointer.
This also creates memory overhead both in the same AppDomain and when marshaling to different AppDomains.
Just understand that there are reference types and value types. A reference type is just that - a reference. You don't need to ref it unless you need to pass a double-pointer. This is all discussed in the .NET Framework as well and is fundamental in programming for the .NET Framework.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I want to create these attributes dynamically in a datagrid from the codebehind.
<asp:datagrid id="dataGrid1" runat="server"
="" bordercolor="black" borderwidth="1" gridlines="Both" cellpadding="3" cellspacing="0" headerstyle-backcolor="#aaaadd">
I'm was thinking of starting like this:
FindControl("DataGrid1")
But there are no members for attributes.
Please help
Yours sincerely
Andla
|
|
|
|
|
First: This should be post on ASP.NET page:
Second:
There are many place to set tyles depend on what style you want to set,these are some hints:
DataGrid.Columns[index].ItemStyle<br />
DataGrid.Columns[index].HeaderStyle<br />
DataGrid.Attributes<br />
DataGrid.CellPadding<br />
DataGrid.CellSpacing
Mazy
"Improvisation is the touchstone of wit." - Molière
|
|
|
|
|
Hi Andla,
The FindControl method returns a generic Control, so you need to caste it to get at the DataGrid's Attribute collection.
So I'd try something like:
<br />
DataGrid DGTmp=(DataGrid)FindControl("DataGrid1");<br />
Then you can manipulate the attributes like this:
<br />
DGTmp.Attributes["Border"]="1";<br />
Notice the use of "Border" rather than "BorderWidth". The Attribute property is written directly to the client, so it does not map exactly to the attributes listed in the tag in the aspx file.
Someone smarter than me will have to explain (to both of use) why this is so...
Hope this helps,
Bill
|
|
|
|
|
HELP please someone I'm using MSComm with C#.....at the moment I'm communicating via a null modem connection between 2 PCs. At the moment I'm just sending out some bytes from one PC to another PC... Got the communication to work... all is well.
The problem; I only expect something to be sent from one PC to other when I press a button on the sending PC which then uses the command object.Output= new byte [] {a number}; to send a byte to the other computer.... the recieving computer in turn recieves this and displays it via the ONComm event handler. The ONComm event on the recieving PC is raised when a byte arrives at the recieving PC serial port on pressing a button on the sending PC.... HOWEVER the ONComm Event is also raised (on the recieving PC)when I start up and exit the sending application. I just can't understand this! This could obviously be a really big problem... Can someone explain or have a solution to this? I would be grateful... I just can't seem to stop this event from raising when the sending application starts up or shuts down... This could be reallly disasterous if the sending PC sends something on start up (ie without me even having instructed the sending application to output a signal by pressing a button) when connected to a PICmicrocontroller which shouldn't recieve that signal....
Here is how i set up the 2 PCs;
com.CommPort = 1;
if (com.PortOpen) com.PortOpen = false;
com.RThreshold = 1;
com.Settings = "9600,n,8,1";
//com.DTREnable = true;
com.Handshaking = MSCommLib.HandshakeConstants.comNone;
com.InputMode = MSCommLib.InputModeConstants.comInputModeBinary;
com.InputLen = 0;
com.NullDiscard = false;
com.OnComm += new System.EventHandler(this.OnComm);
com.PortOpen = true;
Here is the ONComm event handler on the recieving PC which is connected with a null modem cable;
// MSCommLib OnComm Event Handler
private void OnComm(object sender, EventArgs e)
{
//Message box 1
MessageBox.Show("recieved something 1");
switch (com.CommEvent)
{
case (short)MSCommLib.OnCommConstants.comEvReceive:
//Message box 2
MessageBox.Show("recieved something 2");
byte[] x = (byte[]) com.Input;
foreach (byte byt in x)
{
strrecvd += (char) byt;
//Message box 3
MessageBox.Show(strrecvd);........................
The only Message box that should show on the recieving PC is 3 and that only when I press a button to send a byte on the sending PC... However on the inclusion of message boxes 1 and 2 I noticed that when I started up or shut down the sending application they would show on the recieving PC....
anyone?
I would never have known this happens had i not put a message box there to check something else...SCARY!
maria (phillips_maria2@hotmail.com)
|
|
|
|
|
I am converting some code written by someone else in VB(.NET?) to Visual C#. I have figured out most of the weird language, but I am stuck on one problem. Here is the VB code (you may recognize it from MSDN):
Private Sub ConfigureContextMenu(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Popup<br />
Dim ctl As TextBox = CType(Me.SourceControl, TextBox)<br />
MenuItems .Enabled = ctl.CanUndo<br />
MenuItems(7).Enabled = (ctl.SelectedText <> ctl.Text)<br />
MenuItems(2).Enabled = (ctl.SelectionLength > 0) ' cut<br />
... (continues)<br />
How do I translate the usage of the Handles keyword in VB to C#. I know there is no handles keyword.
Thanks in advance for any help,
Josh Koppang
There's a fine line between confidence and arrogance.
|
|
|
|
|
Josh,
I am unfamiliar with the rest of the code...so please take this with the appropriate grain of salt.
It looks like you want your method (ConfigeContextMenu) to handle the Popup event on MyBase (a ContextMenu?)
In C# you do would something like:
MyBase.Popup+=new EventHandler(ConfigContextMenu);
Bill
|
|
|
|
|
Is their any way to return the url the moment the user type it in the browser?
or
Is their any way to return the url typed in the current instance of the browser?
====================
I am sorry, really I did not get you enough information.
I am developping a software (windows appication) with C# that enables users to connect to the internet. I want to restrict the sites that the user can enter by preventing him from accessing a specified web sites that is determine in the database. So I need to know what the user type in the browser or what is typed in the browser assuming that this site is opened from a link.
Also I need to restrict what the user can download from the internet.
can any one help me?
Thank You.
|
|
|
|
|
Return it to where?
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
Yes there is, but as Colin was getting at, from what do you want to get the URL? From a BHO? A separate program? What?
If you already have an instance of the InternetExplorer out-of-process automation server (or for the WebBrowser ActiveX control that can be interop'd for use with .NET), you can simply get this using the LocationURL property of the IWebBrowser2 interface that the WebBrowser control and InternetExplorer automation server implement.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I am sorry, really I did not get you enough information.
I am developping a software (windows appication) with C# that enables users to connect to the internet. I want to restrict the sites that the user can enter by preventing him from accessing a specified web sites that is determined in the database. So I need to know what the user type in the browser or what is typed in the browser assuming that this site is opened from a link.
Also I need to restrict what the user can download from the internet.
can any one help me?
Thank You.
|
|
|
|
|
Hi,
I would appreciate if you could help me with my source code.
I want to create web user controls dynamically each time i press the button.
But i only get one control each time. If i use a 'for'-loop then it is no problem.
public Control c1;
private void Button1_Click(object sender, System.EventArgs e)
{
c1 = LoadControl("WebUserControl1.ascx");
((WebUserControl1)c1).Color="green";
FindControl("WebForm1").Controls.Add(c1);
FindControl("WebForm1").Controls.Add(new HtmlGenericControl("hr"));
}
Yours sincerely
Andla
|
|
|
|
|
I'm not sure I fully understand what you are looking for, but here's my best shot at the information you might find useful.
Because ASP.NET is stateless it doesn't remember from one page request to the next what happened. That is why there are various mechanisms to help it remember. (like Viewstate, Session and Application variables).
When you create any control dynamically on a page you have to manually create a mechanism to remember it was created, so than on the postback you can dynamically re-create it again. (This sounds like a real pain - and believe me it can be a great source of bugs if care is not taken).
So, what can you do? One way might be to store the information about the control in to the Session object, then in the page load you can extract that information and recreate all the dynamic controls you created the last time so that the page is back to the way it was before. Then when the button click event is fired and you go to create a new dynamic control all the other previously created dynamic controls are already there so you get the effect that it is adding more controls.
--Colin Mackay--
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
To get the URL that is currently beign typed into the Address Bar ? Did you mean that ? I think by normal methods it's not possible. Maybe only by scanning keyboard input when Address Bar is focused (if you can determine witch control of an application is being focused).
|
|
|
|
|
Sorry, I posted this accedentaly to a wrong thread. I ment to post this to a thread above "Is there a way to ....". Sorry.
|
|
|
|
|
hi
i make a win app , iwant to make the action of enter press in keyboard as the action of button OK in my form i dont Know how;)
|
|
|
|
|
Hi,
The Form has an "AcceptButton" property that will do let you this.
Viewing the form in Design mode, look at the properties window. The AcceptButton property will have a list of all the buttons on the form. Pick you one you want.
Or you can set it in code:
<br />
Button MyButton=new Button();<br />
...<br />
Form.AcceptButton=MyButton<br />
According to the Help: "You can use this property to allow the user to quickly navigate a simple form by allowing them to simply press the ENTER key when they are finished instead of manually clicking the accept button with their mouse."
In a nutshell, when the user hits enter, the Click event on the specified button is fired.
Hope this helps,
Bill
ps: there is a CancelButton property that sets a default button for ESC.
|
|
|
|
|
I've made an implementation of WH_KEYBOARD_LL, however this is only supported in the NT side of windows. I need compatibility with 9x so I'll have to go with WH_KEYBOARD. However, the WH_KEYBOARD isn't system-wide by deafult and has to reside in a seperate library. Would it be a good idea to do this in C# and are there any suggestion and pointers to examples/articles on this?
|
|
|
|
|
There's several articles on hooks here on CodeProject. Just search.
Using a system hook is never a good idea unless you really need to. Using .NET is probably not a good idea either, especially for keyboard and mouse hooks because of the sheer volume of messages posted. If you only need this for an application, take a look at the IMessageFilter interface and the Application.AddMessageFilter method for a much easier, application-specific means.
Microsoft MVP, Visual C#
My Articles
|
|
|
|