Download from URI in model error

Other problem was solved, now I've this one xD. Trying to do the following:
public static async Task DownloadFunction(List<Card> cards)
{
foreach (var card in cards)
{
byte[] fileBytes = await client.GetByteArrayAsync(card.ImageUris.Small);
File.WriteAllBytes($@"C:\Users\samue\Desktop\MTGImages\{card.Root.name}", fileBytes);
}
}
public static async Task DownloadFunction(List<Card> cards)
{
foreach (var card in cards)
{
byte[] fileBytes = await client.GetByteArrayAsync(card.ImageUris.Small);
File.WriteAllBytes($@"C:\Users\samue\Desktop\MTGImages\{card.Root.name}", fileBytes);
}
}
and getting errors
Error CS0120 An object reference is required for the non-static field, method, or property 'Card.ImageUris.Small'
Error CS0120 An object reference is required for the non-static field, method, or property 'Card.ImageUris.Small'
Error CS0572 'ImageUris': cannot reference a type through an expression; try 'Card.ImageUris' instead
Error CS0572 'ImageUris': cannot reference a type through an expression; try 'Card.ImageUris' instead
102 Replies
Henkypenky
Henkypenky2y ago
can you explain a little bit more what you want to do?
BigggMoustache
I want to download from the url in card.ImageUri.Small
Henkypenky
Henkypenky2y ago
and where is that defined? what type is it?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
Okie doke I will try that instead. I was just going with things I found on the internet lol
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
ye
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
why can't I access the values in the properties?
Henkypenky
Henkypenky2y ago
card.ImageUris.Small what's the type of this? where is it defined, where does it come from?
BigggMoustache
I just edited and it added errors
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
It's a string property in a model for Json. It's just a URL for the image
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
I thought card was the instance and imageuri was inside it
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
I get what it means lol I just don't understand how it happened
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky2y ago
try new Uri() and pass that
BigggMoustache
I would rather understand my misunderstanding with the Card class first
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky2y ago
yep
BigggMoustache
It's long do you just want the relevant chunk? It's all at the top anyways
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
BigggMoustache
using System.Text.Json.Serialization;

