-1

I declare a variable of type Car named ShowOne, create an instance of class Car in the body of the loop, and after the body of the loop I try to assign a reference to the class created in the loop, tell me the correct link passing practice?

static void Main(string[] args)

    {
        int height = 0;
        int peoplePlane = 0;
        int peopleShip = 0;
        string port = null;

        string Plane = "Plane";
        string Car = "Avto";
        string Ship = "Ship";

        Console.WriteLine("Specify vehicle parameters:");
        Console.WriteLine(new string('-', 10));

        Welcome infoShowWelcome = new Welcome();
        Vehicle TransportShow = new Vehicle();
        Car ShowOne;
        Plane ShowTwo;
        Ship ShowThree; 


        for (int i = 0; i <= 2; i++)
        {
            string nameTransport;
            if (i == 0)
            {
                nameTransport = Car;
                infoShowWelcome.ShowInfo(nameTransport);
                Car TransportOne = new Car(infoShowWelcome);
                ShowOne = TransportOne;
            }
            else if (i == 1)
            {
                nameTransport = Plane;
                infoShowWelcome.ShowInfo(nameTransport);
                Console.WriteLine("height" + " " + nameTransport + ":");
                height = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("people" + " " + nameTransport + ":");
                peoplePlane = Convert.ToInt32(Console.ReadLine());
                Plane TransportTwo = new Plane(infoShowWelcome, height, peoplePlane);
                ShowTwo = TransportTwo;
            }
            else if (i == 2)
            {
                nameTransport = Ship;
                infoShowWelcome.ShowInfo(nameTransport);
                Console.WriteLine("port" + " " + nameTransport + ":");
                port = Console.ReadLine();
                Console.WriteLine("people" + " " + nameTransport + ":");
                peopleShip = Convert.ToInt32(Console.ReadLine());
                Ship TransportThree = new Ship(infoShowWelcome, port, peopleShip);
                ShowThree = TransportThree;
            }
            else
            {
                break;
            }
            ShowOne.ShowInfo();
            ShowTwo.ShowInfo();
            ShowThree.ShowInfo();
        }

        Console.ReadKey();
    }
}

VS emphasizes textual ShowOne: the use of a local variable of which is not assigned a value.

philomelka
  • 41
  • 6
  • 1
    this isn't C not even C++. C# ? – Jean-François Fabre Jan 31 '19 at 20:02
  • `nameTransport = Car` looks suspicious. And it's definitely not C (keyword `new`) – Stephan Lechner Jan 31 '19 at 20:02
  • @Jean-FrançoisFabre When creating a theme, I chose c # but for some reason the tag was saved c, I think this bug is related to google translate in google chrome. – philomelka Jan 31 '19 at 20:06
  • 1
    Looks like `Car` is the name of a class. You are trying to set `nameTransport` equal to it. I don't think you're allowed to do that – Cubemaster Jan 31 '19 at 20:06
  • 1
    1. you cant assign CLASS (CAR) to string reference (nameTransport). 2. you are missing a closing brace `}`. 3. what is `infoShowWelcome`? 4. what are you trying to achieve? ... Generally, please be more specific with your problem, and youll get great answers :) – Tomer W Jan 31 '19 at 20:07
  • @Cubemaster Unfortunately, this part of my code incorrectly recognized syntax highlighting, in the string `nameTransport = Car;` Car is not a class, in my code it is a variable above, and there is also a class with the given name, it was seen in the code that I attached here – philomelka Jan 31 '19 at 20:13
  • 1
    In that case, you should rename your `Car` variable. Even if that isn't an issue in this case, it is still bad form to name your variables the same as your class. Even something so simple as `myCar` will do. – Cubemaster Jan 31 '19 at 20:17
  • `ShowOne = TransportOne;` This much is fine. This will make ShowOne have a reference to the new Car that was created in the i==0 iteration of the loop. I think that's all you are asking right? Do you have a bug or something? If you do, then probably you need to show more of the actual code. Did you use your debugger to see what gets assigned? – Wyck Jan 31 '19 at 20:22
  • 1
    @Wyck I posted the full Main () – philomelka Jan 31 '19 at 20:33
  • Possible duplicate of [How do I pass a variable by reference?](https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference) – Spinkoo Jan 31 '19 at 22:46

1 Answers1

0

There are many things about your code that are not really "best practice". But if you're specifically asking about how to assign the reference to the variables, then you've done it correctly. You could perhaps shorten it to eliminate the temporary variable.

Although your code is correct:

Car TransportOne = new Car(infoShowWelcome);
ShowOne = TransportOne;

it could be shortened to become:

ShowOne = new Car(infoShowWelcome);

...assuming you're not going to use the temporary TransportOne variable elsewhere later.

It is unusual to see your code written in a way that has a loop, but does not make use of the counter. Really, the only piece of code you have reused is the bit that does this after each iteration:

