C
C#β€’4w ago
Ale

Acces data from another form

I don't understand why in this example it doesn't show the name of the car, if someone can explain it to me in more detail or tell me how to make it work. It only appears that,, the car is,,. Thank you very much! https://paste.ofcode.org/35K3CPDgRTAqSEnwsaWGB57
72 Replies
leowest
leowestβ€’4w ago
because car on Form2 is a complete new instance of car you are creating 2 separated instances of car on each form
MODiX
MODiXβ€’4w ago
leowest
REPL Result: Success
var a = new FormA();
a.Print();
var b = new FormB();
b.Print();

public class FormA
{
private Car car;
public FormA()
{
car = new Car("Porchse");
}

public void Print()
{
Console.WriteLine(car.Name);
}
}

public class FormB
{
private Car car;
public FormB()
{
car = new Car("BMW");
}

public void Print()
{
Console.WriteLine(car.Name);
}
}

public record Car(string Name);
var a = new FormA();
a.Print();
var b = new FormB();
b.Print();

public class FormA
{
private Car car;
public FormA()
{
car = new Car("Porchse");
}

public void Print()
{
Console.WriteLine(car.Name);
}
}

public class FormB
{
private Car car;
public FormB()
{
car = new Car("BMW");
}

public void Print()
{
Console.WriteLine(car.Name);
}
}

public record Car(string Name);
Console Output
Porchse
BMW
Porchse
BMW
Quoted by
<@1102729783969861782> from #bot-spam (click here)
Compile: 520.063ms | Execution: 57.166ms | React with ❌ to remove this embed.
leowest
leowestβ€’4w ago
in order to share the same instance of car between both forms you need to pass that instance of car from FormA to FormB, be it via calling a method or other means. The most common way is called dependency injection, where u pass it via the constructor as a requiriment of the FormB.
public class FormB
{
private readonly Car _car;
public FormB(Car car)
{
_car = car;
}

public void Print()
{
Console.WriteLine(_car.Name);
}
}
public class FormB
{
private readonly Car _car;
public FormB(Car car)
{
_car = car;
}

public void Print()
{
Console.WriteLine(_car.Name);
}
}
and to pass it you would do something like:
var formB = new FormB(car);
formB.Show();
var formB = new FormB(car);
formB.Show();
@Ale does that make sense to u?
Ale
Aleβ€’4w ago
OMG it s works sjdnfsakdfjnsakf , how i find this in internet to seach dependecy injection thank you soo much
leowest
leowestβ€’4w ago
$di
MODiX
MODiXβ€’4w ago
Dependency injection is really just a pretentious way to say 'taking an argument' See: http://blog.ploeh.dk/2017/01/27/dependency-injection-is-passing-an-argument/
Dependency injection is passing an argument
Is dependency injection really just passing an argument? A brief review.
leowest
leowestβ€’4w ago
might be a bit too much to read let me see if there is a simpler version
leowest
leowestβ€’4w ago
ah there we go this one is a bit easier to explain it https://www.tutorialsteacher.com/ioc/dependency-injection
leowest
leowestβ€’4w ago
the name is a bit scary but the concept as I explained above is really simple and above that u also have libraries that further implement DI which is a more complex concept
Ale
Aleβ€’4w ago
it really seems very difficult. Thank you very much I'm already waiting to look over the documentation you sent
MODiX
MODiXβ€’4w ago
If you have no further questions, please use /close to mark the forum thread as answered
Ale
Aleβ€’4w ago
I have another question related to this topic, if I have the form1 class and then I have form2 and I need those parameters in form3, do I do the same? form1 for form3
leowest
leowestβ€’4w ago
yes what is form3
Ale
Aleβ€’4w ago
another form class where I want to use parameters from formA and among the classes there is the form B class
leowest
leowestβ€’4w ago
Depending on whether u want to access formA method and car, u could instead pass a reference of formA which would give u access to both too so instead of say
private readonly Car _car;
public FormB(Car car)
{
_car = car;
}
private readonly Car _car;
public FormB(Car car)
{
_car = car;
}
You would do
private readonly Form1 _parent;
public FormB(Form1 parent)
{
_parent = parent;
}
private readonly Form1 _parent;
public FormB(Form1 parent)
{
_parent = parent;
}
and in Form1 u would have Car as
public Car Car { get; set; }
public Car Car { get; set; }
so u could access _parent.Car.Name or _parent.Class1.Message or some method u made public in Form1 u might also want to use ShowDialog instead of Show so that the user needs to finish interacting with Form2 before it can do any action on Form1 again etc
Ale
Aleβ€’4w ago
but if I don't need parameters from FormA in Form B, I still can't jump directly to FormC, right?
leowest
leowestβ€’4w ago
what u mean jump to formc? in order to have formc u need to initialize formc and call show from somewhere be it program.cs or forma
Ale
Aleβ€’4w ago
it is a form that separates the form1 or formA from where I need the values ​​form3 or formC https://paste.ofcode.org/nTgXXEdYTi25vAUwNwpZYD i make an example similar but i add a form class between two forms
leowest
leowestβ€’4w ago
can u like show some images? it might help me provide a better answer
Ale
Aleβ€’4w ago
No description
No description
No description
No description
No description
leowest
leowestβ€’4w ago
no I mean images of your forms not the code because all this chaining of a to b to c could have been different depending on what they are could have been just like a menu with a panel that replaces usercontrols instead of opening new windows there are many ways to do things
Ale
Aleβ€’4w ago
from now on I say it's not who knows what
No description
No description
No description
leowest
leowestβ€’4w ago
some make more sense then others as being intuitive so they are like pages to u? u click confirm, it should show the arrow to next?
Ale
Aleβ€’4w ago
yes
leowest
leowestβ€’4w ago
imagine this then instead of window u create UserControl then u add a panel you do something like
var input = new InputForm();
panel.Add(input);
var input = new InputForm();
panel.Add(input);
u press confirm, now it changes from InputForm to say next page
var next = new NextForm();
panel.Remove(input);
panel.Add(next);
var next = new NextForm();
panel.Remove(input);
panel.Add(next);
this would all be working in a single window as if u were walking pages note: this is just hypothetical code, I have not tested I dont usually use winform, but I know its possible
Ale
Aleβ€’4w ago
Well, I intentionally put 3 pages because I don't know how to transfer the data from page 1 to page 3 I know that I could do better on one page but I can understand the transfer of data when a page does not need that data and is fixed in the middle. It seems very complicated to me
leowest
leowestβ€’4w ago
u mean this
No description
leowest
leowestβ€’4w ago
I honestly dont understand why u have it
Ale
Aleβ€’4w ago
yes this is the middle page
leowest
leowestβ€’4w ago
why can't u go from A to C
Ale
Aleβ€’4w ago
umm idk 😦
leowest
leowestβ€’4w ago
:waaaaaaaaaahhhhhh: it would make more sense no? even if u reverse yoru forms first form having say a label and a button called create car
Ale
Aleβ€’4w ago
yea i know 70 / 5.000 but I wanted to know how I can do this if there is an extra class in the middle
leowest
leowestβ€’4w ago
and once u create the car on the form2 it returns to form1 and upsate the label with the car name
Ale
Aleβ€’4w ago
ignore 70/5000
leowest
leowestβ€’4w ago
by extra class you're referring to class Method?
Ale
Aleβ€’4w ago
I do this thing above because I want to make a project and I made a sketch in my head in which the first form enters some data after the second form does something else and the third form I need data from the first form for a method form2
leowest
leowestβ€’4w ago
well because you're going into a chain i.e.: u open form2 from form1 and form3 from form2 then yes u need to chain passing that reference unless you create a singleton of car which I am not sure I would recommend at this point but if u use, usercontrol as pages and panel then it would be slightly different
Ale
Aleβ€’4w ago
so it is not done as shown the first time?
leowest
leowestβ€’4w ago
this is sort of what u have right now
Ale
Aleβ€’4w ago
that's how I want it to work like you did above
leowest
leowestβ€’4w ago
then yes u need to do what I explained earlier that is what I am doing above
Ale
Aleβ€’4w ago
can you show me the code pleaseeeeeeeeeeeee
leowest
leowestβ€’4w ago
I already have πŸ‘†
Ale
Aleβ€’4w ago
i made this in formA var formB = new FormB(car); formB.Show(); and in form B i put this var formC = new FormC(car); formC.Show();
leowest
leowestβ€’4w ago
yes
leowest
leowestβ€’4w ago
this is what the other way looks like
leowest
leowestβ€’4w ago
without using multiple windows this is using UserControl and a single window the process is similar to what u do with window in terms of passing the car over, but it all exists in a single window and feels more consistent as if u were navigating from page to page
Ale
Aleβ€’4w ago
and you also put the instance of car here, right?
No description
leowest
leowestβ€’4w ago
no u dont change Form1 only Form2 and 3 Car already exists in Form1
leowest
leowestβ€’4w ago
No description
leowest
leowestβ€’4w ago
u can remove the readonly on Form1 leave on the others
Ale
Aleβ€’4w ago
i dont know what i do but don t show the name of car
No description
No description
No description
leowest
leowestβ€’4w ago
looks fine what I would change is
leowest
leowestβ€’4w ago
No description
leowest
leowestβ€’4w ago
dont do this u assign textBoxNameCar.Text to _car.NameCar before testing if textBoxNameCar.Text is safe to use
if (string.IsNullOrEmpty(textBoxNameCar.Text))
{
label2.Text = "Car name cannot be empty";
return;
}

