arguments d'une fonction : à déclarer initialement?
bonjour,
J'ai écrit un petit script en C# qui montre bien qu'il n'est pas nécessaire de déclarer initialement la variable passée en argument d'une fonction prototype:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class transp : MonoBehaviour
{
public Material mat;
// Material mat1,mat2;
float alphaValue;
public Mesh mesh;
public GameObject obj;
// Start is called before the first frame update
void Start()
{
obj.AddComponent<MeshRenderer>().material=mat;
obj.AddComponent<MeshFilter>().mesh = mesh;
var cubeRenderer1 = obj.GetComponent<Renderer>();
cubeRenderer1.material.SetColor("_Color", Color.red);
obj.AddComponent<MeshRenderer>().material=ChangeAlpha(mat,0);//appel de la fonction avec la variable non muette mat nécessitant d'être déclarée initialement
}
// Update is called once per frame
void Update()
{
}
Material ChangeAlpha(Material mat1, float alphaValue)
{
Color oldColor = mat.color;
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, alphaValue);
mat.SetColor("_Color", newColor);
return mat2;
}
} |
Les paramètres de la fonction ChangeAlphe sont muets donc je peux omettre de déclarer initialement dans la classe la variable mat1 comme ceci:
Par contre,lecompilateur me retourne une erreur si je n'avais pas déclaré lau début de la classe la variable mat2.
On peut remarquer aussi grâce aux erreurs que détecte le compilateur que çan'aurait aussi pas fonctionné si j'avais déclaré mat2 au debut de la fonctinn ChangeAlpha
comme cecii et non en début de classe
Code:
1 2 3 4 5 6 7 8
|
Material ChangeAlpha(Material mat1, float alphaValue)
{ Material mat2;
Color oldColor = mat.color;
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, alphaValue);
mat.SetColor("_Color", newColor);
return mat2;
} |
Le seul cas où le compilateur ne me retourne pas d'erreur est celui-ci:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class transp : MonoBehaviour
{
public Material mat;
Material mat2; //j'aurai pu écrire aussi Material mat1,mat2;
float alphaValue;
public Mesh mesh;
public GameObject obj;
// Start is called before the first frame update
void Start()
{
obj.AddComponent<MeshRenderer>().material=mat;
obj.AddComponent<MeshFilter>().mesh = mesh;
var cubeRenderer1 = obj.GetComponent<Renderer>();
cubeRenderer1.material.SetColor("_Color", Color.red);
obj.AddComponent<MeshRenderer>().material=ChangeAlpha(mat,0);
}
// Update is called once per frame
void Update()
{
}
Material ChangeAlpha(Material mat1, float alphaValue)
{
Color oldColor = mat.color;
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, alphaValue);
mat.SetColor("_Color", newColor);
return mat2;
}
} |
Dois-je alors comprendre que les variables muettes d'une fonction prototype ne nécessitent pas d'être déclareées initialement dans la classe?
A mon fort avis oui étant donné qiue les arguments des méthodes d'une classe ne nécessitent pas d'etre déclarées initialement.Par contre,si le corps de la méthode utilise une variable,celle-ci doit etre déclarée initialement dans la classe.
voir cet exemple d'une classe::
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
lcass CName {
public:
char m_szFirst[sizeOfBuffer];
char m_szLast[sizeOfBuffer];
public:
void SetName(char* pszFirst, char* pszLast) {
strcpy(m_szFirst, pszFirst);
strcpy(m_szLast, pszLast);
}
}; |
merci de votre aide