Self-updating the program code
Self-updating the program code
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...
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...
Re: Self-updating the program code
Since FB is compiled those things are not preserved in the exe unless you include it as debug information.
Re: Self-updating the program code
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
Re: Self-updating the program code
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.
Re: Self-updating the program code
Yes you are right, I misunderstood the question I thought it needed to change the name of a function to assign it new values.
Re: Self-updating the program code
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.
Re: Self-updating the program code
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.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.
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.
Re: Self-updating the program code
Fair enough. But I strongly dispute there is some magical definition of "true" classes.
Re: Self-updating the program code
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.
Re: Self-updating the program code
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?
Re: Self-updating the program code
Do static member procedures have access to instance data? If not, that sounds wasteful.
Construction (implicit or not, how to handle exceptions and thus memory safety in constructors?)- Implicitly having a default virtual destructor (for full compatibility with polymorphism).
- others???
Does an instance imply a reference or not?
Class variables.
classtype types and variables.
Multiple inheritance/interfaces etc.
Re: Self-updating the program code
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
Re: Self-updating the program code
Thanks!dodicat wrote: ↑Feb 13, 2022 22:28using 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
