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
Henkypenky3y ago
can you explain a little bit more what you want to do?
BigggMoustache
BigggMoustacheOP3y ago
I want to download from the url in card.ImageUri.Small
Henkypenky
Henkypenky3y ago
and where is that defined? what type is it?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
Okie doke I will try that instead. I was just going with things I found on the internet lol
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
ye
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
why can't I access the values in the properties?
Henkypenky
Henkypenky3y ago
card.ImageUris.Small what's the type of this? where is it defined, where does it come from?
BigggMoustache
BigggMoustacheOP3y ago
I just edited and it added errors
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
It's a string property in a model for Json. It's just a URL for the image
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
I thought card was the instance and imageuri was inside it
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
I get what it means lol I just don't understand how it happened
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky3y ago
try new Uri() and pass that
BigggMoustache
BigggMoustacheOP3y ago
I would rather understand my misunderstanding with the Card class first
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky3y ago
yep
BigggMoustache
BigggMoustacheOP3y ago
It's long do you just want the relevant chunk? It's all at the top anyways
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3y 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
BigggMoustacheOP3y ago
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
Henkypenky3y ago
oof
BigggMoustache
BigggMoustacheOP3y ago
oh for sure I just figure it's right at the top so ya know haha
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky3y ago
a class inside a class
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
👀
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
i thought it worked like a directory kek
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
part of the deserialize magic was my assumption, yes lol
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky3y ago
there's no way json2csharp gave you that
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
I'm just using what Json2CSharp gave me
Henkypenky
Henkypenky3y ago
can u share the json
BigggMoustache
BigggMoustacheOP3y ago
$code
MODiX
MODiX3y 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
BigggMoustacheOP3y ago
BlazeBin - sibkytwzugmv
A tool for sharing your source code with the world!
BigggMoustache
BigggMoustacheOP3y ago
Okie doke my next question is if Deserialize doesn't create an instance of ImageUris how does it populate the properties?
Henkypenky
Henkypenky3y ago
wait a moment first things first
BigggMoustache
BigggMoustacheOP3y ago
ya np
Henkypenky
Henkypenky3y ago
go put that json in json2chsarp again
BigggMoustache
BigggMoustacheOP3y ago
alright
Henkypenky
Henkypenky3y 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
BigggMoustacheOP3y ago
Yeah taht's what I got $code
MODiX
MODiX3y 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
Henkypenky3y ago
okay then why did you put a class inside a class?
BigggMoustache
BigggMoustacheOP3y ago
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
Henkypenky3y 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
BigggMoustacheOP3y ago
alright, I understand what you're telling me here
Henkypenky
Henkypenky3y ago
the idea is that classes are singular
BigggMoustache
BigggMoustacheOP3y ago
yeah I get this now lol let me take that out and show you original error then
Henkypenky
Henkypenky3y 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
BigggMoustacheOP3y ago
right right thank you so much for that let me see if it even still give the error
Henkypenky
Henkypenky3y ago
if it finds a ImageUris type it will go to that class and serialize/deserialize it
BigggMoustache
BigggMoustacheOP3y ago
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
Henkypenky3y ago
yeah that's like another problem unrelated to this one
BigggMoustache
BigggMoustacheOP3y ago
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
Henkypenky3y ago
actually we all helped, hate taking the credit xd
BigggMoustache
BigggMoustacheOP3y ago
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
Henkypenky3y 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 User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
Was my next step tobe tyvm
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
alright folks, i'll let you know if it works in 9 hours when I'm free again
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
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 User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
yeah ngl the naming of things confuses me a bunch lol
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
i never know which side of what it's referring to
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
like create
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
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 User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
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 User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
Oh for sure man. I hope you get I'm speaking generally without specific reference to any instruction
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
okay wait so use using?
Henkypenky
Henkypenky3y ago
might be better to open the filestream once since there will be multiple creations instead of 1 per creation
BigggMoustache
BigggMoustacheOP3y ago
yeah I was gonna ask that too
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Henkypenky
Henkypenky3y ago
multiple images it's a list
BigggMoustache
BigggMoustacheOP3y ago
yeah
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
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 User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
🤣 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 User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
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 User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
❤️
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
later buddy I'll close in 9 hours when I get to try it again
Want results from more Discord servers?
Add your server