Rapport syntax c# Only a assignment, call, increment, decrement, expectation and new object expressions can be used as statement

StackOverflow https://stackoverflow.com/questions/23677636

  •  23-07-2023
  •  | 
  •  

Domanda

I am a beginner in C# and have an error in my code

I had a variable

Session.GetBobbaz().PetData = "0 5 #fff";

which work fine. But I want to make the second number and the color randomly So I've done this:

 var random = new Random();
   var color = String.Format("#{0:X6}", random.Next(0x1000000)); 
   Random randNum = new Random();
   var race = randNum.Next(25);
   var typeanimal = 1; 
   Session.GetBobbaz().PetData = typeanimal, race, color; 

PetData = typeanimal, race, color; Those three are underlined and make the error.

È stato utile?

Soluzione

Property PetData is of type string. You should assign string to it. Simplest way to build string in some specific format is String.Format method:

Session.GetBobbaz().PetData = 
    String.Format("{0} {1} {2}", typeanimal, race, color);

It gets string representation of each parameter you pass and puts them in format placeholders (by index).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top