+ 1
Where did I go wrong?
I am trying to change the name attribute of ins1 instance.. but its showing an error.. Why?? https://code.sololearn.com/cQbTD5n7Tig1/?ref=app
16 Antworten
+ 3
You don't need the "out" specifiers. Without them your program works just fine.
https://code.sololearn.com/cWk2jLO4TaWp/?ref=app
+ 2
Change out to ref.
+ 2
Samael Morningstar
Yes, it creates a completely new object to get the job done because your out parameter is a class.
The first object is probably disposed by the garbage collector, what it does is to change the object completely with its field assigned with a name before leaving the method.
+ 2
Samael Morningstar
Seems like you're trying to practice with classes.
I'd suggest declaring the method in that class rather than in Program where Main exists, at least this is how I would write it:
class Person
{
public string name;
public string Name { get { return name; } set { name = value; } }
public void UpdateName(string name)
{
Name = name;
}
}
class Program
{
static void Main()
{
Person p1 = new Person();
p1.UpdateName("Mehedi");
Console.WriteLine(p1.Name);
}
}
My brain is kinda burned here, have a good day.
+ 2
Samael Morningstar
No problem at all 👍
Let me know if you have more questions
+ 2
Samael Morningstar
Every variable is unique so you probably should change the name of the other variable
+ 1
Samael Morningstar
The memory address of ins1 is never updated/changed, which is why it doesn't work, instead of passing the class as a parameter, pass in the field of the class instead, Here's a workaround:
void ChangeName(out Method method)
{
method = new Method();
method.name = "Mehedi";
}
Here's the solution I was talking about by passing the field directly:
void ChangeName(out string name)
{
name = "Mehedi";
}
And call it like this in the Main method:
instance1.ChangeName(out ins1.name);
+ 1
Nick 😂😂 Thanks a lot, Nick..
I am playing with methods..
+ 1
Nick Hm.. I may have more.. But for now, I will try to understand what you have said and go deeper.. Thanks
+ 1
Samael Morningstar
Idk if I can share my Discord info here because it's very hard to explain here on Sololearn
+ 1
Check your messages Samael Morningstar
+ 1
On Sololearn, there's messages
0
CarrieForle But why this code isn't not working??
0
Nick
https://code.sololearn.com/crpZDE1F3B57/?ref=app
Why this time it's not creating a new object?? (With the same name)
0
So, the thing here is going on is..
We declared and instance which refers to an object inside Main method..
And our other methods didn't receive it because it's a local variable..
Though we can change it via methods with ref type parameter or value parameter..
But I want to do it with out type parameter..
Nick, what if we send the address of our first object to ins (my first codebit) or (y--- my second codebit) instead of creating a new object??.. Ah.. that can be done with ref type parameter..
But I want to know about out type parameter
Note: Let's do this experiment with my second codebit.. easier to read
https://code.sololearn.com/crpZDE1F3B57/?ref=app
0
Nick
It's meaningless doing all these.. we can do in other ways..
But my question is.. out type parameter helps us to console something, doesn't it?? or whatever.. but why it's making problem while doing the same thing, which we did with other type parameters..