返回整个班级 [英] Return an entire Class

查看:67
本文介绍了返回整个班级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB.net中,可以返回整个类并从另一个类访问该类中的变量.我在理解如何在C#中实现这一目标上遇到了最困难的时光.

在VB.net中,我可以像这样做某事...

函数getFinal(ByVal dblONE,ByVal intTWO)

昏暗的report1作为新的Class2_REPORT

report1.Summed_TOTAL = dblONE + intTWO

report1.Subtracted_TOTAL = dblONE-intTWO

返回报告1

最终功能


我无法在C#中执行相同的操作,因为该函数期望从其他类调用此方法,因此我返回某些内容.但是我不能在C#中返回多个值,因此我的理解是我需要返回整个类".任何帮助表示赞赏.一段时间以来,我一直在寻找解决方案,但找不到任何东西.

In VB.net one is able to return an entire class and access the variables in the class from another class. I am having the most difficult time understanding how to accomplish this in C#.

in VB.net I can do somehting like this...

Function getFinal(ByVal dblONE, ByVal intTWO)

Dim report1 as New Class2_REPORT

report1.Summed_TOTAL = dblONE + intTWO

report1.Subtracted_TOTAL = dblONE - intTWO

Return report1

End Function


I am unable to do the same thing in C# since the function is expecting that I return something since I am calling this method from another class. But I cant return more than one value in C# so my understanding is that I need to return an "entire class". Any help is appreciated. I have been searching for a solution to this for a while and cannot find anything.

推荐答案

实际上,您可以在 C#中返回(引用)整个对象,请尝试:
Actually you can return (a reference to) an entire object in C#, try:
class MyPoint
 {
    public int x, y;
 };
 class Program
 {
     static public MyPoint getOrigin()
     {
         MyPoint p = new MyPoint();
         p.x = p.y = 0;
         return p;
     }
     static void Main(string[] args)
     {
         MyPoint O=getOrigin();
         Console.WriteLine("O=({0},{1})", O.x, O.y);
     }
 }


U在C#中可以做同样的事情.请记住,在C#中,类的实例是引用.

U can do the same thing in C#. Remember that in C# instances of a class are references.

class A{
    public static int a;
    public static double b;

    public A(){
    //constructor implementation
    }
}


A myFunction(int parameter1, double parameter2){
    A result = new A();
    A.a = parameter1;
    A.b = parameter2;
    return A;
}




此函数应返回对类A实例的引用.

希望这能回答您的问题.




This function should return a reference to an instance of class A.

Hope this answers your question.


这篇关于返回整个班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