Self-updating the program code

New to FreeBASIC? Post your questions here.
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Self-updating the program code

Post by gerry »

Hello, FreeBasic Team!

A bit of a strange question..

Is it possible that after each program launch, the names of variables, functions, and so on constantly change?

If so, throw an example
More for the sake of interest than use...
Imortis
Moderator
Posts: 1982
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Self-updating the program code

Post by Imortis »

Since FB is compiled those things are not preserved in the exe unless you include it as debug information.
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Self-updating the program code

Post by gerry »

Imortis wrote: Feb 10, 2022 16:43 Since FB is compiled those things are not preserved in the exe unless you include it as debug information.
Understood, thank you)
exagonx
Posts: 339
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: Self-updating the program code

Post by exagonx »

gerry wrote: Feb 10, 2022 16:05 Hello, FreeBasic Team!

A bit of a strange question..

Is it possible that after each program launch, the names of variables, functions, and so on constantly change?

If so, throw an example
More for the sake of interest than use...
No you cant , but you can make a class object instead

Ill make you an example:

Code: Select all


' This is an example of how create a personal object class in FreeBASIC
'
' 
'
' type nameofclass
'	dim myvar1 as integer
'	dim myvar2 as integer
' end type
 
 
 
' Creation the personal class

type People
	'the vars where store the data
	dim Firstname as string
	dim Surname as string
	dim AgeBirth as integer
	dim MontBirth as integer
	dim DayBirth as integer
	dim Gender as string
	
	'the function where retrive the age
	declare function CurrentAge()as integer
end type
function People.CurrentAge()as integer
	dim myage as integer
	dim currentyear as integer
	dim currentmont as integer
	dim currentday as integer
	
	' retrive the year , mont and day current
	currentyear = val(right(date,4))
	currentmont = val(mid(date,4,2))
	currentday = val(left(date,2))
	
	'calculate the age
	myage = currentyear - this.AgeBirth - 1
	if currentmont >= this.MontBirth then
		if currentday >= this.DayBirth then
			myage = myage + 1
		end if
	
	end if
	return myage
end function
	

'Now you dimension the array as class
' in this way you can use the same function with different name as you need
dim Person(3) as People


dim as integer Cicle = 0

print "Insert data"
do 

	line input "Name:", Person(Cicle).Firstname
	line input "Surname:", Person(Cicle).Surname
	input "Age Birth: ", Person(Cicle).AgeBirth
	input "Mont Birth: ", Person(Cicle).MontBirth
	input "Day Birth: ", Person(Cicle).DayBirth
	line input "Gender: ", Person(Cicle).Gender
	
	print "Age: " & Person(Cicle).CurrentAge()
	Cicle = Cicle + 1
loop while Cicle < 4
Munair
Posts: 1289
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Self-updating the program code

Post by Munair »

