# Referencing a variable of any data type

**URL:** <https://discourse.processing.org/t/referencing-a-variable-of-any-data-type/5213>\
**Category:** Libraries\
**Created:** [November 5, 2018, 3:08am UTC](https://discourse.processing.org/t/referencing-a-variable-of-any-data-type/5213 "2018-11-05T03:08:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mchar7](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@Mchar7](https://discourse.processing.org/u/Mchar7)\
**Post date:** [November 5, 2018, 3:08am UTC](https://discourse.processing.org/t/referencing-a-variable-of-any-data-type/5213/1 "2018-11-05T03:08:37Z")

</div>

So, I am trying to make a function that will display a certain picture OR play a video depending on whether the variable passed to the function as an argument is a picture (data type PImage) or a video (data type Movie). I am kinda stumped on how to let the function work with both data types. (Some context: I am making an instructions screen for my game and when the player presses a next button, the next game topic to be demonstrated is drawn. That can either be a short clip or an image.) Here’s what I have so far:

```auto
void newInstructionSlide(<< pic or vid variable>>, String filename)
{
  String type; // to be defined later

  // takes the extension of the input file
  String extension = filename.substring(filename.length()-3);

  if (extension=="mov" || extension=="mp4")
  {
    type="movie"; // distinguish media as video
  } else if (extension=="png" || extension=="jpg")
  {
    type="picture"; // distinguish media as picture
  } else
  {
    // give error to console
    println("error distinguishing file type; double-check filename");
  }

  if (type=="movie")
  {
    (draw video)
  } else if (type=="picture")
  {
    (draw picture)
  } else
  {
    (throw error)
  }

```

Any help is appreciated! 🙂

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 5, 2018, 3:36am UTC](https://discourse.processing.org/t/referencing-a-variable-of-any-data-type/5213/2 "2018-11-05T03:36:14Z")

</div>

You could just use both in different methods. Like

void sameMethod(Movie movie) {  
loadMovie();  
}

void sameMethod(Image image) {  
loadImage();  
}

it overrides the other method depending on the data type inputted.  
And if it’s just used like a branch method, then thats basically all you’ll need…

Btw, using the .getClass() method would be easier to identify the class of the file 😉

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 5, 2018, 3:58am UTC](https://discourse.processing.org/t/referencing-a-variable-of-any-data-type/5213/3 "2018-11-05T03:58:41Z")

</div>

[Processing.org/reference/String\_equals\_.html](http://Processing.org/reference/String_equals_.html)

---

<div class="post-metadata">

**Author:** ![Mchar7](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@Mchar7](https://discourse.processing.org/u/Mchar7)\
**Post date:** [November 5, 2018, 4:51am UTC](https://discourse.processing.org/t/referencing-a-variable-of-any-data-type/5213/4 "2018-11-05T04:51:34Z")

</div>

Okay, I will try to implement that into my game tomorrow morning. Thanks!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 5, 2018, 5:19am UTC](https://discourse.processing.org/t/referencing-a-variable-of-any-data-type/5213/5 "2018-11-05T05:19:48Z")

</div>

> [@Mchar7](#):
>
> filename.substring(filename.length()-3);

possibly should SPLIT on the “.”

```auto
String filename="picture_filename.jpg";
String[] fnameparts;
fnameparts = split(filename, '.');

//println(fnameparts.length);
if ( fnameparts.length == 2 ) {
  println("full name: "+filename+" name: "+fnameparts[0]+" ext: "+fnameparts[1]);
  if (fnameparts[1].equals("jpg") || fnameparts[1].equals("JPG") ) println(" + + + got a JPG file");
}

```
