| |

VerySource

 Forgot password?
 Register
Search
View: 1746|Reply: 10

Ask C # about operator overloading?

[Copy link]

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-13 08:00:01
| Show all posts |Read mode
Hello everyone, I now encounter a problem of operator overloading, please help to solve:

public class Test
{
    public int Value;
}


public class Program
{
    static void Main (string [] args)
    {
        Test t = new Test ();
        Console.WriteLine (t.Value); // such code OK

        Console.WriteLine (Test.Value); // such code error

        // This is my question, how do I implement a field (or property) by using
        // class.xxx and object.xxx
        // No error (but '.' Doesn't seem to be overloaded)
    }
}

// thanks for your help
Reply

Use magic Report

0

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-4-12 09:45:02
| Show all posts
example:
using System;

namespace prjCx
{
/// <summary>
/// Summary description of clsGlobalSingleton.
/// </ summary>
public class clsGlobalSingleton: clsGlobal
{
private static clsGlobal clsglobal = null; // Declare static class;

public clsGlobalSingleton ()
{
//
// TODO: Add constructor logic here
//
}

/// <summary>
/// Hungry single mode returns an instance of class clsGlobal
/// </ summary>
public static clsGlobal GlobalInstance
{
get
{
if (clsglobal == null)
{
clsglobal = new clsGlobal ();
}
return clsglobal;
}
}

}
}
Reply

Use magic Report

0

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-4-12 15:15:01
| Show all posts
public class clsAA
{

  public static void LogAA ()
  {
  ...
  }
}

You can use clsAA.LogAA ();
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-4-12 17:45:02
| Show all posts
Curiously asked, why is there such a demand?
Reply

Use magic Report

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2020-4-14 18:45:01
| Show all posts
This is not an operator overload and can be set as a static variable!
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-15 10:45:01
| Show all posts
Set to a static variable to achieve.
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-4-23 03:30:01
| Show all posts
// I cannot set it as a static variable.
// For example, I have a class, customer
Customer customer = new Customer ();
customer.Name = "Test";
customer.Save ();
// In this way, I create a customer object, set his name, and save it.
// Then I wrote a query to find him out
Customer customer2 = new Query <Customer> (Customer.Name.Like ("Test")). GetEntity ();
if (customer2! = null)
{
    Console.WriteLine (customer2.Name);
}
// Screen output: Test


Please see the query condition code: Customer.Name.Like ("Test"), here I want to use the class. Field (or property)
Excuse me, how do I implement this Customer class, the most important thing is that the class. Field and object. Field point to a stuff at the same time
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 Unknown

 Author| Post time: 2020-4-23 17:15:01
| Show all posts
class Customer
{
public string Name;
public static string Name;
}

I can't figure out why the above code is not compiled by Microsoft, a static, a non-static, the well water does not violate the river water, and there will be no impact, halo
Reply

Use magic Report

0

Threads

32

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-5-2 08:45:02
| Show all posts
Not possible, personal opinion.
Static: There is only one copy in the same application domain.
Non-static: one copy for each instance.
If it can be written like this, how does the compiler know which one is called? Function overloading and signatures can be distinguished, this is exactly the same, how to distinguish.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-5-3 18:00:01
| Show all posts
************************************************** **********************************
Please see the query condition code: Customer.Name.Like ("Test"), here I want to use the class. Field (or property)
Excuse me, how do I implement this Customer class, the most important thing is that the class. Field and object. Field point to a stuff at the same time
************************************************** **********************************
Class. Field: There is only one such static,
Object. Field: How many objects can you create in a class, and which object's field should the class. Field point to? Do you say this is possible?
Now that you have queried out customer2, why not directly use the customer2 object to get the Name, use class .Name
You actually want to assign only the value in front of the save and directly call it later. In that case, you can only use one static variable!
Customer customer = new Customer ();
customer.Name = "Test";
Customer.staticName = "Test" // so you can use this variable when querying
customer.Save ();
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list