|
an example
using System;
class Player
{
public int neili;
public int tili;
public int jingyan;
public int neili_r;
public int tili_r;
public Player()
{
neili = 10;
tili = 50;
jingyan = 0;
neili_r = 50;
tili_r = 50;
}
//
public static Player operator ++(Player p)
{
p.neili = p.neili + 50;
p.tili = p.tili + 50;
p.neili_r = p.neili;
p.tili_r = p.tili;
return p;
}
//
public static Player operator +(Player p1, Player p2)
{
Player p = new Player();
p.neili = p1.neili +p2.neili;
p.tili = p1.tili + p2.tili;
p.neili_r = p.neili;
p.tili_r = p.tili;
return p;
}
public static void Main()
{
Player p1 = new Player();
Player p2 = new Player();
p1++;
Console.WriteLine(" {0}", p1.neili);
Player p = p1 + p2;
Console.WriteLine(" {0}", p.neili);
}
}
fay
|
|
|
|
|
hi,
I need to overload the assignment operator (=) not the + or ++.
I want to replace this:
<br />
for (i=1; i < m_nHeight; i++)<br />
for (j=0; j < m_nWidth; j++)<br />
{<br />
m_board[i-1, j].IsSet=m_board[i, j].IsSet;<br />
m_board[i-1, j].BrushType=m_board[i, j].BrushType;<br />
m_board[i-1, j].ForegroundColor=m_board[i, j].ForegroundColor;<br />
m_board[i-1, j].BackgroundColor=m_board[i, j].BackgroundColor;<br />
m_board[i-1, j].LinearGradientMode=m_board[i, j].LinearGradientMode;<br />
m_board[i-1, j].HatchStyle=m_board[i, j].HatchStyle;<br />
};<br />
by simply this:
<br />
for (i=1; i < m_nHeight; i++)<br />
for (j=0; j < m_nWidth; j++)<br />
m_board[i-1, j]=m_board[i, j];<br />
when I use the standard '=' operator the standard assignment fails. that's why I would like to rewrite this operator.
There is no spoon.
|
|
|
|
|
You can't overload the '=' operator. You can overload these[^] operators.
As a workaround, you could write an Assign, or CopyObject, whatever you want to call it, method into your class that accomplishes the same thing.
for (i=0; i < m_nHeight; i++)
for (j=0; j < m_nWidth; j++)
m_board[i-1, j].Assign(m_board[i, j]);
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I was thinking abuot it, but it's not the way I want
I guess C# allows to overload '='...
There is no spoon.
|
|
|
|
|
bouli wrote:
I guess C# allows to overload '='
As the topic that Dave linked stated - straight from the MSDN documentation - the = operator cannot be overloaded whether you like it or not.
You can, however, implement a cast operator so that if you want to customize assignment from another type (say an int to your struct), you could actually do something like this:
MyStruct s = (MyStruct)1;
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Heath!
ok, thanks,
the code will remain like that...
by the way delegates are very convinient :P I can combine them with DirectX
There is no spoon.
|
|
|
|
|
I did a windows usercontrol,when I put it to a webapplication, a error was happened what "System.Security.Permissions.FileIOPermission, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed."
Because I had some file operations in my windows usercontrol. It may be something question of framework's config?
|
|
|
|
|
Hello jzb, I never saw this error but I know that wincontrol cannot get client machine information for security reasons. Maybe .NET thinks that your try to manipulate files isn't safe.
Good luck...
Wender Oliveira
.NET Programmer
|
|
|
|
|
This is not true at all. See my reply to the original post.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You mean you embedded the Windows Forms UserControl in a web page? If you simply use it on the server, it won't matter.
If you embedded it in the web page (client-side), then the client's machine's code access security for .NET must be configured. Obviously you're using .NET 1.1 (since .NET 1.0 didn't grant applications in the Internet zone any permissions; thank you're truly as one of the people that asked Microsoft to change this in 1.0). You are only granted the following permissions, though:- Open File Dialog
- 10240 bytes of domain-isolated user storage
- Code execution
- Safe top-level windows
- Own clipboard
- Safe printing
So, you must add a code group to the user's machine. It is absolutely not recommended to chance the existing Internet_Zone code group, as this applies to all code run from the Internet, including that which you do not know and probably wouldn't trust.
Instead, add a code group. You can do this through a custom installer or running a command-line like so:
caspol.exe -m -ag All_Code -url http: Now, it's recommended you don't require FullTrust, but instead create a permission set with the privileges necessary. You can do this with XML files, custom actions, or other methods during installation.
For example, the GotDotNet Workspaces have a .NET UserControl that requires that you download a Windows Installer package (.msi; Visual Studio .NET can make these, as well as the Microsoft .NET Framework Configuration 1.1 snap-in, which is available in Administrative Tools). This installs a code group that allows the control to run. Restart Internet Explorer (since the CLR was already loaded by an unsuccessful attempt to load the control) and the control will load.
You can read more about code access security here on this site. Read Understanding Code Access Security[^]. You should also read the documentation in the .NET Framework SDK about code access security (although that article is comprehensive and very well-written, it's also good to hear straight from the horse's mouth). Read Securing Applications[^] and Configuring Security[^] in the .NET Framework SDK.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi gurus,
According to MS in the .NET Framework documentation, when they declare a delegate, they declare the parameters like that:
<br />
public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);<br />
Is it an obligation that the parameter must be a derived class from the EventArgs class?
Are such declarations valid?
<br />
internal delegate void GameStatusChangedEventHandler(object sender, GameStatus gameStatus, int score, int level, int linesDone);<br />
<br />
internal delegate void LevelChangedEventHandler(object sender, int level);<br />
<br />
public delegate void ReceivedMessageEventHandler(object sender, String message);<br />
Should I create a derived EventArgs class for each delegates?
Thanks.
There is no spoon.
|
|
|
|
|
It's common to use the sender-eventargs-pattern, but it's not binding.
Also your custom eventargs doesn't have to be derived from the System.EventArgs class, but again this is a common approach.
www.troschuetz.de
|
|
|
|
|
ok, thanks.
There is no spoon.
|
|
|
|
|
I have a panel which is located in a AutoScroll-enabled panel. The
problem is when I click in the panel, the contining panel
automatically scrolls to position (0,0).
How can I get around this annoying behaviour?
|
|
|
|
|
i want to create a form and show it in a thread. Like this code:
Thread th = new Thread(new ThreadStart(t));
t.Start();
void t()
{
Form2 f2 = new Form2();
f2.Show();
}
-----------------------
But it doesnt work. How to deal with it ?
|
|
|
|
|
what is happening is your thread starts and shows the form and then it's done showing the form and the thread "dies". Do you see the form flash real fast?
I believe you have to make your form modial.
AND to do this, use .ShowDialog() instead of .Show()
happy birthday
/\ |_ E X E GG
|
|
|
|
|
Hi, I'm going to need to execute SQL statement against MySQL, Oracle and MS SQL, just wondering if there's anything from Source Forge which help abstract away the detail.
Thanks.
Norman Fung
|
|
|
|
|
norm wrote:
just wondering if there's anything from Source Forge
Then why not look there ?
Personally, I'd write stored procedures for each database, and then have the differences of SQL version within the stored procedures. Then your data access code does not have to change, the changes are within the layer that requires it: the database itself.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
That'd be ADO.NET itself. It already is an abstract API. In .NET 2.0, a provider pattern will make it more abstract, but for now you can simply use the System.Data.OleDb namespace. MySQL, Oracle, SQL Server, and many others (including Excel, Fox Pro, ...) all have OLE DB drivers, so all that would change is your connection string (although some providers don't provide all the same features, so make sure you do some exception handling).
So, just for a crude example:
public enum DbProvider
{
MySQL,
Oracle,
MSSQL
}
public OleDbConnection GetConnection(DbProvider provider)
{
OleDbConnection conn = null;
switch (provider)
{
case DbProvider.MySQL:
conn = new OleDbConnection("Provider=MySQLProv;Data Source=mydb;" +
"User ID=me;Password=12345;");
break;
case DbProvider.Oracle:
conn = new OleDbConnection("Provider=msdaora;Data Source=oradb;" +
"User ID=sa;Password=;");
break;
case DbProvider.MSSQL:
conn = new OleDbConnection("Provider=SQLOLEDb;Data Source=DBSERV1;" +
"Initial Catalog=northwind;Integrated Security=SSPI;");
break;
}
return conn;
} This isn't the best way to do it, though. By using an enum, you basically hard-code the choices (unless you want to). A better way might be to put the connection string in the .config file so you can change it to use a different provider at any time (though changes to the .config file in all but ASP.NET requires that you restart the application; even the ASP.NET web application must be restarted, though this happens automatically by a worker process).
You can also use the typed classes (like System.Data.SqlClient for SQL Server, System.Data.OracleClient for Oracle, etc.) and refer to them by their abstract base classes, or the interfaces (like IDbConnection and IDbCommand ). You can find more providers online by doing a google search.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I am using a C# code to insert a data in SQL Server.
Stored Procedure which inserts data is created as :
-----------------------------------------
CREATE PROCEDURE spAddNewAddress
@AddressID char, @AddressType char, @HomeAddress varchar,
@City varchar, @State varchar, @Zip varchar, @Country varchar
AS
INSERT INTO Address VALUES(@AddressID, @AddressType, @HomeAddress,
@City, @State, @Zip, @Country)
GO
-----------------------------------------
Then C# code which inserts data is :
public void insertNewData(Address a)
{
try
{
//create sql string
sqlAddress = "spAddNewAddress";
//Open the connection
scAddress.Open();
//create the cmdAddress
cmdAddress = new SqlCommand(sqlAddress, scAddress);
cmdAddress.CommandType = CommandType.StoredProcedure;
//adding parameters for command
cmdAddress.Parameters.Add("@AddressID",SqlDbType.Char,10);
cmdAddress.Parameters["@AddressID"].Value = a.getAddressID();
cmdAddress.Parameters.Add("@AddressType",SqlDbType.Char,1);
cmdAddress.Parameters["@AddressType"].Value=a.getAddressType();
cmdAddress.Parameters.Add("@HomeAddress",SqlDbType.VarChar,100);
cmdAddress.Parameters["@HomeAddress"].Value=a.getHomeAddress();
cmdAddress.Parameters.Add("@City",SqlDbType.VarChar,25);
cmdAddress.Parameters["@City"].Value=a.getCity();
cmdAddress.Parameters.Add("@State",SqlDbType.VarChar,25);
cmdAddress.Parameters["@State"].Value=a.getState();
cmdAddress.Parameters.Add("@Zip",SqlDbType.VarChar,25);
cmdAddress.Parameters["@Zip"].Value=a.getZip();
cmdAddress.Parameters.Add("@Country",SqlDbType.VarChar,70);
cmdAddress.Parameters["@Country"].Value=a.getCountry();
cmdAddress.ExecuteNonQuery();
//clean up code
cmdAddress.Dispose();
scAddress.Close();
}
catch(Exception ex) { Console.WriteLine(ex.ToString() ); }
-----------------------------------------
Code executes with out any trouble (no breaks/errors or exceptions).
Only problem is when i try to view this data in SQL Server's Enterprise Manager, only first letter of the inserted data appears.
e.g : If inserted AddressCode = A03C0001 then in the EnterPrise Managers it appears as :
"A".
Another strange thing, if try to query database with AddressCode="A", it generates error (that data don't exists).
Although If you try to query database with AddressCode="A03C0001", then it extracts complete information as inserted.
any suggestions!!!!!
|
|
|
|
|
ronin1770 wrote:
cmdAddress.Parameters.Add("@AddressID",SqlDbType.Char,10);
cmdAddress.Parameters["@AddressID"].Value = a.getAddressID();
cmdAddress.Parameters.Add("@AddressType",SqlDbType.Char,1);
cmdAddress.Parameters["@AddressType"].Value=a.getAddressType();
I didn't see AddressCode from your code, is it AddressID? You may have mixed up the order of the columns AddressID and AddressType. To prevent this, your insert statement should look like the following:
insert into Address(FieldName1,FieldName2,...) values(FileValue1,FieldValue2,...)
My articles and software tools
|
|
|
|
|
HI,
i tried the way u suggested... it was never the order of fields
but no success so far...
it is beyond my understanding... probably problem with SQL Server Instance
|
|
|
|
|
If the database returns the data you asked it to insert, then it is there, the problem can only be in how you are viewing the data within SQL server. If you do a select of everything in the table, what does that look like ?
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
Since You Have Given DataType As Char For AddressCode and AddressType, It will store Only one char. Better user Varchar(size) in the creation of Store proc, It will Execute fine and u can see what u have inserted.
|
|
|
|
|
The problem isn't your code but your procedure. When you declared "varName varchar" SQL see it like one char variable. If you want to use more than one char you must to declare "varName varchar(10)" for a 10 chars variable.
Cya
Wender Oliveira
.NET Programmer
|
|
|
|
|