ShowOne.ShowInfo();
ShowTwo.ShowInfo();
ShowThree.ShowInfo();

But you'll find that 1) you have not initialized your variables and 2) they are null the first time you go to display them. If you explicitly initialize them to null where you declare them, you'll see why.

I might have been inclined to put that into a function and then "unroll" the loop to eliminate it entirely except for the fact that it will crash because ShowTwo is undefined after the first iteration. You likely just want to show them all once at the end anyway. So again, that's another point in favour of unrolling the loop. Like this:

static void Main(string[] args)
{
    int height = 0;
    int peoplePlane = 0;
    int peopleShip = 0;
    string port = null;

    string Plane = "Plane";
    string Car = "Avto";
    string Ship = "Ship";

    Console.WriteLine("Specify vehicle parameters:");
    Console.WriteLine(new string('-', 10));

    Welcome infoShowWelcome = new Welcome();
    Vehicle TransportShow = new Vehicle();
    Car ShowOne = null;
    Plane ShowTwo = null;
    Ship ShowThree = null;

    string nameTransport;

    nameTransport = Car;
    infoShowWelcome.ShowInfo(nameTransport);
    Car TransportOne = new Car(infoShowWelcome);
    ShowOne = TransportOne;

    nameTransport = Plane;
    infoShowWelcome.ShowInfo(nameTransport);
    Console.WriteLine("height" + " " + nameTransport + ":");
    height = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("people" + " " + nameTransport + ":");
    peoplePlane = Convert.ToInt32(Console.ReadLine());
    Plane TransportTwo = new Plane(infoShowWelcome, height, peoplePlane);
    ShowTwo = TransportTwo;

    nameTransport = Ship;
    infoShowWelcome.ShowInfo(nameTransport);
    Console.WriteLine("port" + " " + nameTransport + ":");
    port = Console.ReadLine();
    Console.WriteLine("people" + " " + nameTransport + ":");
    peopleShip = Convert.ToInt32(Console.ReadLine());
    Ship TransportThree = new Ship(infoShowWelcome, port, peopleShip);
    ShowThree = TransportThree;

    ShowOne.ShowInfo();
    ShowTwo.ShowInfo();
    ShowThree.ShowInfo();

    Console.ReadKey();
}

And although it's technically legal. It's really bad practice to have strings and classes with the same unqualified names like that. Definitely change that.

And respectfully, there are more bad practices abound in your code, but I have to stop here and just focus on the part that you asked about, which was the references to the objects.


And in case anyone else needs it, here are empty implementations (required to make this a complete example).

internal class Ship
{
    private Welcome infoShowWelcome;
    private string port;
    private int peopleShip;

    public Ship(Welcome infoShowWelcome, string port, int peopleShip)
    {
        this.infoShowWelcome = infoShowWelcome;
        this.port = port;
        this.peopleShip = peopleShip;
    }

    internal void ShowInfo()
    {
        Console.WriteLine(this);
    }
}

internal class Plane
{
    private Welcome infoShowWelcome;
    private int height;
    private int peoplePlane;

    public Plane(Welcome infoShowWelcome, int height, int peoplePlane)
    {
        this.infoShowWelcome = infoShowWelcome;
        this.height = height;
        this.peoplePlane = peoplePlane;
    }

    internal void ShowInfo()
    {
        Console.WriteLine(this);
    }
}

internal class Car
{
    private Welcome infoShowWelcome;

    public Car(Welcome infoShowWelcome)
    {
        this.infoShowWelcome = infoShowWelcome;
    }

    internal void ShowInfo()
    {
        Console.WriteLine(this);
    }
}

internal class Vehicle
{
    public Vehicle()
    {
    }
}

internal class Welcome
{
    public Welcome()
    {
    }

    internal void ShowInfo(string nameTransport)
    {
        Console.WriteLine(this);

    }
}
Wyck
  • 10,311
  • 6
  • 39
  • 60
  • Thank you very much, it was all about uninitialized variables like Car, Ship, Plane. After initializing them null, everything worked, thanks for the review about bad practice, I wrote this code very quickly, but I listened to your recommendations! Tell me why the reference to the object was not assigned to variables without the initial Declaration of their null – philomelka Jan 31 '19 at 21:30
  • When you say "_Tell me why the reference to the object was not assigned to variables without the initial Declaration of their null_" I have to say the references _were_ being assigned to the variables correctly. You can remove the `= null` from my example and it will still work. I added them to illustrate that you were probably getting a warning about your `ShowTwo.ShowInfo();` referencing an uninitialized variable on the first pass of your loop. `ShowTwo.ShowInfo()` would have thrown an null reference exception on the first pass of the loop because nothing was assigned to it yet. – Wyck Feb 01 '19 at 07:15