exagonx wrote: Feb 11, 2022 15:16
gerry wrote: Feb 10, 2022 16:05 Is it possible that after each program launch, the names of variables, functions, and so on constantly change?
No you cant , but you can make a class object instead
First, it is not a class in the true sense of the word (FB doesn't fully support classes).

Second, your example is not related in any way to the OP's question about changing the names of identifiers after each program launch. As Imortis correctly pointed out, symbols are lost after compilation.
exagonx
Posts: 339
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: Self-updating the program code

Post by exagonx »

Munair wrote: Feb 11, 2022 20:19
First, it is not a class in the true sense of the word (FB doesn't fully support classes).

Second, your example is not related in any way to the OP's question about changing the names of identifiers after each program launch.
Yes you are right, I misunderstood the question I thought it needed to change the name of a function to assign it new values.
caseih
Posts: 2197
Joined: Feb 26, 2007 5:32

Re: Self-updating the program code

Post by caseih »

Munair wrote: Feb 11, 2022 20:19First, it is not a class in the true sense of the word (FB doesn't fully support classes).
What do you mean by that?
Munair
Posts: 1289
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Self-updating the program code

Post by Munair »

There are a few things I can think of that FB "classes" still do not support, which have already been discussed multiple times on this forum. I had a discussion a few years back about having full control over when (nested) objects are instantiated, which is currently not possible in FB without using pointers (rather than references): viewtopic.php?p=254996#p254996.
exagonx
Posts: 339
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: Self-updating the program code

Post by exagonx »

Munair wrote: Feb 12, 2022 9:01 There are a few things I can think of that FB "classes" still do not support, which have already been discussed multiple times on this forum. I had a discussion a few years back about having full control over when (nested) objects are instantiated, which is currently not possible in FB without using pointers (rather than references): viewtopic.php?p=254996#p254996.
It is true that type does not create a class in the true sense of the word, but I have used (I know wrongly) the term class because the example shown by me in other languages can only be done with a class, certainly in many cases it is mandatory use pointers but we must remember that FreeBASIC even if it uses BASIC syntax remains C.

And for me it is a masterpiece considering that anyone even without programming knowledge can use it.

Ps.
I am among those who have learned programming by self-taught, often finding myself creating complex things that in the absence of a learning base I often get stuck.
caseih
Posts: 2197
Joined: Feb 26, 2007 5:32

Re: Self-updating the program code

Post by caseih »

Fair enough. But I strongly dispute there is some magical definition of "true" classes.
exagonx
Posts: 339
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: Self-updating the program code

Post by exagonx »

caseih wrote: Feb 12, 2022 15:50 Fair enough. But I strongly dispute there is some magical definition of "true" classes.
I agree with you, considering that the PHP classes operate with some differences between the classes of C ++, C # and Java, each programming language has its own syntax management, and structure and why not? even classes.
fxm
Moderator
Posts: 12577
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Self-updating the program code

Post by fxm »

Munair wrote: Feb 11, 2022 20:19
exagonx wrote: Feb 11, 2022 15:16
gerry wrote: Feb 10, 2022 16:05 Is it possible that after each program launch, the names of variables, functions, and so on constantly change?
No you cant , but you can make a class object instead
First, it is not a class in the true sense of the word (FB doesn't fully support classes).
A class would just be a particularization of a TYPE with default features:
- Implicitly extending OBJECT (from the base CLASS).
- Implicitly all non-static member procedures being VIRTUAL (or ABSTRACT if no procedure body is defined).
- Implicitly having a default virtual destructor (for full compatibility with polymorphism).
- others???

The CLASS keyword is already reserved, so why not implement it now like that?
marcov
Posts: 3503
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Self-updating the program code

Post by marcov »

fxm wrote: Feb 13, 2022 17:16 - Implicitly all non-static member procedures being VIRTUAL (or ABSTRACT if no procedure body is defined).
Do static member procedures have access to instance data? If not, that sounds wasteful.
- Implicitly having a default virtual destructor (for full compatibility with polymorphism).
- others???
Construction (implicit or not, how to handle exceptions and thus memory safety in constructors?)

Does an instance imply a reference or not?

Class variables.

classtype types and variables.

Multiple inheritance/interfaces etc.
dodicat
Posts: 8267
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Self-updating the program code

Post by dodicat »

gerry wrote: Feb 10, 2022 16:05 Hello, FreeBasic Team!

A bit of a strange question..

Is it possible that after each program launch, the names of variables, functions, and so on constantly change?

If so, throw an example
More for the sake of interest than use...
using arrays then maybe:

Code: Select all

#include "file.bi"
sub savefile(filename As String,p As String)
    Dim As long n=freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename:sleep:end
    End If
End sub

Function loadfile(file as string) as String
   dim as long  f=freefile
   if Open (file For Binary Access Read As #f)=0 then
    Dim As String text
    If Lof(f) > 0 Then
      text = String(Lof(f), 0)
      Get #f, , text
    End If
    Close #f
    return text
 else:Print file;" not found":end if
end Function

randomize

Dim Shared As Double ans(1 To 12),a(1 To 12),b(1 To 12),c(1 To 12),d(1 To 12),x(1 To 12)

Dim Shared As Long  i
if fileexists("_number.dat")=0 then savefile("_number.dat","0")
if fileexists("_number.dat") then i=valint(loadfile("_number.dat"))+1
if i>12 then kill "_number.dat":i=1
savefile("_number.dat",str(i))
For n As Long=1 To 12
      a(n)=rnd-rnd
      b(n)=-.5
      c(n)=.2
      d(n)=1
      x(n)=6
Next

#macro getfunction(z)
Function dothis##z As Double
      If i=z Then
            Print "index ";i
            Print "function name " + __function__
            Return a(i)*x(i)^2*b(i)*x(i)^2-c(i)*x(i)+d(i)
      Else
            Return 0
      End If
End Function
#endmacro
getfunction(1)
getfunction(2)
getfunction(3)
getfunction(4)
getfunction(5)
getfunction(6)
getfunction(7)
getfunction(8)
getfunction(9)
getfunction(10)
getfunction(11)
getfunction(12)

Select Case i
Case 1
      print dothis1
Case 2
      print dothis2
Case 3
      print dothis3
Case 4
      print dothis4
Case 5
      print dothis5
Case 6
      print dothis6
Case 7
      print dothis7
Case 8
      print dothis8
Case 9
      print dothis9
Case 10
      print dothis10
Case 11
      print dothis11
Case 12
      print dothis12    
      
End Select

Sleep


 
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Self-updating the program code

Post by gerry »

dodicat wrote: Feb 13, 2022 22:28
gerry wrote: Feb 10, 2022 16:05 Hello, FreeBasic Team!

A bit of a strange question..

Is it possible that after each program launch, the names of variables, functions, and so on constantly change?

If so, throw an example
More for the sake of interest than use...
using arrays then maybe:

Code: Select all

#include "file.bi"
sub savefile(filename As String,p As String)
    Dim As long n=freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename:sleep:end
    End If
End sub

Function loadfile(file as string) as String
   dim as long  f=freefile
   if Open (file For Binary Access Read As #f)=0 then
    Dim As String text
    If Lof(f) > 0 Then
      text = String(Lof(f), 0)
      Get #f, , text
    End If
    Close #f
    return text
 else:Print file;" not found":end if
end Function

randomize

Dim Shared As Double ans(1 To 12),a(1 To 12),b(1 To 12),c(1 To 12),d(1 To 12),x(1 To 12)

Dim Shared As Long  i
if fileexists("_number.dat")=0 then savefile("_number.dat","0")
if fileexists("_number.dat") then i=valint(loadfile("_number.dat"))+1
if i>12 then kill "_number.dat":i=1
savefile("_number.dat",str(i))
For n As Long=1 To 12
      a(n)=rnd-rnd
      b(n)=-.5
      c(n)=.2
      d(n)=1
      x(n)=6
Next

#macro getfunction(z)
Function dothis##z As Double
      If i=z Then
            Print "index ";i
            Print "function name " + __function__
            Return a(i)*x(i)^2*b(i)*x(i)^2-c(i)*x(i)+d(i)
      Else
            Return 0
      End If
End Function
#endmacro
getfunction(1)
getfunction(2)
getfunction(3)
getfunction(4)
getfunction(5)
getfunction(6)
getfunction(7)
getfunction(8)
getfunction(9)
getfunction(10)
getfunction(11)
getfunction(12)

Select Case i
Case 1
      print dothis1
Case 2
      print dothis2
Case 3
      print dothis3
Case 4
      print dothis4
Case 5
      print dothis5
Case 6
      print dothis6
Case 7
      print dothis7
Case 8
      print dothis8
Case 9
      print dothis9
Case 10
      print dothis10
Case 11
      print dothis11
Case 12
      print dothis12    
      
End Select

Sleep


 
Thanks! :wink:
Post Reply