PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 6, 2022

[FIXED] How to use ref variable outside calling method in c#

 July 06, 2022     c#, pass-by-reference, unity3d     No comments   

Issue

How can I use ref variable outside the calling method in C#.

I will explain what I would like to acheive.

I have a method inside a class(MyClass), which looks like

private MyObject myobj;
public ref MyObject GetMyObject()
{
    return ref myobj;
}

Now I am fetching the variable inside another method from different class(FetchClass)

private MyObject fetchObj;
void Start()
{
    fetchObj = ref MyClass.instance.GetMyObject(); //getting error here
}

I like to use fetchObj in different method in the same class. Thats the reason I have declared outside. But I am getting error in Start method.


Solution

Since your MyObject is a class you can use an expression bodied read-only property:

private MyObject fetchObj => MyClass.instance.GetMyObjectNotByRef();


Answered By - Guru Stron
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing