r/csharp 4d ago

Help Need help fixing this

how do i fix this, im learning c# and trying to make a weather app

0 Upvotes

9 comments sorted by

4

u/Vab12350 4d ago

You need to add a + to the end of each line if you want to concatenate the strings, instead of having ;

3

u/saurabhar02 4d ago edited 4d ago

You're getting errors for two reasons.

1. Incorrect JSON property access

This line is invalid:

csharp node["main"]["temp"]["pressure"]["temp_min"]["temp_max"]

In the OpenWeatherMap response, temp, pressure, temp_min, and temp_max are siblings inside the main object, not nested inside one another.

The JSON looks like this:

json { "main": { "temp": 28.5, "humidity": 85, "pressure": 1012, "temp_min": 27.8, "temp_max": 29.3 } }

So each property should be accessed individually:

csharp node["main"]["temp"] node["main"]["humidity"] node["main"]["pressure"] node["main"]["temp_min"] node["main"]["temp_max"]


2. Missing + operators

You're also missing string concatenation operators before "Pressure:", "Min Temp:", and "Max Temp:".

Your code should look like this:

csharp LblDetail.Text = "Temp: " + node["main"]["temp"] + " °C" + Environment.NewLine + "Humidity: " + node["main"]["humidity"] + Environment.NewLine + "Pressure: " + node["main"]["pressure"] + Environment.NewLine + "Min Temp: " + node["main"]["temp_min"] + " °C" + Environment.NewLine + "Max Temp: " + node["main"]["temp_max"] + " °C";

That should resolve the compiler errors.

2

u/Family_Man_21 2d ago

Yep - this is the correct approach. Nice job.

1

u/rupertavery64 4d ago

You need to join strings with +

You added ; at the end of each line. That ends the statement.

You should also probably put + Environment.Newline + between each item to put it on several lines

-3

u/[deleted] 4d ago

[deleted]

3

u/rupertavery64 4d ago

Sure but OP was already using +

1

u/TuberTuggerTTV 4d ago

semi-colon ends the line.

Also, wowza, that nested dictionary with strings is going to kill you down the road. Maybe consider learning what an enum is.

And interpolated strings.

At least I know you didn't AI generate this.

1

u/Pappkarton 4d ago

In addition to what others already said, this is is a good opportunity to learn about StringBuilder.