if (textBoxNameCar.Text.Length < 3)
{
label2.Text = "Car name must contain more than 3 letters";
return;
}

_car.NameCar = textBoxNameCar.Text;
if (string.IsNullOrEmpty(textBoxNameCar.Text))
{
label2.Text = "Car name cannot be empty";
return;
}

if (textBoxNameCar.Text.Length < 3)
{
label2.Text = "Car name must contain more than 3 letters";
return;
}

_car.NameCar = textBoxNameCar.Text;
Ale
Aleβ€’4w ago
don t appear the name of car
leowest
leowestβ€’4w ago
can you show me the Car class
Ale
Aleβ€’4w ago
when i press the button show name
Ale
Aleβ€’4w ago
No description
leowest
leowestβ€’4w ago
yes I understand that, I am trying to help u by fixing the things I see that are wrong such as the ones I pointed above
MODiX
MODiXβ€’4w ago
leowest
can you show me the Car class
Quoted by
<@1102729783969861782> from #Acces data from another form (click here)
React with ❌ to remove this embed.
Ale
Aleβ€’4w ago
No description
leowest
leowestβ€’4w ago
ok looks fine... u forgot to
Ale
Aleβ€’4w ago
?
leowest
leowestβ€’4w ago
No description
leowest
leowestβ€’4w ago
VerNameCar is not being called so it never sets the car name when u hit confirm :catderp:
Ale
Aleβ€’4w ago
damn i forgot :Smadge:
leowest
leowestβ€’4w ago
:waaaaaaaaaahhhhhh: anyway I will be back in 20 minutes need to leave my seat
Ale
Aleβ€’4w ago
it worked, thank you very much for everything :catHeyHello:
leowest
leowestβ€’4w ago
np πŸ˜‰
MODiX
MODiXβ€’4w ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server
More Posts