namespace MTGCardDownloader
{
public class Card
{
public class ImageUris
{
[JsonPropertyName("small")]
public string Small { get; set; }
using System.Text.Json.Serialization;

namespace MTGCardDownloader
{
public class Card
{
public class ImageUris
{
[JsonPropertyName("small")]
public string Small { get; set; }
Henkypenky
Henkypenky2y ago
oof
BigggMoustache
oh for sure I just figure it's right at the top so ya know haha
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky2y ago
a class inside a class
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
👀
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
i thought it worked like a directory kek
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
part of the deserialize magic was my assumption, yes lol
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky2y ago
there's no way json2csharp gave you that
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
I'm just using what Json2CSharp gave me
Henkypenky
Henkypenky2y ago
can u share the json
BigggMoustache
$code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
BigggMoustache
BlazeBin - sibkytwzugmv
A tool for sharing your source code with the world!
BigggMoustache
Okie doke my next question is if Deserialize doesn't create an instance of ImageUris how does it populate the properties?
Henkypenky
Henkypenky2y ago
wait a moment first things first
BigggMoustache
ya np
Henkypenky
Henkypenky2y ago
go put that json in json2chsarp again
BigggMoustache
alright
Henkypenky
Henkypenky2y ago
you should get something like this
// Root myDeserializedClass = JsonSerializer.Deserialize<List<Root>>(myJsonResponse);
public class ImageUris
{

}

public class Legalities
{

}

public class Prices
{

}

public class RelatedUris
{

}

public class Root
{

}
// Root myDeserializedClass = JsonSerializer.Deserialize<List<Root>>(myJsonResponse);
public class ImageUris
{

}

public class Legalities
{

}

public class Prices
{

}

public class RelatedUris
{

}

public class Root
{

}
BigggMoustache
Yeah taht's what I got $code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Henkypenky
Henkypenky2y ago
okay then why did you put a class inside a class?
BigggMoustache
OH YOU'RE RIGHT I DID DO THAT fudge i couldn't get it to work otherwise i forget what error it gave, I can show you if you'd like
Henkypenky
Henkypenky2y ago
Root already knows there is a property of type ImageUris
[JsonPropertyName("image_uris")]
public ImageUris ImageUris { get; set; }
[JsonPropertyName("image_uris")]
public ImageUris ImageUris { get; set; }
so it will go the Imageuris class to get the other properties per object since you are deserializing to a list every object (root) has one property named ImageUris which is this:
public class ImageUris
{
[JsonPropertyName("small")]
public string Small { get; set; }

[JsonPropertyName("normal")]
public string Normal { get; set; }

[JsonPropertyName("large")]
public string Large { get; set; }

[JsonPropertyName("png")]
public string Png { get; set; }

[JsonPropertyName("art_crop")]
public string ArtCrop { get; set; }

[JsonPropertyName("border_crop")]
public string BorderCrop { get; set; }
}
public class ImageUris
{
[JsonPropertyName("small")]
public string Small { get; set; }

[JsonPropertyName("normal")]
public string Normal { get; set; }

[JsonPropertyName("large")]
public string Large { get; set; }

[JsonPropertyName("png")]
public string Png { get; set; }

[JsonPropertyName("art_crop")]
public string ArtCrop { get; set; }

[JsonPropertyName("border_crop")]
public string BorderCrop { get; set; }
}
BigggMoustache
alright, I understand what you're telling me here
Henkypenky
Henkypenky2y ago
the idea is that classes are singular
BigggMoustache
yeah I get this now lol let me take that out and show you original error then
Henkypenky
Henkypenky2y ago
so you can rename Root to let's say Card then put that class in one file RelatedUris in another and so on all inside a folder and they will all be related to each other when you deserialize or serialize you go for the top one which is Card which will start travelling down
BigggMoustache
right right thank you so much for that let me see if it even still give the error
Henkypenky
Henkypenky2y ago
if it finds a ImageUris type it will go to that class and serialize/deserialize it
BigggMoustache
Huh the error is no longer there I understand now though the way it's not nested but relational, and you start with the primary class in the relation thank you very much I'm going to look back up at what ToBeClone said before the reason I made the download choice I did was googling "download file with httpclient" lol
Henkypenky
Henkypenky2y ago
yeah that's like another problem unrelated to this one
BigggMoustache
just to make it visible here Well you solved the riddle of my model problem lol so it's no longer a problem xD it now deserializes fine
Henkypenky
Henkypenky2y ago
actually we all helped, hate taking the credit xd
BigggMoustache
and the download code does not give error anymore either, lets see if it works! well i fixed the naming folder empty xD I've never tried this before so ya know maybe it's the problem File.WriteAllBytes($@"C:\Users\samue\Desktop\MTGImages\{card.Name}", fileBytes); interpolate literal string?
Henkypenky
Henkypenky2y ago
i would go with the suggestion TeBeClone said just get the stream open a file stream and copy the stream there basically you get the stream, you enable I/O operations and then copy it
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
Was my next step tobe tyvm
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
alright folks, i'll let you know if it works in 9 hours when I'm free again
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
and @Henkypenky tyvm for explaining the json bit, I had no idea what Iwas doing and now I have a small idea.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
yeah ngl the naming of things confuses me a bunch lol
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
i never know which side of what it's referring to
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
like create
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
is it creating the thing that holds the stream, is it creating the file from the stream, is it creating the stream itself in its container, etc
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
for sure I'm sure if I read it it'd be clearer, I'm just saying on its face it's a bit confusing lots of learning programming has come across that way to me lol
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
Oh for sure man. I hope you get I'm speaking generally without specific reference to any instruction
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
okay wait so use using?
Henkypenky
Henkypenky2y ago
might be better to open the filestream once since there will be multiple creations instead of 1 per creation
BigggMoustache
yeah I was gonna ask that too
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky2y ago
multiple images it's a list
BigggMoustache
yeah
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
nope lol $@"C:\Users\samue\Desktop\MTGImages\{card.Name}.jpg" idk if that works either lol i was gonna google it eventually
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
🤣 Alright I have irl obligations to fulfill. I greatly appreciate the help as always and hope to talk with you again later tonight.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
homie I appreciate it lol. Drop whatever you think I should look at in here, I promise I will. ya that makes sense
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
❤️
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
later buddy I'll close in 9 hours when I get to try it again