- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand
- OverflowAI GenAI features for Teams
- OverflowAPI Train & fine-tune LLMs
- Labs The future of collective knowledge sharing
- About the company Visit the blog
Collectivesβ’ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Get early access and see previews of new features.
What is that the Error of Illegal assignment and how to correct it?
That's supposed to be a correct and well-known code to arrange integers from inferior to superior but compiler still insists saying "illegal assignment to for loop 'j' variable".
What's the problem?
- turbo-pascal
2 Answers 2
The problem is here:
You are not allowed to assign to the for loop variable.
Perhaps you meant to write
you forgot var before t in the header of the procedure
- If you clarify why var is needed before t in the procedure argument list, this could make it a good answer. – Nowhere Man Commented Sep 28, 2020 at 11:47
Your Answer
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Sign up or log in
Post as a guest.
Required, but never shown
By clicking βPost Your Answerβ, you agree to our terms of service and acknowledge you have read our privacy policy .
Not the answer you're looking for? Browse other questions tagged pascal freepascal turbo-pascal or ask your own question .
- The Overflow Blog
- Mobile Observability: monitoring performance through cracked screens, old...
- Featured on Meta
- Announcing a change to the data-dump process
- Bringing clarity to status tag usage on meta sites
- What does a new user need in a homepage experience on Stack Overflow?
- Feedback requested: How do you use tag hover descriptions for curating and do...
- Staging Ground Reviewer Motivation
Hot Network Questions
- How is an inverting opamp adder circuit able to regulate its feedback?
- Pros and cons of ESPRIT versus MUSIC
- Understanding a proof that a bounded sequence in a separable Hilbert space contains a weakly convergent subsequence
- A SF novel where one character makes a "light portrait" of another one, with huge consequences
- Do passengers transiting in YVR (Vancouver) from international to US go through Canadian immigration?
- A classic problem about matrix
- What does the equation of the law of conservation of total mechanical energy with deformation energy look like?
- A SF novel where a very poor girl finds a "talking book" meant for daughters of extremely rich people
- How make this table? And which package more helpful for these tables
- Why is the !? annotation so rare?
- What is this device in my ceiling making out of battery chirps?
- Seinfeldisms in O.R
- Convert HEIC image into JPEG in LWC
- What are the common traits of US backed regime change?
- Identifications in differential geometry
- Why does Jenny hug Cindy at the end of "Commando" (1985)?
- Does a readied action have to be an action?
- How to setup a home lab with a custom domain name?
- What did Horace say about combining Latin and Greek roots?
- How much easier/harder would it be to colonize space if humans found a method of giving ourselves bodies that could survive in almost anything?
- In 1982 Admiral Grace Hopper said "I still haven't found out why helicopter rotors go the way they do". If she were here today, how might one answer?
- Can an international student email a professor at a foreign university for an internship opportunity?
- Create random points using minimum distance calculated on the fly using ModelBuilder in ArcGIS Pro
- A strange Lipschitz function
Type Casting in Apex
Hello devs,
What are Upcasting and Downcasting? How can you cast Apex Map, List, and Set? What is a supertype, and what is the difference between SObject and Object? Answers to these questions and much more can be found in this post!
Let's begin.
Before We Start
I am using different emojis to grab your attention:
- π¨ - Unexpected casting behavior.
- β - Code that works.
- β - Code that does not work.
What supertype is?
A supertype is an abstract/virtual class or an interface that is used by a child class.
The Parent interface is a supertype for the Child class.
A class can implements many interfaces, meaning that a class can have many supertypes .
The Parent abstract class is a supertype/superclass for the Child class.
A class can extends only one abstract class, but it can still implements many interfaces.
The Parent virtual class is a supertype/superclass for the Child class.
A class can extends only one virtual class, but it can still implements many interfaces.
Upcasting vs Downcasting
You know what supertype is, it's necessary to understand the difference between Upcasting and Downcasting .
To better understand upcasting and downcasting, we need some examples.
We have two classes (it can be also class and interface), Parent and Child .
- Parent is a virtual class and has a virtual method.
- Child extends the Parent class and override the virtual method.
If you don't understand what virtual means, you can refer to the Abstract, Virtual, Interface in Apex post.
- Child extends the Parent class. It means that Parent is a supertype/superclass for Child . Because of this, the following syntax: Parent parent = new Child(); is valid.
- Casting a subclass ( Child ) to a supertype/superclass ( Parent ) is called Upcasting .
- Upcasting can be either explicit or implicit.
Implicit Upcasting
Upcasting can be done implicitly.
Explicit Upcasting
Code should be as simple as possible (KISS rule), so the better approach is to use implicit upcasting.
The parent variable has access to:
- Parent public variables.
- Parent public methods.
- Child ONLY overriden methods.
Downcasting
- Casting a supertype/superclass ( Parent ) to a subclass ( Child ) is called Downcasting .
- Upcasting can be done ONLY explicitly.
Implicit Downcasting
Downcasting CANNOT be implicit. Why?
- There can be many children of the Parent class. e.g public class Child2 extends Parent { ... } .
- Apex needs to know to what type the variable should be cast to.
Explicit Downcasting
You have to cast explicitly (Child) so the compiler checks in the background if this type of casting is possible or not. If not, you will get System.TypeException: Invalid conversion from runtime type Parent to Child .
The child variable has access to:
- Child ALL public methods and variables.
- SObject Class
SObject is a supertype for:
- all standard objects (Account, Contact, etc.)
- all custom objects (MyCustomObject__c)
Object is a supertype for:
- all standard objects
- all custom objects
- all apex classes
- all collections (List, Set and Map)
- all apex types (Integer, String, Boolean)
- Methods inherited from Object class
SObject and Object
Sobject and object - instanceof.
Expression | is instanceOf |
---|---|
TRUE β |
- SObject is an instance of Object . Object is a supertype for SObject .
SObject and Object - casting
Object to sobject.
- Object needs to be explicitly cast to SObject .
- Object is a supertype of SObject .
- Conversion from Object to SObject is called Downcasting .
- As you already know from Downcasting section. Downcasting needs to be explicit (SObject) .
SObject to Object
- SObject can be explicitly or implicitly cast to Object .
- Conversion from SObject to Object is called Upcasting .
- As you already know from Upcasting section. Upcasting can be done explicitly or implicitly.
Inherited methods
- Interestingly, Object is a supertype for SObject , but SObject does NOT inherit Object methods like toString(), equals(), hashCode(), clone() . SObject has all of the following methods SObject class methods. .
However, all Apex Classes inherit Object methods ( toString() , equals() , hashCode() , clone() ).
Standard/Custom Object
Standard/custom object - instanceof.
Expression | is instanceOf |
---|---|
TRUE β | |
TRUE β | |
TRUE β |
- Standard/Custom Object is an instance of SObject and Object .
- SObject and Object are supertypes for Standard/Custom Objects .
Standard/Custom Object to SObject - casting
Standard/custom object to sobject.
Why it works?
- SObject is a supertype for Account , Contact , and other standard or custom objects.
- Conversion from Standard/Custom Object to SObject is called Upcasting .
SObject to Standard/Custom Object
- Conversion SObject to Standard/Custom Object is called Downcasting .
- As you already know from Downcasting section. Downcasting can be done ONLY explicitly.
Standard/Custom Object to Object - casting
Standard/custom object to object.
- Object is a supertype for Account , Contact , and other standard or custom objects.
- Conversion from Standard/Custom Object to Object is called Upcasting .
Object to Standard/Custom Object
- Conversion Object to Standard/Custom Object is called Downcasting .
Primitive Types
Primitive types - instanceof.
Expression | is instanceOf |
---|---|
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β |
- All Primitive Data Types are instanceOf Object class.
- Object is a supertype for all primitve types.
Primitive Types - casting
- Primitive Types can be implicilty cast to Object .
- Object is a supertype of all Primitive Types.
- Conversion from Primitive Type to Object is called Upcasting .
List<SObject>
List<sobject> - instanceof.
Expression | is instanceOf |
---|---|
TRUE β | |
TRUE β | |
FALSE β |
- List<SObject> is an instance of concrete Standard/Custom Object!
- List<SObject> is NOT an instance of List<Object> .
List<SObject> - casting
List<sobject> to list<object>.
- List<SObject> is NOT an instance of List<Object> (based on instanceOf ), but you can still assign List<SObject> to List<Object> . Do not trust instanceOf !
List<Standard/Custom> to List<SObject>
- List<SObject> is a supertype for List<Standard/Custom> .
- Conversion from Standard/Custom Object to List<SObject> is called Upcasting .
List<SObject> to List<Standard/Custom>
Upcasting/Downcasting (?)
- List<Standard/CustomObject> is an instance of List<SObject> , and List<SObject> is an instance of List<Standard/CustomObject> .
- List<Standard/CustomObject> is a supertype for List<SObject> , and List<SObject> is a supertype for List<Standard/CustomObject> .
- You can do implicit casting.
List<Object>
List<object> - instanceof.
Expression | is instanceOf |
---|---|
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
FALSE β |
- SObject is always an instance of Object , hovever List<SObject> is NOT an instance of List<Object> .
List<Object> - casting
- We can cast List<ConcreteType> to List<Object> ,
- List<Object> is a supertype of List<ConcreteType> .
- Conversion from List<ConcreteType> to List<Object> is called Upcasting .
- There is no need to cast explicitly by adding (List<Object>) .
- List<SObject> is NOT an instance of List<Object> (based on instanceOf ), but you can still assign List<SObject> to List<Object> .
List Summary
List casting in Apex is a bit unusual.
What is common:
- List<ConcreteType> is an instance of List<Object> .
- List<Object> is a supertype for List<ConcreteType> .
What is unexpected:
- Based on instanceOf . List<SObject> is not an instance of List<Object> , but you can still assign List<SObject> to List<Object> .
- You can even assign List<SObject> to List<Object> . Do not trust instanceOf !
- Even more unexpectedly, List<SObject> is an instance of concrete List<Standard/Custom> Object!
Set<SObject>
Set<sobject> - instanceof.
Expression | is instanceOf |
---|---|
FALSE β | |
FALSE β | |
FALSE β |
- Set<Standard/CustomObject> is NOT an instance of Set<SObject> .
Set<SObject> - casting
Set<sobject> to set<object>.
- You CANNOT cast Set<SObject> to Set<Object>
Set<Standard/Custom> to Set<SObject>
- You CANNOT cast Set<Standard/Custom> to Set<SObject>
Set<SObject> to Set<Standard/Custom>
- You CANNOT cast Set<SObject> to Set<Standard/Custom>
Set<Object>
Set<object> - instanceof.
Expression | is instanceOf |
---|---|
FALSE β | |
FALSE β | |
FALSE β | |
FALSE β | |
FALSE β | |
FALSE β | |
FALSE β | |
FALSE β | |
FALSE β | |
FALSE β | |
FALSE β |
- Other than List , Set<Object> is NOT a supertype for Set<ConcreteType> .
Set<Object> - casting
- We CANNOT cast Set<ConcreteType> to Set<Object> ,
Even if you explicitly add casting (Set<Object>) it will not work.
Set Summary
Map<sobject>, map<sobject> - instanceof.
I skipped cases where SObject is a key. SObject should never be a key in the Map.
Key Type | Value Type | Key Type | Value Type | Is instanceOf |
---|---|---|---|---|
Object | Account | Object | Object | TRUE β |
Object | Account | Object | SObject | TRUE β |
- Map<Object, Account> is an instance of Map<Object, Object> , which means that Map<Object, Object> is a supertype of Map<Object, Account> .
- Map<Object, Account> is an instance of Map<Object, SObject> , which means that Map<Object, SObject> is a supertype of Map<Object, Account> .
Map<SObject> - casting
- Map<Object, Object> is a supertype for Map<Object, Standard/CustomObject> .
- Conversion from Map<Object, Standard/CustomObject> to Map<Object, Object> is called Upcasting .
Even explicit casting will not work. The error is different.
How to fix Map downasting?
Map<Object>
Map<object> - instanceof.
Key Type | Value Type | Key Type | Value Type | Is Instance Of |
---|---|---|---|---|
String | String | Object | Object | FALSE β |
String | String | String | Object | TRUE β |
String | String | Object | String | FALSE β |
String | Object | Object | Object | FALSE β |
String | Object | Object | String | FALSE β |
String | Object | String | String | FALSE β |
Object | String | Object | Object | TRUE β |
Object | String | String | Object | FALSE β |
Object | String | String | String | FALSE β |
Maps are the same instance only in the following cases:
- new Map<MyType, MyType>() instanceOf new Map<MyType, Object>
- new Map<Object, MyType>() instanceOf new Map<Object, Object>
Key Type must be the same.
Map<Object> - casting
You can cast implicitly only when:
Map Summary
Starting from Summer 23', Set implements Iterable interface as List does.
Iterable List
Iterable list - instanceof.
Not surprisingly, a List of concrete types is also an instance of Iterable<Object> .
Expression | is instanceOf |
---|---|
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β | |
TRUE β |
- Instance of List<Object> and Set<Object> are always an instance of System.Iterable<Object> .
Iterable List - casting
It's really interesting.
e.g List<String> is an instance of Iterable<Object> , but you CANNOT use Iterable<Object> as a supertype .
You need to assign List<String> to List<Object> and after it to Iterable<Object> .
Iterable Set
Iterable set - instanceof.
- An interesting thing is that you're getting an error Operation instanceOf is always true... .
- Set<ConcreteType> is also an instance of Iterable<Object> , but not Set<SObject> .
Iterable Set - casting
- You CAN use Iterable<Object> as a supertype for Set , which you CANNOT do with a List .
- You CANNOT assign Set<SObject> to Iterable<Object> .
It's quite a big deal. Let's check an example:
How to fix it?
- You need two methods ( isIn(Iterable<Object> inList) and isIn(List<Object> inList) ) in the interface that will be covered by one method ( isIn(Iterable<Object> inList) in concrete class!
Iterable Summary
- You CANNOT use Iterable<Object> as a supertype for List<ConcreteType> . You need to assign List<ConcreteType> to List<Object> and after it to Iterable<Object> .
- Set<ConcreteType> is instance of Iterable<Object> .
- Set<Sobject> is NOT instance of Set<SObject> .
Casting Rules
Cast to everything.
The problem with the solution below is performance.
Casting Cheat Sheet
Do not trust instanceof.
Based on the type system is broken with regards to collections :
Pretty much the entire "Type" system that governs Maps, Sets, and Lists is broken. Do not trust instanceOf, use your own logical assessment to determine if something is safe or not.
As shown in the post, there are cases where instanceOf does not work correctly.
List<SObject> and List<Object>
instanceOf says that " List<SObject> is never an instance of List<Object> ", but you can assign List<SObject> to List<Object> .
List<ConcreteType> and Iterable<Object>
instanceOf says that List<ConcreteType> is an instance of Iterable<Object> , but you CANOT assign List<ConcreteType> to Iterable<Object> .
SObject Casting
- Casting from Standard/Custom Object to SObject is called upcasting and can be implicitly.
- Casting from SObject to Standard/Custom Object is called downcasting and it needs to be explicit.
- SObject is instance of Object . Object is a supertype for SObject .
- Casting from SObject to Object is called upcasting and can be implicit.
- Casting from Object to SObject is called downcasting and it needs to be explicit.
Object Casting
Primitive types casting.
- All Primitive Data Types are instanceOf Object . Object is a supertype for all primitve types.
- Casting from Primitive Data Types to Object is called upcasting and can be implicit.
List Casting
- All List<ConcreteType> are instance of List<Object> . List<Object> is a supertype of List<ConcreteType> .
- Based on instanceOf method List<SObject> is NOT an instance of List<Object> , but still you can assign List<SObject> to List<Object> . Do not trust instanceOf .
- Casting from List<ConcreteType> to List<Object> is called upcasting and can be implicit.
- List<Standard/Custom> is a supertype for List<SObject> .
Set Casting
- You CANNOT cast Set<ConcreteType> to Set<Object> .
Map Casting
- Map<Object, Account> is instance of Map<Object, Object> , it means that Map<Object, Object> is a supertype for Map<Object, Account> .
- Map<Object, Account> is instance of Map<Object, SObject> , it means that Map<Object, SObject> is a supertype for Map<Object, Account> .
- To fix System.TypeException: Invalid conversion from runtime type Map<ANY,SObject> to Map<ANY,Account> dynamic Map initiation is needed.
Iterable Casting
- Set<ConcreteType> is instance of Iterable<Object> , but Set<SObject> is NOT instance of Iterable<SObject> .
- Unexpected Iterable behavior in Apex
- Classes and Casting
- Using the instanceOf Keyword
- Upcasting Vs Downcasting in Java
You might also like
Simple Salesforce CICD for Developers
Set up a Salesforce CI/CD pipeline easily! Automate deployments, run tests, and more with our step-by-step guide. Happy coding!
Salesforce OAuth 2.0 Flows: Integrate in the right way
TL;DR I just want to know which flow to use If you’re short on time or just want to know what flow is for you, just go here. Introduction Brief Overview OAuth, which stands for Open Authorization, is an open-standard protocol that provides users with secure access to resources on an application, such as Salesforce, […]
Stack Exchange Network
Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Illegal assignment from Account to String
I wrote a trigger which attempts to grab the LastModifiedById and populate it to another field. When I use trigger.NewMap to get the LastModifiedById , it throws this error:
Error: Compile Error: Illegal assignment from Account to String
This is the line throwing the error:
Here's the complete trigger:
Why am I getting this error? How can I fix it?
- compile-error
- 3 What's the point of this? You don't need a trigger. Don't code if you don't need it. Just use a formula. – Martin Lezer Commented Feb 13, 2017 at 10:49
The description of error is quite obvious.
You must log in to answer this question.
Not the answer you're looking for browse other questions tagged apex trigger compile-error ..
- The Overflow Blog
- Mobile Observability: monitoring performance through cracked screens, old...
- Featured on Meta
- Announcing a change to the data-dump process
- Bringing clarity to status tag usage on meta sites
Hot Network Questions
- Does a readied action have to be an action?
- Driveway electric run using existing service poles
- What did Horace say about combining Latin and Greek roots?
- Functor composition rule necessary?
- Is consciousness a prerequisite for knowledge?
- decode the pipe
- How much easier/harder would it be to colonize space if humans found a method of giving ourselves bodies that could survive in almost anything?
- Journal keeps messing with my proof
- Convert HEIC image into JPEG in LWC
- Self-descriptive
- Word for a collection of awards, such as an Olympic athlete’s earned medals
- Are there probabilistic facts of the matter about the universe?
- We are travelling to Phu Quoc from Perth Australia and have a 9 hour stop over in kuala lumpur do we need a visa to go out the airport?
- What story starts off with the character waking up in a battlefield with wolves and vultures and snow?
- Best way to explain the thinking steps from x² = 9 to x=±3
- Expensive constructors. Should they exist? Should they be replaced?
- Word to describe telling yourself that you are not, and will never be, good enough
- Pros and cons of ESPRIT versus MUSIC
- Why is the !? annotation so rare?
- What should be the affiliation of PhD student who submitted thesis but yet to defend, in a conference talk slides?
- Is it a date format of YYMMDD, MMDDYY, and/or DDMMYY?
- Create random points using minimum distance calculated on the fly using ModelBuilder in ArcGIS Pro
- When a star becomes a black hole do the neutrons that are squeezed together release binding energy and if so does this energy escape from the hole?
- Seinfeldisms in O.R
Navigation Menu
Search code, repositories, users, issues, pull requests..., provide feedback.
We read every piece of feedback, and take your input very seriously.
Saved searches
Use saved searches to filter your results more quickly.
To see all available qualifiers, see our documentation .
- Notifications You must be signed in to change notification settings
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type 'void' is not assignable to type 'boolean'. #951
ivanmdh commented Oct 22, 2021 • edited by claudiodekker Loading
version: 0.10.1 version: 0.7.1 When you use the invalid event trigger the compilation fails On app.tsx { Inertia } from '@inertiajs/inertia' Inertia.on('invalid', (event) => { console.log(`An invalid Inertia response was received.`) console.log(event.detail.response) })Error: Possible solution Change to on result of invalid and exception elements types.d.ts parameters: [Errors]; details: { errors: Errors; }; result: void; }; invalid: { parameters: [AxiosResponse]; details: { response: AxiosResponse; }; result: boolean; }; exception: { parameters: [Error]; details: { exception: Error; }; result: boolean; }; |
The text was updated successfully, but these errors were encountered: |
Successfully merging a pull request may close this issue.
IMAGES
VIDEO
COMMENTS
as I writing a test class, I come across this error as I am attempting to save my file. Error: Compile Error: Illegal assignment from String to Boolean at line 15 column 13. Are there any adjustments, I need to make to my code. Many Thanks. @isTest.
I am trying to save my test class for a Rest a webservice (GET) but get the error Illegal assignment from void to String on line 16. If I change the void to string I get a missing return statement string error.
Illegal assignment from void to Id. I assume this about this line: Id testSummaryId = testSummary.Id; but this comes right after an "insert". I thought the "insert" set the Id for the object?
That's supposed to be a correct and well-known code to arrange integers from inferior to superior but compiler still insists saying "illegal assignment to for loop 'j' variable". What's the problem?
Salesforce: Error: Compile Error: Illegal assignment from String to BooleanHelpful? Please support me on Patreon: https://www.patreon.com/roelvandepaarWith ...
Set<Blob> mySetBlobs = new Set<Blob>{ Blob.valueof('StringToBlob') }; Set<Object> myBlobs = mySetBlobs; // Illegal assignment from Set<Blob> to Set<Object> Set<Boolean> mySetBooleans = new Set<Boolean>{ true }; Set<Object> myBooleans = mySetBooleans; // Illegal assignment from Set<Boolean> to Set<Object> Set<Date> mySetDates = new Set<Date ...
Boolean Methods. The following are methods for Boolean. All methods are static. valueOf (stringToBoolean) Converts the specified string to a Boolean value and returns true if the specified string value is true. Otherwise, returns false. valueOf (fieldValue) Converts the specified object to a Boolean value. Use this method to convert a history ...
Salesforce: Illegal assignment from void to StringHelpful? Please support me on Patreon: https://www.patreon.com/roelvandepaarWith thanks & praise to God, a...
Error: Compile Error: Illegal assignment from Account to String. This is the line throwing the error: String userIdString = trigger.newmap.get(acc.LastModifiedById); Here's the complete trigger:
Type 'void' is not assignable to type 'boolean'. Possible solution. Change to boolean | void on result of invalid and exception elements. types.d.ts. error: { . parameters: [Errors]; . details: { . errors: Errors; }; . result: void; }; . invalid: { . parameters: [AxiosResponse]; . details: { . response: AxiosResponse; }; .